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 protected void startApp() throws MIDletStateChangeException {
72 player = createPlayer();
74 MultimediaManager multimediaManager = new AdvancedMultimediaManager();
75 // Comment line above / uncomment below to make the basic version
76 //MultimediaManager multimediaManager = new DefaultMultimediaManager();
77 multimediaManager.setZoom(player);
78 multimediaManager.setExposure(player);
79 videoControl = (VideoControl) player.getControl("VideoControl");
80 canvas = new VideoCanvas(this);
81 canvas.setFullScreenMode(true);
82 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
83 videoControl.setDisplayLocation(0, 0);
84 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
85 videoControl.setVisible(true);
87 Display.getDisplay(this).setCurrent(canvas);
88 } catch (IOException ioe) {
89 throw new MIDletStateChangeException(ioe.toString());
90 } catch (MediaException me) {
91 throw new MIDletStateChangeException(me.toString());
95 private static Player createPlayer() throws IOException, MediaException {
96 // Try a workaround for Nokias, which want to use capture://image in some cases
98 String platform = System.getProperty("microedition.platform");
99 if (platform != null && platform.indexOf("Nokia") >= 0) {
101 player = Manager.createPlayer("capture://image");
102 } catch (MediaException me) {
103 // if this fails, just continue with capture://video
105 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
106 // We should still try to continue
109 if (player == null) {
110 player = Manager.createPlayer("capture://video");
115 protected void pauseApp() {
116 if (player != null) {
119 } catch (MediaException me) {
126 protected void destroyApp(boolean unconditional) {
127 if (player != null) {
131 } catch (MediaException me) {
145 // Convenience methods to show dialogs
147 private void showOpenURL(String title, String display, final String uri) {
148 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
149 alert.setTimeout(ALERT_TIMEOUT_MS);
150 Command yes = new Command("Yes", Command.OK, 1);
151 alert.addCommand(yes);
152 Command no = new Command("No", Command.CANCEL, 1);
153 alert.addCommand(no);
154 CommandListener listener = new CommandListener() {
155 public void commandAction(Command command, Displayable displayable) {
156 if (command.getCommandType() == Command.OK) {
158 platformRequest(uri);
159 } catch (ConnectionNotFoundException cnfe) {
166 Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
170 alert.setCommandListener(listener);
174 private void showAlert(String title, String text) {
175 Alert alert = new Alert(title, text, null, AlertType.INFO);
176 alert.setTimeout(ALERT_TIMEOUT_MS);
180 void showError(Throwable t) {
181 String message = t.getMessage();
182 if (message != null && message.length() > 0) {
185 showError(t.toString());
189 void showError(String message) {
190 showAlert(new Alert("Error", message, null, AlertType.ERROR));
193 private void showAlert(Alert alert) {
194 Display display = Display.getDisplay(this);
195 display.setCurrent(alert, canvas);
198 void handleDecodedText(Result theResult) {
199 ParsedResult result = ResultParser.parseResult(theResult);
200 ParsedResultType type = result.getType();
201 if (type.equals(ParsedResultType.URI)) {
202 String uri = ((URIParsedResult) result).getURI();
203 showOpenURL("Open Web Page?", uri, uri);
204 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
205 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
206 showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
207 } else if (type.equals(ParsedResultType.SMS)) {
208 SMSParsedResult smsResult = (SMSParsedResult) result;
209 showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
210 } else if (type.equals(ParsedResultType.PRODUCT)) {
211 ProductParsedResult productResult = (ProductParsedResult) result;
212 String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
213 showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
214 } else if (type.equals(ParsedResultType.TEL)) {
215 TelParsedResult telResult = (TelParsedResult) result;
216 showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
218 showAlert("Barcode Detected", result.getDisplayResult());