Refactored the MonochromeBitmapSource class hierarchy into LuminanceSource, Binarizer...
[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.google.zxing.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.Reader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.client.bug.AWTImageLuminanceSource;
26 import com.google.zxing.client.bug.ImageCanvas;
27 import com.google.zxing.common.GlobalHistogramBinarizer;
28
29 import com.buglabs.bug.module.camera.pub.ICameraDevice;
30 import com.buglabs.bug.module.camera.pub.ICameraModuleControl;
31 import com.buglabs.device.ButtonEvent;
32 import com.buglabs.device.IButtonEventListener;
33 import com.buglabs.device.IButtonEventProvider;
34
35 import java.awt.*;
36 import java.awt.image.ImageObserver;
37 import java.io.IOException;
38
39 /**
40  * @author David Albert
41  */
42 public final class BugBarcodeApp implements IButtonEventListener, ImageObserver {
43
44   private final ICameraDevice camera;
45   private final ICameraModuleControl cameraControl;
46   private final Frame frame;
47   private Image image;
48   private ImageCanvas imageCanvas;
49   private Label barcodeLabel;
50   private boolean pictureTaken;
51   private final Reader reader;
52
53   public BugBarcodeApp(Frame frame,
54       ICameraDevice camera,
55       ICameraModuleControl cameraControl,
56       IButtonEventProvider buttonProvider) {
57     this.frame = frame;
58     this.camera = camera;
59     this.reader = new MultiFormatReader();
60     this.cameraControl = cameraControl;
61     pictureTaken = false;
62     buttonProvider.addListener(this);
63     createUI();
64   }
65
66   private void createUI() {
67     frame.setTitle("BugBarcode");
68     frame.setBackground(Color.WHITE);
69     frame.setLayout(new BorderLayout());
70     barcodeLabel = new Label("Take a picture of a barcode!", Label.CENTER);
71     frame.add(barcodeLabel, BorderLayout.SOUTH);
72     imageCanvas = new ImageCanvas(null);
73     frame.setVisible(true);
74   }
75
76   private void shoot() throws IOException {
77     // get image from camera for use with physical bug
78     cameraControl.setLEDFlash(true);
79     image = Toolkit.getDefaultToolkit().createImage(camera.getImage()).getScaledInstance(400, 300,
80         Image.SCALE_FAST);
81     cameraControl.setLEDFlash(false);
82     if (Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, this)) {
83       drawAndScan();
84     }
85   }
86
87   private void drawAndScan() {
88     imageCanvas.setImage(image.getScaledInstance(216, 150, Image.SCALE_FAST));
89     if (!pictureTaken) {
90       frame.add(imageCanvas, BorderLayout.CENTER);
91       pictureTaken = true;
92       frame.setVisible(true);
93     }
94     imageCanvas.repaint();
95     try {
96       LuminanceSource source = new AWTImageLuminanceSource(image);
97       BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
98       Result result = reader.decode(bitmap);
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 }