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;
24 import com.google.zxing.qrcode.detector.GridSampler;
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.lcdui.Graphics;
34 import javax.microedition.lcdui.Image;
35 import javax.microedition.media.Manager;
36 import javax.microedition.media.MediaException;
37 import javax.microedition.media.Player;
38 import javax.microedition.media.control.VideoControl;
39 import javax.microedition.midlet.MIDlet;
40 import javax.microedition.midlet.MIDletStateChangeException;
41 import java.io.IOException;
44 * @author Sean Owen (srowen@google.com)
46 public final class ZXingMIDlet extends MIDlet implements CommandListener {
48 private static final Command DECODE = new Command("Decode", Command.SCREEN, 1);
49 private static final Command EXIT = new Command("Exit", Command.EXIT, 1);
52 GridSampler.setGridSamplerClassName("com.google.zxing.client.j2me.JAIGridSampler");
55 private Player player;
56 private VideoControl videoControl;
58 protected void startApp() throws MIDletStateChangeException {
60 player = Manager.createPlayer("capture://video");
62 videoControl = (VideoControl) player.getControl("VideoControl");
63 Displayable canvas = new VideoCanvas();
64 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
65 videoControl.setDisplayLocation(0, 0);
66 videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
67 videoControl.setVisible(true);
69 FormatControl imageFormatControl = (FormatControl)
70 player.getControl("javax.microedition.amms.control.ImageFormatControl");
71 if (imageFormatControl != null) {
72 imageFormatControl.setFormat("image/png");
73 imageFormatControl.setParameter(FormatControl.PARAM_VERSION_TYPE, "PNG");
75 System.out.println("ImageFormatControl not supported");
78 FocusControl focusControl = (FocusControl)
79 player.getControl("javax.microedition.amms.control.FocusControl");
80 if (focusControl != null) {
81 if (focusControl.isAutoFocusSupported()) {
82 focusControl.setFocus(FocusControl.AUTO);
84 if (focusControl.isMacroSupported()) {
85 focusControl.setMacro(true);
88 System.out.println("FocusControl not supported");
91 canvas.addCommand(DECODE);
92 canvas.addCommand(EXIT);
93 canvas.setCommandListener(this);
94 Display.getDisplay(this).setCurrent(canvas);
96 } catch (IOException ioe) {
97 throw new MIDletStateChangeException(ioe.toString());
98 } catch (MediaException me) {
99 throw new MIDletStateChangeException(me.toString());
103 public void commandAction(Command command, Displayable displayable) {
104 if (command.equals(DECODE)) {
105 new SnapshotThread().start();
106 } else if (command.equals(EXIT)) {
112 protected void pauseApp() {
113 if (player != null) {
116 } catch (MediaException me) {
123 protected void destroyApp(boolean unconditional) {
124 if (player != null) {
131 private void showAlert(String title, String text) {
132 Alert alert = new Alert(title, text, null, AlertType.INFO);
133 alert.setTimeout(Alert.FOREVER);
137 private void showError(Throwable t) {
138 showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
141 private void showAlert(Alert alert) {
142 Display display = Display.getDisplay(this);
143 display.setCurrent(alert, display.getCurrent());
146 private static class VideoCanvas extends Canvas {
147 protected void paint(Graphics graphics) {
152 private class SnapshotThread extends Thread {
156 byte[] snapshot = videoControl.getSnapshot(null);
157 Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
158 MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
159 Reader reader = new MultiFormatReader();
160 Result result = reader.decode(source);
161 showAlert("Barcode detected", result.getText());
162 } catch (ReaderException re) {
164 } catch (MediaException me) {
166 } catch (Throwable t) {
171 } catch (MediaException me) {