0d5a567a4b2690ef2d8247a3be365cbe889c10b0
[zxing.git] / rim / src / com / google / zxing / client / rim / ZXingMainScreen.java
1 /*\r
2  * Copyright 2008 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.client.rim;\r
18 \r
19 import com.google.zxing.MonochromeBitmapSource;\r
20 import com.google.zxing.MultiFormatReader;\r
21 import com.google.zxing.Reader;\r
22 import com.google.zxing.ReaderException;\r
23 import com.google.zxing.Result;\r
24 import com.google.zxing.client.j2me.LCDUIImageMonochromeBitmapSource;\r
25 import net.rim.blackberry.api.invoke.CameraArguments;\r
26 import net.rim.blackberry.api.invoke.Invoke;\r
27 import net.rim.device.api.system.Characters;\r
28 import net.rim.device.api.system.EventInjector;\r
29 import net.rim.device.api.ui.UiApplication;\r
30 import net.rim.device.api.ui.component.Dialog;\r
31 import net.rim.device.api.ui.component.LabelField;\r
32 import net.rim.device.api.ui.container.MainScreen;\r
33 \r
34 import javax.microedition.io.Connector;\r
35 import javax.microedition.io.file.FileConnection;\r
36 import javax.microedition.lcdui.Image;\r
37 import java.io.IOException;\r
38 import java.io.InputStream;\r
39 \r
40 /**\r
41  * @author Sean Owen (srowen@google.com)\r
42  */\r
43 final class ZXingMainScreen extends MainScreen {\r
44 \r
45   private final ZXingUIApp app;\r
46   private final ImageCapturedJournalListener captureListener;\r
47 \r
48   ZXingMainScreen() {\r
49     setTitle("ZXing Barcode Reader");\r
50     add(new LabelField("UNDER CONSTRUCTION"));\r
51     add(new LabelField("1. Press 'Enter' at right to launch the Camera application"));\r
52     add(new LabelField("2. Configure Camera to capture 640x480 image"));\r
53     add(new LabelField("3. Take a picture of a barcode"));\r
54     add(new LabelField("4. If not returned to this application to see result, close Camera application"));\r
55     app = (ZXingUIApp) UiApplication.getUiApplication();\r
56     captureListener = new ImageCapturedJournalListener(this);\r
57     app.addFileSystemJournalListener(captureListener);\r
58   }\r
59 \r
60   public boolean keyChar(char c, int status, int time) {\r
61     if (c == Characters.ENTER) {\r
62       Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());\r
63       return true;\r
64     } else {\r
65       return super.keyChar(c, status, time);\r
66     }\r
67   }\r
68 \r
69   public void close() {\r
70     app.removeFileSystemJournalListener(captureListener);\r
71     super.close();\r
72   }\r
73 \r
74   private void showMessage(String msg) {\r
75     synchronized (app.getAppEventLock()) {\r
76       Dialog.alert(msg);\r
77     }\r
78   }\r
79 \r
80   void handleFile(String path) {\r
81     if (path.endsWith(".jpg") && path.indexOf("IMG") >= 0) {\r
82       // Get out of camera app\r
83       EventInjector.invokeEvent(new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0, 1));\r
84       EventInjector.invokeEvent(new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_UP, Characters.ESCAPE, 0, 1));\r
85       // Try to come to foreground for good measure\r
86       app.requestForeground();\r
87       try {\r
88         FileConnection file = null;\r
89         InputStream is = null;\r
90         Image capturedImage;\r
91         try {\r
92           file = (FileConnection) Connector.open("file://" + path);\r
93           is = file.openInputStream();\r
94           capturedImage = Image.createImage(is);\r
95         } finally {\r
96           if (is != null) {\r
97             try {\r
98               is.close();\r
99             } catch (IOException ioe) {\r
100               // continue\r
101             }\r
102           }\r
103           if (file != null) {\r
104             try {\r
105               file.close();\r
106             } catch (IOException ioe) {\r
107               // continue\r
108             }\r
109           }\r
110         }\r
111         MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);\r
112         Reader reader = new MultiFormatReader();\r
113         Result result = reader.decode(source);\r
114         // If decode was successful...\r
115         try {\r
116           file.delete();\r
117         } catch (IOException ioe) {\r
118           // continue\r
119         }\r
120         showMessage(result.getText());\r
121       } catch (IOException ioe) {\r
122         showMessage(ioe.getMessage());\r
123       } catch (ReaderException re) {\r
124         showMessage("Sorry, no barcode was found.");\r
125       }\r
126     }\r
127   }\r
128 \r
129 }\r