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.client.result.BookmarkDoCoMoResult;
20 import com.google.zxing.client.result.EmailAddressResult;
21 import com.google.zxing.client.result.EmailDoCoMoResult;
22 import com.google.zxing.client.result.ParsedReaderResult;
23 import com.google.zxing.client.result.ParsedReaderResultType;
24 import com.google.zxing.client.result.UPCParsedResult;
25 import com.google.zxing.client.result.URIParsedResult;
27 import javax.microedition.io.ConnectionNotFoundException;
28 import javax.microedition.lcdui.Alert;
29 import javax.microedition.lcdui.AlertType;
30 import javax.microedition.lcdui.Canvas;
31 import javax.microedition.lcdui.Command;
32 import javax.microedition.lcdui.CommandListener;
33 import javax.microedition.lcdui.Display;
34 import javax.microedition.lcdui.Displayable;
35 import javax.microedition.media.Manager;
36 import javax.microedition.media.MediaException;
37 import javax.microedition.media.Player;
38 import javax.microedition.media.control.VideoControl;
39 import javax.microedition.midlet.MIDlet;
40 import javax.microedition.midlet.MIDletStateChangeException;
41 import java.io.IOException;
44 * <p>The actual reader application {@link MIDlet}.</p>
46 * @author Sean Owen (srowen@google.com)
48 public final class ZXingMIDlet extends MIDlet {
50 private Canvas canvas;
51 private Player player;
52 private VideoControl videoControl;
58 VideoControl getVideoControl() {
62 protected void startApp() throws MIDletStateChangeException {
64 player = createPlayer();
66 AdvancedMultimediaManager.setZoom(player);
67 videoControl = (VideoControl) player.getControl("VideoControl");
68 canvas = new VideoCanvas(this);
69 canvas.setFullScreenMode(true);
70 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
71 videoControl.setDisplayLocation(0, 0);
72 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
73 videoControl.setVisible(true);
75 Display.getDisplay(this).setCurrent(canvas);
76 } catch (IOException ioe) {
77 throw new MIDletStateChangeException(ioe.toString());
78 } catch (MediaException me) {
79 throw new MIDletStateChangeException(me.toString());
83 private static Player createPlayer() throws IOException, MediaException {
84 // Try a workaround for Nokias, which want to use capture://image in some cases
86 String platform = System.getProperty("microedition.platform");
87 if (platform != null && platform.indexOf("Nokia") >= 0) {
89 player = Manager.createPlayer("capture://image");
90 } catch (MediaException me) {
91 // if this fails, just continue with capture://video
93 // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
94 // We should still try to continue
98 player = Manager.createPlayer("capture://video");
103 protected void pauseApp() {
104 if (player != null) {
107 } catch (MediaException me) {
114 protected void destroyApp(boolean unconditional) {
115 if (player != null) {
119 } catch (MediaException me) {
133 // Convenience methods to show dialogs
135 private void showOpenURL(String title, final String display, final String uri) {
136 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
137 alert.setTimeout(Alert.FOREVER);
138 Command yes = new Command("Yes", Command.OK, 1);
139 alert.addCommand(yes);
140 Command no = new Command("No", Command.CANCEL, 1);
141 alert.addCommand(no);
142 CommandListener listener = new CommandListener() {
143 public void commandAction(Command command, Displayable displayable) {
144 if (command.getCommandType() == Command.OK) {
146 platformRequest(uri);
147 } catch (ConnectionNotFoundException cnfe) {
154 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
158 alert.setCommandListener(listener);
162 private void showAlert(String title, String text) {
163 Alert alert = new Alert(title, text, null, AlertType.INFO);
164 alert.setTimeout(Alert.FOREVER);
168 void showError(Throwable t) {
169 String message = t.getMessage();
170 if (message != null && message.length() > 0) {
173 showError(t.toString());
177 void showError(String message) {
178 showAlert(new Alert("Error", message, null, AlertType.ERROR));
181 private void showAlert(Alert alert) {
182 Display display = Display.getDisplay(this);
183 display.setCurrent(alert, canvas);
186 void handleDecodedText(String text) {
187 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
188 ParsedReaderResultType type = result.getType();
189 if (type.equals(ParsedReaderResultType.URI)) {
190 String uri = ((URIParsedResult) result).getURI();
191 showOpenURL("Open Web Page?", uri, uri);
192 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
193 String uri = ((BookmarkDoCoMoResult) result).getURI();
194 showOpenURL("Open Web Page?", uri, uri);
195 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
196 String email = ((EmailDoCoMoResult) result).getTo();
197 showOpenURL("Compose E-mail?", email, "mailto:" + email);
198 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
199 String email = ((EmailAddressResult) result).getEmailAddress();
200 showOpenURL("Compose E-mail?", email, "mailto:" + email);
201 } else if (type.equals(ParsedReaderResultType.UPC)) {
202 String upc = ((UPCParsedResult) result).getUPC();
203 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
204 showOpenURL("Look Up Barcode Online?", upc, uri);
206 showAlert("Barcode Detected", result.getDisplayResult());