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
95 player = Manager.createPlayer("capture://video");
100 protected void pauseApp() {
101 if (player != null) {
104 } catch (MediaException me) {
111 protected void destroyApp(boolean unconditional) {
112 if (player != null) {
116 } catch (MediaException me) {
130 // Convenience methods to show dialogs
132 private void showOpenURL(String title, final String display, final String uri) {
133 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
134 alert.setTimeout(Alert.FOREVER);
135 Command yes = new Command("Yes", Command.OK, 1);
136 alert.addCommand(yes);
137 Command no = new Command("No", Command.CANCEL, 1);
138 alert.addCommand(no);
139 CommandListener listener = new CommandListener() {
140 public void commandAction(Command command, Displayable displayable) {
141 if (command.getCommandType() == Command.OK) {
143 platformRequest(uri);
144 } catch (ConnectionNotFoundException cnfe) {
151 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
155 alert.setCommandListener(listener);
159 private void showAlert(String title, String text) {
160 Alert alert = new Alert(title, text, null, AlertType.INFO);
161 alert.setTimeout(Alert.FOREVER);
165 void showError(Throwable t) {
166 String message = t.getMessage();
167 if (message != null && message.length() > 0) {
170 showError(t.toString());
174 void showError(String message) {
175 showAlert(new Alert("Error", message, null, AlertType.ERROR));
178 private void showAlert(Alert alert) {
179 Display display = Display.getDisplay(this);
180 display.setCurrent(alert, canvas);
183 void handleDecodedText(String text) {
184 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
185 ParsedReaderResultType type = result.getType();
186 if (type.equals(ParsedReaderResultType.URI)) {
187 String uri = ((URIParsedResult) result).getURI();
188 showOpenURL("Open web page?", uri, uri);
189 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
190 String uri = ((BookmarkDoCoMoResult) result).getURI();
191 showOpenURL("Open web page?", uri, uri);
192 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
193 String email = ((EmailDoCoMoResult) result).getTo();
194 showOpenURL("Compose e-mail?", email, "mailto:" + email);
195 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
196 String email = ((EmailAddressResult) result).getEmailAddress();
197 showOpenURL("Compose e-mail?", email, "mailto:" + email);
198 } else if (type.equals(ParsedReaderResultType.UPC)) {
199 String upc = ((UPCParsedResult) result).getUPC();
200 String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
201 showOpenURL("Look up UPC?", upc, uri);
203 showAlert("Barcode detected", result.getDisplayResult());