2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.bug.app;
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;
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;
42 * @author David Albert
44 public final class BugBarcodeApp implements IButtonEventListener, ImageObserver {
46 private final ICameraDevice camera;
47 private final ICameraModuleControl cameraControl;
48 private final Frame frame;
50 private ImageCanvas imageCanvas;
51 private Label barcodeLabel;
52 private boolean pictureTaken;
53 private final Reader reader;
55 public BugBarcodeApp(Frame frame,
57 ICameraModuleControl cameraControl,
58 IButtonEventProvider buttonProvider) {
61 this.reader = new MultiFormatReader();
62 this.cameraControl = cameraControl;
64 buttonProvider.addListener(this);
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);
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)) {
88 private void drawAndScan() {
89 imageCanvas.setImage(image.getScaledInstance(216, 150, Image.SCALE_FAST));
91 frame.add(imageCanvas, BorderLayout.CENTER);
93 frame.setVisible(true);
95 imageCanvas.repaint();
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");
105 public void buttonEvent(ButtonEvent event) {
106 if (event.getButton() == ButtonEvent.BUTTON_HOTKEY_1 && event.getAction() == 0) {
109 } catch (IOException ioe) {
115 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
116 if ((infoflags & ALLBITS) != 0) {