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