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 = Manager.createPlayer("capture://video");
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 protected void pauseApp() {
86 } catch (MediaException me) {
93 protected void destroyApp(boolean unconditional) {
98 } catch (MediaException me) {
112 // Convenience methods to show dialogs
114 private void showOpenURL(String title, final String display, final String uri) {
115 Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
116 alert.setTimeout(Alert.FOREVER);
117 final Command cancel = new Command("Cancel", Command.CANCEL, 1);
118 alert.addCommand(cancel);
119 CommandListener listener = new CommandListener() {
120 public void commandAction(Command command, Displayable displayable) {
121 if (command.getCommandType() == Command.OK) {
123 platformRequest(uri);
124 } catch (ConnectionNotFoundException cnfe) {
131 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
135 alert.setCommandListener(listener);
139 private void showAlert(String title, String text) {
140 Alert alert = new Alert(title, text, null, AlertType.INFO);
141 alert.setTimeout(Alert.FOREVER);
145 void showError(Throwable t) {
146 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
149 private void showAlert(Alert alert) {
150 Display display = Display.getDisplay(this);
151 display.setCurrent(alert, canvas);
154 void handleDecodedText(String text) {
155 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
156 ParsedReaderResultType type = result.getType();
157 if (type.equals(ParsedReaderResultType.URI)) {
158 String uri = ((URIParsedResult) result).getURI();
159 showOpenURL("Open web page?", uri, uri);
160 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
161 String uri = ((BookmarkDoCoMoResult) result).getURI();
162 showOpenURL("Open web page?", uri, uri);
163 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
164 String email = ((EmailDoCoMoResult) result).getTo();
165 showOpenURL("Compose e-mail?", email, "mailto:" + email);
166 } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
167 String email = ((EmailAddressResult) result).getEmailAddress();
168 showOpenURL("Compose e-mail?", email, "mailto:" + email);
170 showAlert("Barcode detected", result.getDisplayResult());