2 * Copyright 2007 Google Inc.
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.BookmarkDoCoMoParsedResult;
21 import com.google.zxing.client.result.EmailAddressParsedResult;
22 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
23 import com.google.zxing.client.result.ParsedReaderResult;
24 import com.google.zxing.client.result.ParsedReaderResultType;
25 import com.google.zxing.client.result.SMSParsedResult;
26 import com.google.zxing.client.result.SMSTOParsedResult;
27 import com.google.zxing.client.result.TelParsedResult;
28 import com.google.zxing.client.result.UPCParsedResult;
29 import com.google.zxing.client.result.URIParsedResult;
30 import com.google.zxing.client.result.URLTOParsedResult;
32 import javax.microedition.io.ConnectionNotFoundException;
33 import javax.microedition.lcdui.Alert;
34 import javax.microedition.lcdui.AlertType;
35 import javax.microedition.lcdui.Canvas;
36 import javax.microedition.lcdui.Command;
37 import javax.microedition.lcdui.CommandListener;
38 import javax.microedition.lcdui.Display;
39 import javax.microedition.lcdui.Displayable;
40 import javax.microedition.media.Manager;
41 import javax.microedition.media.MediaException;
42 import javax.microedition.media.Player;
43 import javax.microedition.media.control.VideoControl;
44 import javax.microedition.midlet.MIDlet;
45 import javax.microedition.midlet.MIDletStateChangeException;
46 import java.io.IOException;
49 * <p>The actual reader application {@link MIDlet}.</p>
51 * @author Sean Owen (srowen@google.com)
53 public final class ZXingMIDlet extends MIDlet {
55 private Canvas canvas;
56 private Player player;
57 private VideoControl videoControl;
59 Displayable getCanvas() {
67 VideoControl getVideoControl() {
71 protected void startApp() throws MIDletStateChangeException {
73 player = createPlayer();
75 AdvancedMultimediaManager.setZoom(player);
76 AdvancedMultimediaManager.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 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
198 ParsedReaderResultType type = result.getType();
199 if (type.equals(ParsedReaderResultType.URI)) {
200 String uri = ((URIParsedResult) result).getURI();
201 showOpenURL("Open Web Page?", uri, uri);
202 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
203 String uri = ((BookmarkDoCoMoParsedResult) result).getURI();
204 showOpenURL("Open Web Page?", uri, uri);
205 } else if (type.equals(ParsedReaderResultType.URLTO)) {
206 String uri = ((URLTOParsedResult) result).getURI();
207 showOpenURL("Open Web Page?", uri, uri);
208 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
209 EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
210 showOpenURL("Compose E-mail?", emailResult.getTo(), emailResult.getMailtoURI());
211 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
212 EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
213 showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
214 } else if (type.equals(ParsedReaderResultType.SMS)) {
215 SMSParsedResult smsResult = (SMSParsedResult) result;
216 showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
217 } else if (type.equals(ParsedReaderResultType.SMSTO)) {
218 SMSTOParsedResult smsToResult = (SMSTOParsedResult) result;
219 showOpenURL("Compose SMS?", smsToResult.getNumber(), smsToResult.getSMSURI());
220 } else if (type.equals(ParsedReaderResultType.UPC)) {
221 String upc = ((UPCParsedResult) result).getUPC();
222 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
223 showOpenURL("Look Up Barcode Online?", upc, uri);
224 } else if (type.equals(ParsedReaderResultType.TEL)) {
225 TelParsedResult telResult = (TelParsedResult) result;
226 showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
228 showAlert("Barcode Detected", result.getDisplayResult());