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.UPCParsedResult;
26 import com.google.zxing.client.result.URIParsedResult;
27 import com.google.zxing.client.result.URLTOParsedResult;
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;
64 VideoControl getVideoControl() {
68 protected void startApp() throws MIDletStateChangeException {
70 player = createPlayer();
72 AdvancedMultimediaManager.setZoom(player);
73 videoControl = (VideoControl) player.getControl("VideoControl");
74 canvas = new VideoCanvas(this);
75 canvas.setFullScreenMode(true);
76 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
77 videoControl.setDisplayLocation(0, 0);
78 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
79 videoControl.setVisible(true);
81 Display.getDisplay(this).setCurrent(canvas);
82 } catch (IOException ioe) {
83 throw new MIDletStateChangeException(ioe.toString());
84 } catch (MediaException me) {
85 throw new MIDletStateChangeException(me.toString());
89 private static Player createPlayer() throws IOException, MediaException {
90 // Try a workaround for Nokias, which want to use capture://image in some cases
92 String platform = System.getProperty("microedition.platform");
93 if (platform != null && platform.indexOf("Nokia") >= 0) {
95 player = Manager.createPlayer("capture://image");
96 } catch (MediaException me) {
97 // if this fails, just continue with capture://video
99 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
100 // We should still try to continue
103 if (player == null) {
104 player = Manager.createPlayer("capture://video");
109 protected void pauseApp() {
110 if (player != null) {
113 } catch (MediaException me) {
120 protected void destroyApp(boolean unconditional) {
121 if (player != null) {
125 } catch (MediaException me) {
139 // Convenience methods to show dialogs
141 private void showOpenURL(String title, final String display, final String uri) {
142 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
143 alert.setTimeout(Alert.FOREVER);
144 Command yes = new Command("Yes", Command.OK, 1);
145 alert.addCommand(yes);
146 Command no = new Command("No", Command.CANCEL, 1);
147 alert.addCommand(no);
148 CommandListener listener = new CommandListener() {
149 public void commandAction(Command command, Displayable displayable) {
150 if (command.getCommandType() == Command.OK) {
152 platformRequest(uri);
153 } catch (ConnectionNotFoundException cnfe) {
160 Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
164 alert.setCommandListener(listener);
168 private void showAlert(String title, String text) {
169 Alert alert = new Alert(title, text, null, AlertType.INFO);
170 alert.setTimeout(Alert.FOREVER);
174 void showError(Throwable t) {
175 String message = t.getMessage();
176 if (message != null && message.length() > 0) {
179 showError(t.toString());
183 void showError(String message) {
184 showAlert(new Alert("Error", message, null, AlertType.ERROR));
187 private void showAlert(Alert alert) {
188 Display display = Display.getDisplay(this);
189 display.setCurrent(alert, canvas);
192 void handleDecodedText(Result theResult) {
193 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
194 ParsedReaderResultType type = result.getType();
195 if (type.equals(ParsedReaderResultType.URI)) {
196 String uri = ((URIParsedResult) result).getURI();
197 showOpenURL("Open Web Page?", uri, uri);
198 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
199 String uri = ((BookmarkDoCoMoParsedResult) result).getURI();
200 showOpenURL("Open Web Page?", uri, uri);
201 } else if (type.equals(ParsedReaderResultType.URLTO)) {
202 String uri = ((URLTOParsedResult) result).getURI();
203 showOpenURL("Open Web Page?", uri, uri);
204 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
205 String email = ((EmailDoCoMoParsedResult) result).getTo();
206 showOpenURL("Compose E-mail?", email, "mailto:" + email);
207 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
208 String email = ((EmailAddressParsedResult) result).getEmailAddress();
209 showOpenURL("Compose E-mail?", email, "mailto:" + email);
210 //} else if (type.equals(ParsedReaderResultType.GEO)) {
211 // GeoParsedResult geoResult = (GeoParsedResult) result;
212 // showOpenURL("Open In Google Maps?", geoResult.getDisplayResult(), geoResult.getGoogleMapsURI());
213 } else if (type.equals(ParsedReaderResultType.UPC)) {
214 String upc = ((UPCParsedResult) result).getUPC();
215 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
216 showOpenURL("Look Up Barcode Online?", upc, uri);
218 showAlert("Barcode Detected", result.getDisplayResult());