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.UPCParsedResult;
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>
48 * @author Sean Owen (srowen@google.com)
50 public final class ZXingMIDlet extends MIDlet {
52 private Canvas canvas;
53 private Player player;
54 private VideoControl videoControl;
56 Displayable getCanvas() {
64 VideoControl getVideoControl() {
68 protected void startApp() throws MIDletStateChangeException {
70 player = createPlayer();
72 MultimediaManager multimediaManager = new AdvancedMultimediaManager();
73 // Comment line above / uncomment below to make the basic version
74 //MultimediaManager multimediaManager = new DefaultMultimediaManager();
75 multimediaManager.setZoom(player);
76 multimediaManager.setExposure(player);
77 videoControl = (VideoControl) player.getControl("VideoControl");
78 canvas = new VideoCanvas(this);
79 canvas.setFullScreenMode(true);
80 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
81 videoControl.setDisplayLocation(0, 0);
82 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
83 videoControl.setVisible(true);
85 Display.getDisplay(this).setCurrent(canvas);
86 } catch (IOException ioe) {
87 throw new MIDletStateChangeException(ioe.toString());
88 } catch (MediaException me) {
89 throw new MIDletStateChangeException(me.toString());
93 private static Player createPlayer() throws IOException, MediaException {
94 // Try a workaround for Nokias, which want to use capture://image in some cases
96 String platform = System.getProperty("microedition.platform");
97 if (platform != null && platform.indexOf("Nokia") >= 0) {
99 player = Manager.createPlayer("capture://image");
100 } catch (MediaException me) {
101 // if this fails, just continue with capture://video
103 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
104 // We should still try to continue
107 if (player == null) {
108 player = Manager.createPlayer("capture://video");
113 protected void pauseApp() {
114 if (player != null) {
117 } catch (MediaException me) {
124 protected void destroyApp(boolean unconditional) {
125 if (player != null) {
129 } catch (MediaException me) {
143 // Convenience methods to show dialogs
145 private void showOpenURL(String title, String display, final String uri) {
146 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
147 alert.setTimeout(Alert.FOREVER);
148 Command yes = new Command("Yes", Command.OK, 1);
149 alert.addCommand(yes);
150 Command no = new Command("No", Command.CANCEL, 1);
151 alert.addCommand(no);
152 CommandListener listener = new CommandListener() {
153 public void commandAction(Command command, Displayable displayable) {
154 if (command.getCommandType() == Command.OK) {
156 platformRequest(uri);
157 } catch (ConnectionNotFoundException cnfe) {
164 Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
168 alert.setCommandListener(listener);
172 private void showAlert(String title, String text) {
173 Alert alert = new Alert(title, text, null, AlertType.INFO);
174 alert.setTimeout(Alert.FOREVER);
178 void showError(Throwable t) {
179 String message = t.getMessage();
180 if (message != null && message.length() > 0) {
183 showError(t.toString());
187 void showError(String message) {
188 showAlert(new Alert("Error", message, null, AlertType.ERROR));
191 private void showAlert(Alert alert) {
192 Display display = Display.getDisplay(this);
193 display.setCurrent(alert, canvas);
196 void handleDecodedText(Result theResult) {
197 ParsedResult result = ResultParser.parseResult(theResult);
198 ParsedResultType type = result.getType();
199 if (type.equals(ParsedResultType.URI)) {
200 String uri = ((URIParsedResult) result).getURI();
201 showOpenURL("Open Web Page?", uri, uri);
202 } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
203 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
204 showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
205 } else if (type.equals(ParsedResultType.SMS)) {
206 SMSParsedResult smsResult = (SMSParsedResult) result;
207 showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
208 } else if (type.equals(ParsedResultType.UPC)) {
209 String upc = ((UPCParsedResult) result).getUPC();
210 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
211 showOpenURL("Look Up Barcode Online?", upc, uri);
212 } else if (type.equals(ParsedResultType.TEL)) {
213 TelParsedResult telResult = (TelParsedResult) result;
214 showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
216 showAlert("Barcode Detected", result.getDisplayResult());