Add sharpening filter to this implementation too
[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.Result;
20 import com.google.zxing.client.result.BookmarkDoCoMoParsedResult;
21 import com.google.zxing.client.result.EmailAddressParsedResult;
22 import com.google.zxing.client.result.EmailDoCoMoParsedResult;
23 import com.google.zxing.client.result.ParsedReaderResult;
24 import com.google.zxing.client.result.ParsedReaderResultType;
25 import com.google.zxing.client.result.SMSParsedResult;
26 import com.google.zxing.client.result.SMSTOParsedResult;
27 import com.google.zxing.client.result.TelParsedResult;
28 import com.google.zxing.client.result.UPCParsedResult;
29 import com.google.zxing.client.result.URIParsedResult;
30 import com.google.zxing.client.result.URLTOParsedResult;
31
32 import javax.microedition.io.ConnectionNotFoundException;
33 import javax.microedition.lcdui.Alert;
34 import javax.microedition.lcdui.AlertType;
35 import javax.microedition.lcdui.Canvas;
36 import javax.microedition.lcdui.Command;
37 import javax.microedition.lcdui.CommandListener;
38 import javax.microedition.lcdui.Display;
39 import javax.microedition.lcdui.Displayable;
40 import javax.microedition.media.Manager;
41 import javax.microedition.media.MediaException;
42 import javax.microedition.media.Player;
43 import javax.microedition.media.control.VideoControl;
44 import javax.microedition.midlet.MIDlet;
45 import javax.microedition.midlet.MIDletStateChangeException;
46 import java.io.IOException;
47
48 /**
49  * <p>The actual reader application {@link MIDlet}.</p>
50  *
51  * @author Sean Owen (srowen@google.com)
52  */
53 public final class ZXingMIDlet extends MIDlet {
54
55   private Canvas canvas;
56   private Player player;
57   private VideoControl videoControl;
58
59   Displayable getCanvas() {
60     return canvas;
61   }
62
63   Player getPlayer() {
64     return player;
65   }
66
67   VideoControl getVideoControl() {
68     return videoControl;
69   }
70
71   protected void startApp() throws MIDletStateChangeException {
72     try {
73       player = createPlayer();
74       player.realize();
75       AdvancedMultimediaManager.setZoom(player);
76       AdvancedMultimediaManager.setExposure(player);
77       videoControl = (VideoControl) player.getControl("VideoControl");
78       canvas = new VideoCanvas(this);
79       canvas.setFullScreenMode(true);
80       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
81       videoControl.setDisplayLocation(0, 0);
82       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
83       videoControl.setVisible(true);
84       player.start();
85       Display.getDisplay(this).setCurrent(canvas);
86     } catch (IOException ioe) {
87       throw new MIDletStateChangeException(ioe.toString());
88     } catch (MediaException me) {
89       throw new MIDletStateChangeException(me.toString());
90     }
91   }
92
93   private static Player createPlayer() throws IOException, MediaException {
94     // Try a workaround for Nokias, which want to use capture://image in some cases
95     Player player = null;
96     String platform = System.getProperty("microedition.platform");
97     if (platform != null && platform.indexOf("Nokia") >= 0) {
98       try {
99         player = Manager.createPlayer("capture://image");
100       } catch (MediaException me) {
101         // if this fails, just continue with capture://video
102       } catch (Error e) {
103         // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
104         // We should still try to continue
105       }
106     }
107     if (player == null) {
108       player = Manager.createPlayer("capture://video");
109     }
110     return player;
111   }
112
113   protected void pauseApp() {
114     if (player != null) {
115       try {
116         player.stop();
117       } catch (MediaException me) {
118         // continue?
119         showError(me);
120       }
121     }
122   }
123
124   protected void destroyApp(boolean unconditional) {
125     if (player != null) {
126       videoControl = null;
127       try {
128         player.stop();
129       } catch (MediaException me) {
130         // continue
131       }
132       player.deallocate();
133       player.close();
134       player = null;
135     }
136   }
137
138   void stop() {
139     destroyApp(false);
140     notifyDestroyed();
141   }
142
143   // Convenience methods to show dialogs
144
145   private void showOpenURL(String title, String display, final String uri) {
146     Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
147     alert.setTimeout(Alert.FOREVER);
148     Command yes = new Command("Yes", Command.OK, 1);
149     alert.addCommand(yes);
150     Command no = new Command("No", Command.CANCEL, 1);
151     alert.addCommand(no);
152     CommandListener listener = new CommandListener() {
153       public void commandAction(Command command, Displayable displayable) {
154         if (command.getCommandType() == Command.OK) {
155           try {
156             platformRequest(uri);
157           } catch (ConnectionNotFoundException cnfe) {
158             showError(cnfe);
159           } finally {
160             stop();
161           }
162         } else {
163           // cancel
164           Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
165         }
166       }
167     };
168     alert.setCommandListener(listener);
169     showAlert(alert);
170   }
171
172   private void showAlert(String title, String text) {
173     Alert alert = new Alert(title, text, null, AlertType.INFO);
174     alert.setTimeout(Alert.FOREVER);
175     showAlert(alert);
176   }
177
178   void showError(Throwable t) {
179     String message = t.getMessage();
180     if (message != null && message.length() > 0) {
181       showError(message);
182     } else {
183       showError(t.toString());
184     }
185   }
186
187   void showError(String message) {
188     showAlert(new Alert("Error", message, null, AlertType.ERROR));
189   }
190
191   private void showAlert(Alert alert) {
192     Display display = Display.getDisplay(this);
193     display.setCurrent(alert, canvas);
194   }
195
196   void handleDecodedText(Result theResult) {
197     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
198     ParsedReaderResultType type = result.getType();
199     if (type.equals(ParsedReaderResultType.URI)) {
200       String uri = ((URIParsedResult) result).getURI();
201       showOpenURL("Open Web Page?", uri, uri);
202     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
203       String uri = ((BookmarkDoCoMoParsedResult) result).getURI();
204       showOpenURL("Open Web Page?", uri, uri);      
205     } else if (type.equals(ParsedReaderResultType.URLTO)) {
206       String uri = ((URLTOParsedResult) result).getURI();
207       showOpenURL("Open Web Page?", uri, uri);
208     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
209       EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
210       showOpenURL("Compose E-mail?", emailResult.getTo(), emailResult.getMailtoURI());
211     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
212       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
213       showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
214     } else if (type.equals(ParsedReaderResultType.SMS)) {
215       SMSParsedResult smsResult = (SMSParsedResult) result;
216       showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
217     } else if (type.equals(ParsedReaderResultType.SMSTO)) {
218       SMSTOParsedResult smsToResult = (SMSTOParsedResult) result;
219       showOpenURL("Compose SMS?", smsToResult.getNumber(), smsToResult.getSMSURI());
220     } else if (type.equals(ParsedReaderResultType.UPC)) {
221       String upc = ((UPCParsedResult) result).getUPC();
222       String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
223       showOpenURL("Look Up Barcode Online?", upc, uri);
224     } else if (type.equals(ParsedReaderResultType.TEL)) {
225       TelParsedResult telResult = (TelParsedResult) result;
226       showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
227     } else {
228       showAlert("Barcode Detected", result.getDisplayResult());
229     }
230   }
231
232 }