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