2 * Copyright 2008 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package com.google.zxing.client.rim;
\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
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
41 * @author Sean Owen (srowen@google.com)
\r
43 final class ZXingMainScreen extends MainScreen {
\r
45 private final ZXingUIApp app;
\r
46 private final ImageCapturedJournalListener captureListener;
\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
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
65 return super.keyChar(c, status, time);
\r
69 public void close() {
\r
70 app.removeFileSystemJournalListener(captureListener);
\r
74 private void showMessage(String msg) {
\r
75 synchronized (app.getAppEventLock()) {
\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
88 FileConnection file = null;
\r
89 InputStream is = null;
\r
90 Image capturedImage;
\r
92 file = (FileConnection) Connector.open("file://" + path);
\r
93 is = file.openInputStream();
\r
94 capturedImage = Image.createImage(is);
\r
99 } catch (IOException ioe) {
\r
103 if (file != null) {
\r
106 } catch (IOException ioe) {
\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
117 } catch (IOException ioe) {
\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