2 * Copyright 2007 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.j2me;
19 import com.google.zxing.Result;
20 import com.google.zxing.client.result.EmailAddressParsedResult;
21 import com.google.zxing.client.result.ParsedResult;
22 import com.google.zxing.client.result.ParsedResultType;
23 import com.google.zxing.client.result.ResultParser;
24 import com.google.zxing.client.result.SMSParsedResult;
25 import com.google.zxing.client.result.TelParsedResult;
26 import com.google.zxing.client.result.ProductParsedResult;
27 import com.google.zxing.client.result.URIParsedResult;
29 import javax.microedition.io.ConnectionNotFoundException;
30 import javax.microedition.lcdui.Alert;
31 import javax.microedition.lcdui.AlertType;
32 import javax.microedition.lcdui.Canvas;
33 import javax.microedition.lcdui.Command;
34 import javax.microedition.lcdui.CommandListener;
35 import javax.microedition.lcdui.Display;
36 import javax.microedition.lcdui.Displayable;
37 import javax.microedition.media.Manager;
38 import javax.microedition.media.MediaException;
39 import javax.microedition.media.Player;
40 import javax.microedition.media.control.VideoControl;
41 import javax.microedition.midlet.MIDlet;
42 import javax.microedition.midlet.MIDletStateChangeException;
43 import java.io.IOException;
46 * <p>The actual reader application {@link MIDlet}.</p>
50 public final class ZXingMIDlet extends MIDlet {
52 private static final int ALERT_TIMEOUT_MS = 5 * 1000;
54 private Canvas canvas;
55 private Player player;
56 private VideoControl videoControl;
58 Displayable getCanvas() {
66 VideoControl getVideoControl() {
70 static MultimediaManager buildMultimediaManager() {
71 return new AdvancedMultimediaManager();
72 // Comment line above / uncomment below to make the basic version
73 // return new DefaultMultimediaManager();
76 protected void startApp() throws MIDletStateChangeException {
78 player = createPlayer();
80 MultimediaManager multimediaManager = buildMultimediaManager();
81 multimediaManager.setZoom(player);
82 multimediaManager.setExposure(player);
83 videoControl = (VideoControl) player.getControl("VideoControl");
84 canvas = new VideoCanvas(this);
85 canvas.setFullScreenMode(true);
86 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
87 videoControl.setDisplayLocation(0, 0);
88 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
89 videoControl.setVisible(true);
91 Display.getDisplay(this).setCurrent(canvas);
92 } catch (IOException ioe) {
93 throw new MIDletStateChangeException(ioe.toString());
94 } catch (MediaException me) {
95 throw new MIDletStateChangeException(me.toString());
99 private static Player createPlayer() throws IOException, MediaException {
100 // Try a workaround for Nokias, which want to use capture://image in some cases
101 Player player = null;
102 String platform = System.getProperty("microedition.platform");
103 if (platform != null && platform.indexOf("Nokia") >= 0) {
105 player = Manager.createPlayer("capture://image");
106 } catch (MediaException me) {
107 // if this fails, just continue with capture://video
109 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
110 // We should still try to continue
113 if (player == null) {
114 player = Manager.createPlayer("capture://video");
119 protected void pauseApp() {
120 if (player != null) {
123 } catch (MediaException me) {
130 protected void destroyApp(boolean unconditional) {
131 if (player != null) {
135 } catch (MediaException me) {
149 // Convenience methods to show dialogs
151 private void showOpenURL(String title, String display, final String uri) {
152 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
153 alert.setTimeout(ALERT_TIMEOUT_MS);
154 Command yes = new Command("Yes", Command.OK, 1);
155 alert.addCommand(yes);
156 Command no = new Command("No", Command.CANCEL, 1);
157 alert.addCommand(no);
158 CommandListener listener = new CommandListener() {
159 public void commandAction(Command command, Displayable displayable) {
160 if (command.getCommandType() == Command.OK) {
162 platformRequest(uri);
163 } catch (ConnectionNotFoundException cnfe) {
170 Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
174 alert.setCommandListener(listener);
178 private void showAlert(String title, String text) {
179 Alert alert = new Alert(title, text, null, AlertType.INFO);
180 alert.setTimeout(ALERT_TIMEOUT_MS);
184 void showError(Throwable t) {
185 String message = t.getMessage();
186 if (message != null && message.length() > 0) {
189 showError(t.toString());
193 void showError(String message) {
194 showAlert(new Alert("Error", message, null, AlertType.ERROR));
197 private void showAlert(Alert alert) {
198 Display display = Display.getDisplay(this);
199 display.setCurrent(alert, canvas);
202 void handleDecodedText(Result theResult) {
203 ParsedResult result = ResultParser.parseResult(theResult);
204 ParsedResultType type = result.getType();
205 if (type.equals(ParsedResultType.URI)) {
206 String uri = ((URIParsedResult) result).getURI();
207 showOpenURL("Open Web Page?", uri, uri);
208 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
209 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
210 showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
211 } else if (type.equals(ParsedResultType.SMS)) {
212 SMSParsedResult smsResult = (SMSParsedResult) result;
213 showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
214 } else if (type.equals(ParsedResultType.PRODUCT)) {
215 ProductParsedResult productResult = (ProductParsedResult) result;
216 String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
217 showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
218 } else if (type.equals(ParsedResultType.TEL)) {
219 TelParsedResult telResult = (TelParsedResult) result;
220 showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
222 showAlert("Barcode Detected", result.getDisplayResult());