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 javax.microedition.io.ConnectionNotFoundException;
20 import javax.microedition.lcdui.Alert;
21 import javax.microedition.lcdui.AlertType;
22 import javax.microedition.lcdui.Canvas;
23 import javax.microedition.lcdui.Command;
24 import javax.microedition.lcdui.CommandListener;
25 import javax.microedition.lcdui.Display;
26 import javax.microedition.lcdui.Displayable;
27 import javax.microedition.media.Manager;
28 import javax.microedition.media.MediaException;
29 import javax.microedition.media.Player;
30 import javax.microedition.media.control.VideoControl;
31 import javax.microedition.midlet.MIDlet;
32 import javax.microedition.midlet.MIDletStateChangeException;
33 import javax.microedition.amms.control.camera.ZoomControl;
34 import java.io.IOException;
37 * <p>The actual reader application {@link MIDlet}.</p>
39 * @author Sean Owen (srowen@google.com)
41 public final class ZXingMIDlet extends MIDlet {
43 private static final int NO_ZOOM = 100;
44 private static final int MAX_ZOOM = 250;
46 private Canvas canvas;
47 private Player player;
48 private VideoControl videoControl;
54 VideoControl getVideoControl() {
58 protected void startApp() throws MIDletStateChangeException {
60 player = Manager.createPlayer("capture://video");
63 videoControl = (VideoControl) player.getControl("VideoControl");
64 canvas = new VideoCanvas(this);
65 canvas.setFullScreenMode(true);
66 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
67 videoControl.setDisplayLocation(0, 0);
68 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
69 videoControl.setVisible(true);
71 Display.getDisplay(this).setCurrent(canvas);
72 } catch (IOException ioe) {
73 throw new MIDletStateChangeException(ioe.toString());
74 } catch (MediaException me) {
75 throw new MIDletStateChangeException(me.toString());
79 private static void setZoom(Player player) {
80 ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
81 if (zoomControl != null) {
82 // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
83 // This is a crude way of dealing with the fact that many phone cameras will not focus at a
85 int maxZoom = zoomControl.getMaxOpticalZoom();
86 if (maxZoom > NO_ZOOM) {
87 zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
89 int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
90 if (maxDigitalZoom > NO_ZOOM) {
91 zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
97 protected void pauseApp() {
101 } catch (MediaException me) {
108 protected void destroyApp(boolean unconditional) {
109 if (player != null) {
113 } catch (MediaException me) {
127 // Convenience methods to show dialogs
129 void showYesNo(String title, final String text) {
130 Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);
131 alert.setTimeout(Alert.FOREVER);
132 final Command cancel = new Command("Cancel", Command.CANCEL, 1);
133 alert.addCommand(cancel);
134 CommandListener listener = new CommandListener() {
135 public void commandAction(Command command, Displayable displayable) {
136 if (command.getCommandType() == Command.OK) {
138 platformRequest(text);
139 } catch (ConnectionNotFoundException cnfe) {
146 Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
150 alert.setCommandListener(listener);
154 void showAlert(String title, String text) {
155 Alert alert = new Alert(title, text, null, AlertType.INFO);
156 alert.setTimeout(Alert.FOREVER);
160 void showError(Throwable t) {
161 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
164 private void showAlert(Alert alert) {
165 Display display = Display.getDisplay(this);
166 display.setCurrent(alert, canvas);
169 void handleDecodedText(String text) {
170 // This is a crude imitation of the code found in module core-ext, which handles the contents
171 // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
172 // on URL parsing routines in java.net. This should be somehow worked around: TODO
173 // For now, detect URLs in a simple way, and treat everything else as text
174 if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
175 showYesNo("Open web page?", text);
177 showAlert("Barcode detected", text);
181 private static boolean maybeURLWithoutScheme(String text) {
182 return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;