01d6d05a65e89c8799d6fbe4e8b6de791ffc8e73
[zxing.git] / bug / src / com / google / zxing / client / bug / app / BugBarcodeApp.java
1 /*
2  * Copyright 2008 ZXing authors
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.bug.app;
18
19 import com.buglabs.bug.module.camera.pub.ICameraDevice;
20 import com.buglabs.bug.module.camera.pub.ICameraModuleControl;
21 import com.buglabs.device.ButtonEvent;
22 import com.buglabs.device.IButtonEventListener;
23 import com.buglabs.device.IButtonEventProvider;
24 import com.google.zxing.MonochromeBitmapSource;
25 import com.google.zxing.MultiFormatReader;
26 import com.google.zxing.Reader;
27 import com.google.zxing.ReaderException;
28 import com.google.zxing.Result;
29 import com.google.zxing.client.bug.AWTImageMonochromeBitmapSource;
30 import com.google.zxing.client.bug.ImageCanvas;
31
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Frame;
35 import java.awt.Image;
36 import java.awt.Label;
37 import java.awt.Toolkit;
38 import java.awt.image.ImageObserver;
39 import java.io.IOException;
40
41 /**
42  * @author David Albert
43  */
44 public final class BugBarcodeApp implements IButtonEventListener, ImageObserver {
45
46         private final ICameraDevice camera;
47   private final ICameraModuleControl cameraControl;
48         private final Frame frame;
49         private Image image;
50         private ImageCanvas imageCanvas;
51         private Label barcodeLabel;
52         private boolean pictureTaken;
53         private final Reader reader;
54
55   public BugBarcodeApp(Frame frame,
56                        ICameraDevice camera,
57                        ICameraModuleControl cameraControl,
58                        IButtonEventProvider buttonProvider) {
59                 this.frame = frame;
60                 this.camera = camera;
61                 this.reader = new MultiFormatReader();
62                 this.cameraControl = cameraControl;
63                 pictureTaken = false;
64                 buttonProvider.addListener(this);
65                 createUI();
66         }
67         
68         private void createUI() {
69                 frame.setTitle("BugBarcode");
70                 frame.setBackground(Color.WHITE);
71                 frame.setLayout(new BorderLayout());
72                 barcodeLabel = new Label("Take a picture of a barcode!", Label.CENTER);
73                 frame.add(barcodeLabel, BorderLayout.SOUTH);
74                 imageCanvas = new ImageCanvas(null);
75                 frame.setVisible(true);
76         }
77         
78         private void shoot() throws IOException {
79                 // get image from camera for use with physical bug
80           cameraControl.setLEDFlash(true);
81                 image = Toolkit.getDefaultToolkit().createImage(camera.getImage()).getScaledInstance(400, 300, Image.SCALE_FAST);
82           cameraControl.setLEDFlash(false);
83                 if (Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, this)) {
84                         drawAndScan();
85                 }
86         }
87         
88         private void drawAndScan() {
89                 imageCanvas.setImage(image.getScaledInstance(216, 150, Image.SCALE_FAST));
90                 if (!pictureTaken) {
91                         frame.add(imageCanvas, BorderLayout.CENTER);
92                         pictureTaken = true;
93                         frame.setVisible(true);
94                 }
95                 imageCanvas.repaint();
96                 try {
97       MonochromeBitmapSource source = new AWTImageMonochromeBitmapSource(image);
98       Result result = reader.decode(source);
99                         barcodeLabel.setText(result.getText());
100                 } catch (ReaderException re) {
101                         barcodeLabel.setText("I can't find a barcode here");
102                 }
103         }
104         
105         public void buttonEvent(ButtonEvent event) {
106                 if (event.getButton() == ButtonEvent.BUTTON_HOTKEY_1 && event.getAction() == 0) {
107       try {
108         shoot();
109       } catch (IOException ioe) {
110         // continue
111       }
112     }
113         }
114
115         public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
116                 if ((infoflags & ALLBITS) != 0) {
117       drawAndScan();
118       return false;
119                 }
120                 return true;
121         }
122 }