Refactored the MonochromeBitmapSource class hierarchy into LuminanceSource, Binarizer...
[zxing.git] / javase / src / com / google / zxing / client / j2se / GUIRunner.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.j2se;
18
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.common.GlobalHistogramBinarizer;
25
26 import java.awt.*;
27 import java.awt.image.BufferedImage;
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31
32 import javax.imageio.ImageIO;
33 import javax.swing.*;
34
35 /**
36  * <p>Simple GUI frontend to the library. Right now, only decodes a local file.
37  * This definitely needs some improvement. Just throwing something down to start.</p>
38  *
39  * @author Sean Owen
40  */
41 public final class GUIRunner extends JFrame {
42
43   private final JLabel imageLabel;
44   private final JTextArea textArea;
45
46   private GUIRunner() {
47     imageLabel = new JLabel();
48     textArea = new JTextArea();
49     textArea.setEditable(false);
50     textArea.setMaximumSize(new Dimension(400, 200));
51     Container panel = new JPanel();
52     panel.setLayout(new FlowLayout());
53     panel.add(imageLabel);
54     panel.add(textArea);
55     setTitle("ZXing");
56     setSize(400, 400);
57     setDefaultCloseOperation(EXIT_ON_CLOSE);
58     setContentPane(panel);
59     setLocationRelativeTo(null);
60   }
61
62   public static void main(String[] args) throws MalformedURLException {
63     GUIRunner runner = new GUIRunner();
64     runner.setVisible(true);
65     runner.chooseImage();
66   }
67
68   private void chooseImage() throws MalformedURLException {
69     JFileChooser fileChooser = new JFileChooser();
70     fileChooser.showOpenDialog(this);
71     File file = fileChooser.getSelectedFile();
72     Icon imageIcon = new ImageIcon(file.toURI().toURL());
73     setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight() + 100);
74     imageLabel.setIcon(imageIcon);
75     String decodeText = getDecodeText(file);
76     textArea.setText(decodeText);
77   }
78
79   private static String getDecodeText(File file) {
80     BufferedImage image;
81     try {
82       image = ImageIO.read(file);
83     } catch (IOException ioe) {
84       return ioe.toString();
85     }
86     if (image == null) {
87       return "Could not decode image";
88     }
89     LuminanceSource source = new BufferedImageLuminanceSource(image);
90     BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
91     Result result;
92     try {
93       result = new MultiFormatReader().decode(bitmap);
94     } catch (ReaderException re) {
95       return re.toString();
96     }
97     return result.getText();
98   }
99
100 }