Progress on a crude Blackberry client -- still needs much polish but basic functional...
[zxing.git] / rim / src / com / google / zxing / client / rim / ZXingMainScreen.java
diff --git a/rim/src/com/google/zxing/client/rim/ZXingMainScreen.java b/rim/src/com/google/zxing/client/rim/ZXingMainScreen.java
new file mode 100644 (file)
index 0000000..9ded1ed
--- /dev/null
@@ -0,0 +1,119 @@
+/*\r
+ * Copyright 2008 Google Inc.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.google.zxing.client.rim;\r
+\r
+import com.google.zxing.MonochromeBitmapSource;\r
+import com.google.zxing.MultiFormatReader;\r
+import com.google.zxing.Reader;\r
+import com.google.zxing.ReaderException;\r
+import com.google.zxing.Result;\r
+import com.google.zxing.client.j2me.LCDUIImageMonochromeBitmapSource;\r
+import net.rim.blackberry.api.invoke.CameraArguments;\r
+import net.rim.blackberry.api.invoke.Invoke;\r
+import net.rim.device.api.system.Characters;\r
+import net.rim.device.api.ui.UiApplication;\r
+import net.rim.device.api.ui.component.Dialog;\r
+import net.rim.device.api.ui.component.LabelField;\r
+import net.rim.device.api.ui.container.MainScreen;\r
+\r
+import javax.microedition.io.Connector;\r
+import javax.microedition.io.file.FileConnection;\r
+import javax.microedition.lcdui.Image;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+/**\r
+ * @author Sean Owen (srowen@google.com)\r
+ */\r
+final class ZXingMainScreen extends MainScreen {\r
+\r
+  private final ZXingUIApp app;\r
+  private final ImageCapturedJournalListener captureListener;\r
+\r
+  ZXingMainScreen() {\r
+    setTitle("Barcode Reader");\r
+    add(new LabelField("ZXing"));\r
+    app = (ZXingUIApp) UiApplication.getUiApplication();\r
+    captureListener = new ImageCapturedJournalListener(this);\r
+    app.addFileSystemJournalListener(captureListener);\r
+  }\r
+\r
+  public boolean keyChar(char c, int status, int time) {\r
+    if (c == Characters.ENTER) {\r
+      Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());\r
+      return true;\r
+    } else {\r
+      return super.keyChar(c, status, time);\r
+    }\r
+  }\r
+\r
+  public void close() {\r
+    app.removeFileSystemJournalListener(captureListener);\r
+    super.close();\r
+  }\r
+\r
+  private void showMessage(String msg) {\r
+    synchronized (app.getAppEventLock()) {\r
+      Dialog.alert(msg);\r
+    }\r
+  }\r
+\r
+  void handleFile(String path) {\r
+    if (path.endsWith(".jpg") && path.indexOf("IMG") >= 0) {\r
+      app.requestForeground();\r
+      try {\r
+        FileConnection file = null;\r
+        InputStream is = null;\r
+        Image capturedImage;\r
+        try {\r
+          file = (FileConnection) Connector.open("file://" + path);\r
+          is = file.openInputStream();\r
+          capturedImage = Image.createImage(is);\r
+        } finally {\r
+          if (is != null) {\r
+            try {\r
+              is.close();\r
+            } catch (IOException ioe ) {\r
+              // continue\r
+            }\r
+          }\r
+          if (file != null) {\r
+            try {\r
+              file.close();\r
+            } catch (IOException ioe ) {\r
+              // continue\r
+            }\r
+          }\r
+        }\r
+        MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);\r
+        Reader reader = new MultiFormatReader();\r
+        Result result = reader.decode(source);\r
+        try {\r
+          file.delete();\r
+        } catch (IOException ioe) {\r
+          // continue\r
+        }\r
+        showMessage(result.getText());\r
+      } catch (IOException ioe) {\r
+        showMessage(ioe.getMessage());\r
+      } catch (ReaderException re) {\r
+        showMessage("Sorry, no barcode was found.");\r
+      }\r
+    }\r
+  }\r
+\r
+}\r