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.URIParsedResult;
26 import javax.microedition.io.ConnectionNotFoundException;
27 import javax.microedition.lcdui.Alert;
28 import javax.microedition.lcdui.AlertType;
29 import javax.microedition.lcdui.Canvas;
30 import javax.microedition.lcdui.Command;
31 import javax.microedition.lcdui.CommandListener;
32 import javax.microedition.lcdui.Display;
33 import javax.microedition.lcdui.Displayable;
34 import javax.microedition.media.Manager;
35 import javax.microedition.media.MediaException;
36 import javax.microedition.media.Player;
37 import javax.microedition.media.control.VideoControl;
38 import javax.microedition.midlet.MIDlet;
39 import javax.microedition.midlet.MIDletStateChangeException;
40 import java.io.IOException;
43 * <p>The actual reader application {@link MIDlet}.</p>
45 * @author Sean Owen (srowen@google.com)
47 public final class ZXingMIDlet extends MIDlet {
49 private Canvas canvas;
50 private Player player;
51 private VideoControl videoControl;
57 VideoControl getVideoControl() {
61 protected void startApp() throws MIDletStateChangeException {
63 player = createPlayer();
65 AdvancedMultimediaManager.setZoom(player);
66 videoControl = (VideoControl) player.getControl("VideoControl");
67 canvas = new VideoCanvas(this);
68 canvas.setFullScreenMode(true);
69 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
70 videoControl.setDisplayLocation(0, 0);
71 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
72 videoControl.setVisible(true);
74 Display.getDisplay(this).setCurrent(canvas);
75 } catch (IOException ioe) {
76 throw new MIDletStateChangeException(ioe.toString());
77 } catch (MediaException me) {
78 throw new MIDletStateChangeException(me.toString());
82 private static Player createPlayer() throws IOException, MediaException {
83 // Try a workaround for Nokias, which want to use capture://image in some cases
85 String platform = System.getProperty("microedition.platform");
86 if (platform != null && platform.indexOf("Nokia") >= 0) {
88 player = Manager.createPlayer("capture://image");
89 } catch (MediaException me) {
90 // if this fails, just continue with capture://video
94 player = Manager.createPlayer("capture://video");
99 protected void pauseApp() {
100 if (player != null) {
103 } catch (MediaException me) {
110 protected void destroyApp(boolean unconditional) {
111 if (player != null) {
115 } catch (MediaException me) {
129 // Convenience methods to show dialogs
131 private void showOpenURL(String title, final String display, final String uri) {
132 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
133 alert.setTimeout(Alert.FOREVER);
134 Command yes = new Command("Yes", Command.OK, 1);
135 alert.addCommand(yes);
136 Command no = new Command("No", Command.CANCEL, 1);
137 alert.addCommand(no);
138 CommandListener listener = new CommandListener() {
139 public void commandAction(Command command, Displayable displayable) {
140 if (command.getCommandType() == Command.OK) {
142 platformRequest(uri);
143 } catch (ConnectionNotFoundException cnfe) {
150 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
154 alert.setCommandListener(listener);
158 private void showAlert(String title, String text) {
159 Alert alert = new Alert(title, text, null, AlertType.INFO);
160 alert.setTimeout(Alert.FOREVER);
164 void showError(Throwable t) {
165 showError(t.getMessage());
168 void showError(String message) {
169 showAlert(new Alert("Error", message, null, AlertType.ERROR));
172 private void showAlert(Alert alert) {
173 Display display = Display.getDisplay(this);
174 display.setCurrent(alert, canvas);
177 void handleDecodedText(String text) {
178 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
179 ParsedReaderResultType type = result.getType();
180 if (type.equals(ParsedReaderResultType.URI)) {
181 String uri = ((URIParsedResult) result).getURI();
182 showOpenURL("Open web page?", uri, uri);
183 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
184 String uri = ((BookmarkDoCoMoResult) result).getURI();
185 showOpenURL("Open web page?", uri, uri);
186 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
187 String email = ((EmailDoCoMoResult) result).getTo();
188 showOpenURL("Compose e-mail?", email, "mailto:" + email);
189 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
190 String email = ((EmailAddressResult) result).getEmailAddress();
191 showOpenURL("Compose e-mail?", email, "mailto:" + email);
193 showAlert("Barcode detected", result.getDisplayResult());