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