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