git-svn-id: http://zxing.googlecode.com/svn/trunk@6 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.j2me;
18
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;
25
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;
42
43 /**
44  * @author Sean Owen (srowen@google.com)
45  */
46 public final class ZXingMIDlet extends MIDlet implements CommandListener {
47
48   private static final Command DECODE = new Command("Decode", Command.SCREEN, 1);
49   private static final Command EXIT = new Command("Exit", Command.EXIT, 1);
50
51   static {
52     GridSampler.setGridSamplerClassName("com.google.zxing.client.j2me.JAIGridSampler");
53   }
54
55   private Player player;
56   private VideoControl videoControl;
57
58   protected void startApp() throws MIDletStateChangeException {
59     try {
60       player = Manager.createPlayer("capture://video");
61       player.realize();
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);
68       /*
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");
74       } else {
75         System.out.println("ImageFormatControl not supported");
76       }
77
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);
83         }
84         if (focusControl.isMacroSupported()) {
85           focusControl.setMacro(true);
86         }
87       } else {
88         System.out.println("FocusControl not supported");
89       }
90        */
91       canvas.addCommand(DECODE);
92       canvas.addCommand(EXIT);
93       canvas.setCommandListener(this);
94       Display.getDisplay(this).setCurrent(canvas);
95       player.start();
96     } catch (IOException ioe) {
97       throw new MIDletStateChangeException(ioe.toString());
98     } catch (MediaException me) {
99       throw new MIDletStateChangeException(me.toString());
100     }
101   }
102
103   public void commandAction(Command command, Displayable displayable) {
104     if (command.equals(DECODE)) {
105       new SnapshotThread().start();
106     } else if (command.equals(EXIT)) {
107       destroyApp(false);
108       notifyDestroyed();
109     }
110   }
111
112   protected void pauseApp() {
113     if (player != null) {
114       try {
115         player.stop();
116       } catch (MediaException me) {
117         // continue?
118         showError(me);        
119       }
120     }
121   }
122
123   protected void destroyApp(boolean unconditional) {
124     if (player != null) {
125       player.close();
126       player = null;
127       videoControl = null;
128     }
129   }
130
131   private void showAlert(String title, String text) {
132     Alert alert = new Alert(title, text, null, AlertType.INFO);
133     alert.setTimeout(Alert.FOREVER);
134     showAlert(alert);
135   }
136
137   private void showError(Throwable t) {
138     showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
139   }
140
141   private void showAlert(Alert alert) {
142     Display display = Display.getDisplay(this);
143     display.setCurrent(alert, display.getCurrent());
144   }
145
146   private static class VideoCanvas extends Canvas {
147     protected void paint(Graphics graphics) {
148       // do nothing
149     }
150   }
151
152   private class SnapshotThread extends Thread {
153     public void run() {
154       try {
155         player.stop();
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) {
163         showError(re);
164       } catch (MediaException me) {
165         showError(me);
166       } catch (Throwable t) {
167         showError(t);
168       } finally {
169         try {
170           player.start();
171         } catch (MediaException me) {
172           // continue?
173           showError(me);
174         }
175       }
176
177     }
178   }
179
180 }