Should fix up remaining problems in J2ME build and enable build of deployable .jar...
[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
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;
41
42 /**
43  * @author Sean Owen (srowen@google.com)
44  */
45 public final class ZXingMIDlet extends MIDlet implements CommandListener {
46
47   private static final Command DECODE = new Command("Decode", Command.SCREEN, 1);
48   private static final Command EXIT = new Command("Exit", Command.EXIT, 1);
49
50   private Player player;
51   private VideoControl videoControl;
52
53   protected void startApp() throws MIDletStateChangeException {
54     try {
55       player = Manager.createPlayer("capture://video");
56       player.realize();
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);
63       /*
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);
69         }
70         if (focusControl.isMacroSupported()) {
71           focusControl.setMacro(true);
72         }
73       } else {
74         System.out.println("FocusControl not supported");
75       }
76        */
77       canvas.addCommand(DECODE);
78       canvas.addCommand(EXIT);
79       canvas.setCommandListener(this);
80       Display.getDisplay(this).setCurrent(canvas);
81       player.start();
82     } catch (IOException ioe) {
83       throw new MIDletStateChangeException(ioe.toString());
84     } catch (MediaException me) {
85       throw new MIDletStateChangeException(me.toString());
86     }
87   }
88
89   public void commandAction(Command command, Displayable displayable) {
90     if (command.equals(DECODE)) {
91       new SnapshotThread().start();
92     } else if (command.equals(EXIT)) {
93       destroyApp(false);
94       notifyDestroyed();
95     }
96   }
97
98   protected void pauseApp() {
99     if (player != null) {
100       try {
101         player.stop();
102       } catch (MediaException me) {
103         // continue?
104         showError(me);        
105       }
106     }
107   }
108
109   protected void destroyApp(boolean unconditional) {
110     if (player != null) {
111       player.close();
112       player = null;
113       videoControl = null;
114     }
115   }
116
117   private void showAlert(String title, String text) {
118     Alert alert = new Alert(title, text, null, AlertType.INFO);
119     alert.setTimeout(Alert.FOREVER);
120     showAlert(alert);
121   }
122
123   private void showError(Throwable t) {
124     showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
125   }
126
127   private void showAlert(Alert alert) {
128     Display display = Display.getDisplay(this);
129     display.setCurrent(alert, display.getCurrent());
130   }
131
132   private static class VideoCanvas extends Canvas {
133     protected void paint(Graphics graphics) {
134       // do nothing
135     }
136   }
137
138   private class SnapshotThread extends Thread {
139     public void run() {
140       try {
141         player.stop();
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) {
149         showError(re);
150       } catch (MediaException me) {
151         showError(me);
152       } catch (Throwable t) {
153         showError(t);
154       } finally {
155         try {
156           player.start();
157         } catch (MediaException me) {
158           // continue?
159           showError(me);
160         }
161       }
162
163     }
164   }
165
166 }