9ded1ed69dfe1041723f79df39f68fc0e6b6dea1
[zxing.git] / rim / src / com / google / zxing / client / rim / ZXingMainScreen.java
1 /*\r
2  * Copyright 2008 Google Inc.\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.ui.UiApplication;\r
29 import net.rim.device.api.ui.component.Dialog;\r
30 import net.rim.device.api.ui.component.LabelField;\r
31 import net.rim.device.api.ui.container.MainScreen;\r
32 \r
33 import javax.microedition.io.Connector;\r
34 import javax.microedition.io.file.FileConnection;\r
35 import javax.microedition.lcdui.Image;\r
36 import java.io.IOException;\r
37 import java.io.InputStream;\r
38 \r
39 /**\r
40  * @author Sean Owen (srowen@google.com)\r
41  */\r
42 final class ZXingMainScreen extends MainScreen {\r
43 \r
44   private final ZXingUIApp app;\r
45   private final ImageCapturedJournalListener captureListener;\r
46 \r
47   ZXingMainScreen() {\r
48     setTitle("Barcode Reader");\r
49     add(new LabelField("ZXing"));\r
50     app = (ZXingUIApp) UiApplication.getUiApplication();\r
51     captureListener = new ImageCapturedJournalListener(this);\r
52     app.addFileSystemJournalListener(captureListener);\r
53   }\r
54 \r
55   public boolean keyChar(char c, int status, int time) {\r
56     if (c == Characters.ENTER) {\r
57       Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());\r
58       return true;\r
59     } else {\r
60       return super.keyChar(c, status, time);\r
61     }\r
62   }\r
63 \r
64   public void close() {\r
65     app.removeFileSystemJournalListener(captureListener);\r
66     super.close();\r
67   }\r
68 \r
69   private void showMessage(String msg) {\r
70     synchronized (app.getAppEventLock()) {\r
71       Dialog.alert(msg);\r
72     }\r
73   }\r
74 \r
75   void handleFile(String path) {\r
76     if (path.endsWith(".jpg") && path.indexOf("IMG") >= 0) {\r
77       app.requestForeground();\r
78       try {\r
79         FileConnection file = null;\r
80         InputStream is = null;\r
81         Image capturedImage;\r
82         try {\r
83           file = (FileConnection) Connector.open("file://" + path);\r
84           is = file.openInputStream();\r
85           capturedImage = Image.createImage(is);\r
86         } finally {\r
87           if (is != null) {\r
88             try {\r
89               is.close();\r
90             } catch (IOException ioe ) {\r
91               // continue\r
92             }\r
93           }\r
94           if (file != null) {\r
95             try {\r
96               file.close();\r
97             } catch (IOException ioe ) {\r
98               // continue\r
99             }\r
100           }\r
101         }\r
102         MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);\r
103         Reader reader = new MultiFormatReader();\r
104         Result result = reader.decode(source);\r
105         try {\r
106           file.delete();\r
107         } catch (IOException ioe) {\r
108           // continue\r
109         }\r
110         showMessage(result.getText());\r
111       } catch (IOException ioe) {\r
112         showMessage(ioe.getMessage());\r
113       } catch (ReaderException re) {\r
114         showMessage("Sorry, no barcode was found.");\r
115       }\r
116     }\r
117   }\r
118 \r
119 }\r