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.MonochromeBitmapSource;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.Reader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
25 import javax.microedition.lcdui.Alert;
26 import javax.microedition.lcdui.AlertType;
27 import javax.microedition.lcdui.Canvas;
28 import javax.microedition.lcdui.Command;
29 import javax.microedition.lcdui.CommandListener;
30 import javax.microedition.lcdui.Display;
31 import javax.microedition.lcdui.Displayable;
32 import javax.microedition.lcdui.Graphics;
33 import javax.microedition.lcdui.Image;
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 * @author Sean Owen (srowen@google.com)
45 public final class ZXingMIDlet extends MIDlet implements CommandListener {
47 private static final Command DECODE = new Command("Decode", Command.SCREEN, 1);
48 private static final Command EXIT = new Command("Exit", Command.EXIT, 1);
50 private Player player;
51 private VideoControl videoControl;
53 protected void startApp() throws MIDletStateChangeException {
55 player = Manager.createPlayer("capture://video");
57 videoControl = (VideoControl) player.getControl("VideoControl");
58 Displayable canvas = new VideoCanvas();
59 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
60 videoControl.setDisplayLocation(0, 0);
61 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
62 videoControl.setVisible(true);
64 FocusControl focusControl = (FocusControl)
65 player.getControl("javax.microedition.amms.control.FocusControl");
66 if (focusControl != null) {
67 if (focusControl.isAutoFocusSupported()) {
68 focusControl.setFocus(FocusControl.AUTO);
70 if (focusControl.isMacroSupported()) {
71 focusControl.setMacro(true);
74 System.out.println("FocusControl not supported");
77 canvas.addCommand(DECODE);
78 canvas.addCommand(EXIT);
79 canvas.setCommandListener(this);
80 Display.getDisplay(this).setCurrent(canvas);
82 } catch (IOException ioe) {
83 throw new MIDletStateChangeException(ioe.toString());
84 } catch (MediaException me) {
85 throw new MIDletStateChangeException(me.toString());
89 public void commandAction(Command command, Displayable displayable) {
90 if (command.equals(DECODE)) {
91 new SnapshotThread().start();
92 } else if (command.equals(EXIT)) {
98 protected void pauseApp() {
102 } catch (MediaException me) {
109 protected void destroyApp(boolean unconditional) {
110 if (player != null) {
117 private void showAlert(String title, String text) {
118 Alert alert = new Alert(title, text, null, AlertType.INFO);
119 alert.setTimeout(Alert.FOREVER);
123 private void showError(Throwable t) {
124 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
127 private void showAlert(Alert alert) {
128 Display display = Display.getDisplay(this);
129 display.setCurrent(alert, display.getCurrent());
132 private static class VideoCanvas extends Canvas {
133 protected void paint(Graphics graphics) {
138 private class SnapshotThread extends Thread {
142 byte[] snapshot = videoControl.getSnapshot(null);
143 Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
144 MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
145 Reader reader = new MultiFormatReader();
146 Result result = reader.decode(source);
147 showAlert("Barcode detected", result.getText());
148 } catch (ReaderException re) {
150 } catch (MediaException me) {
152 } catch (Throwable t) {
157 } catch (MediaException me) {