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.TelParsedResult;
26 import com.google.zxing.client.result.UPCParsedResult;
27 import com.google.zxing.client.result.URIParsedResult;
28 import com.google.zxing.client.result.URLTOParsedResult;
30 import javax.microedition.io.ConnectionNotFoundException;
31 import javax.microedition.lcdui.Alert;
32 import javax.microedition.lcdui.AlertType;
33 import javax.microedition.lcdui.Canvas;
34 import javax.microedition.lcdui.Command;
35 import javax.microedition.lcdui.CommandListener;
36 import javax.microedition.lcdui.Display;
37 import javax.microedition.lcdui.Displayable;
38 import javax.microedition.media.Manager;
39 import javax.microedition.media.MediaException;
40 import javax.microedition.media.Player;
41 import javax.microedition.media.control.VideoControl;
42 import javax.microedition.midlet.MIDlet;
43 import javax.microedition.midlet.MIDletStateChangeException;
44 import java.io.IOException;
47 * <p>The actual reader application {@link MIDlet}.</p>
49 * @author Sean Owen (srowen@google.com)
51 public final class ZXingMIDlet extends MIDlet {
53 private Canvas canvas;
54 private Player player;
55 private VideoControl videoControl;
65 VideoControl getVideoControl() {
69 protected void startApp() throws MIDletStateChangeException {
71 player = createPlayer();
73 AdvancedMultimediaManager.setZoom(player);
74 videoControl = (VideoControl) player.getControl("VideoControl");
75 canvas = new VideoCanvas(this);
76 canvas.setFullScreenMode(true);
77 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
78 videoControl.setDisplayLocation(0, 0);
79 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
80 videoControl.setVisible(true);
82 Display.getDisplay(this).setCurrent(canvas);
83 } catch (IOException ioe) {
84 throw new MIDletStateChangeException(ioe.toString());
85 } catch (MediaException me) {
86 throw new MIDletStateChangeException(me.toString());
90 private static Player createPlayer() throws IOException, MediaException {
91 // Try a workaround for Nokias, which want to use capture://image in some cases
93 String platform = System.getProperty("microedition.platform");
94 if (platform != null && platform.indexOf("Nokia") >= 0) {
96 player = Manager.createPlayer("capture://image");
97 } catch (MediaException me) {
98 // if this fails, just continue with capture://video
100 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
101 // We should still try to continue
104 if (player == null) {
105 player = Manager.createPlayer("capture://video");
110 protected void pauseApp() {
111 if (player != null) {
114 } catch (MediaException me) {
121 protected void destroyApp(boolean unconditional) {
122 if (player != null) {
126 } catch (MediaException me) {
140 // Convenience methods to show dialogs
142 private void showOpenURL(String title, final String display, final String uri) {
143 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
144 alert.setTimeout(Alert.FOREVER);
145 Command yes = new Command("Yes", Command.OK, 1);
146 alert.addCommand(yes);
147 Command no = new Command("No", Command.CANCEL, 1);
148 alert.addCommand(no);
149 CommandListener listener = new CommandListener() {
150 public void commandAction(Command command, Displayable displayable) {
151 if (command.getCommandType() == Command.OK) {
153 platformRequest(uri);
154 } catch (ConnectionNotFoundException cnfe) {
161 Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
165 alert.setCommandListener(listener);
169 private void showAlert(String title, String text) {
170 Alert alert = new Alert(title, text, null, AlertType.INFO);
171 alert.setTimeout(Alert.FOREVER);
175 void showError(Throwable t) {
176 String message = t.getMessage();
177 if (message != null && message.length() > 0) {
180 showError(t.toString());
184 void showError(String message) {
185 showAlert(new Alert("Error", message, null, AlertType.ERROR));
188 private void showAlert(Alert alert) {
189 Display display = Display.getDisplay(this);
190 display.setCurrent(alert, canvas);
193 void handleDecodedText(Result theResult) {
194 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
195 ParsedReaderResultType type = result.getType();
196 if (type.equals(ParsedReaderResultType.URI)) {
197 String uri = ((URIParsedResult) result).getURI();
198 showOpenURL("Open Web Page?", uri, uri);
199 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
200 String uri = ((BookmarkDoCoMoParsedResult) result).getURI();
201 showOpenURL("Open Web Page?", uri, uri);
202 } else if (type.equals(ParsedReaderResultType.URLTO)) {
203 String uri = ((URLTOParsedResult) result).getURI();
204 showOpenURL("Open Web Page?", uri, uri);
205 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
206 String email = ((EmailDoCoMoParsedResult) result).getTo();
207 showOpenURL("Compose E-mail?", email, "mailto:" + email);
208 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
209 String email = ((EmailAddressParsedResult) result).getEmailAddress();
210 showOpenURL("Compose E-mail?", email, "mailto:" + email);
211 //} else if (type.equals(ParsedReaderResultType.GEO)) {
212 // GeoParsedResult geoResult = (GeoParsedResult) result;
213 // showOpenURL("Open In Google Maps?", geoResult.getDisplayResult(), geoResult.getGoogleMapsURI());
214 } else if (type.equals(ParsedReaderResultType.UPC)) {
215 String upc = ((UPCParsedResult) result).getUPC();
216 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
217 showOpenURL("Look Up Barcode Online?", upc, uri);
218 } else if (type.equals(ParsedReaderResultType.TEL)) {
219 String number = ((TelParsedResult) result).getNumber();
220 showOpenURL("Dial Number?", number, "tel:" + number);
222 showAlert("Barcode Detected", result.getDisplayResult());