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.EmailDoCoMoResult;
21 import com.google.zxing.client.result.ParsedReaderResult;
22 import com.google.zxing.client.result.ParsedReaderResultType;
23 import com.google.zxing.client.result.URIParsedResult;
25 import javax.microedition.io.ConnectionNotFoundException;
26 import javax.microedition.lcdui.Alert;
27 import javax.microedition.lcdui.AlertType;
28 import javax.microedition.lcdui.Canvas;
29 import javax.microedition.lcdui.Command;
30 import javax.microedition.lcdui.CommandListener;
31 import javax.microedition.lcdui.Display;
32 import javax.microedition.lcdui.Displayable;
33 import javax.microedition.media.Manager;
34 import javax.microedition.media.MediaException;
35 import javax.microedition.media.Player;
36 import javax.microedition.media.control.VideoControl;
37 import javax.microedition.midlet.MIDlet;
38 import javax.microedition.midlet.MIDletStateChangeException;
39 import java.io.IOException;
42 * <p>The actual reader application {@link MIDlet}.</p>
44 * @author Sean Owen (srowen@google.com)
46 public final class ZXingMIDlet extends MIDlet {
48 private Canvas canvas;
49 private Player player;
50 private VideoControl videoControl;
56 VideoControl getVideoControl() {
60 protected void startApp() throws MIDletStateChangeException {
62 player = Manager.createPlayer("capture://video");
64 AdvancedMultimediaManager.setZoom(player);
65 videoControl = (VideoControl) player.getControl("VideoControl");
66 canvas = new VideoCanvas(this);
67 canvas.setFullScreenMode(true);
68 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
69 videoControl.setDisplayLocation(0, 0);
70 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
71 videoControl.setVisible(true);
73 Display.getDisplay(this).setCurrent(canvas);
74 } catch (IOException ioe) {
75 throw new MIDletStateChangeException(ioe.toString());
76 } catch (MediaException me) {
77 throw new MIDletStateChangeException(me.toString());
81 protected void pauseApp() {
85 } catch (MediaException me) {
92 protected void destroyApp(boolean unconditional) {
97 } catch (MediaException me) {
111 // Convenience methods to show dialogs
113 private void showOpenURL(final String text) {
114 Alert alert = new Alert("Open web page?", text, null, AlertType.CONFIRMATION);
115 alert.setTimeout(Alert.FOREVER);
116 final Command cancel = new Command("Cancel", Command.CANCEL, 1);
117 alert.addCommand(cancel);
118 CommandListener listener = new CommandListener() {
119 public void commandAction(Command command, Displayable displayable) {
120 if (command.getCommandType() == Command.OK) {
122 platformRequest(text);
123 } catch (ConnectionNotFoundException cnfe) {
130 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
134 alert.setCommandListener(listener);
138 private void showAlert(String title, String text) {
139 Alert alert = new Alert(title, text, null, AlertType.INFO);
140 alert.setTimeout(Alert.FOREVER);
144 void showError(Throwable t) {
145 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
148 private void showAlert(Alert alert) {
149 Display display = Display.getDisplay(this);
150 display.setCurrent(alert, canvas);
153 void handleDecodedText(String text) {
154 ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
155 ParsedReaderResultType type = result.getType();
156 if (type.equals(ParsedReaderResultType.URI)) {
157 showOpenURL(((URIParsedResult) result).getURI());
158 } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
159 showOpenURL(((BookmarkDoCoMoResult) result).getURI());
160 } else if (type.equals(ParsedReaderResultType.EMAIL)) {
161 showOpenURL(((EmailDoCoMoResult) result).getMailtoURI());
163 showAlert("Barcode detected", result.getDisplayResult());