2 * Copyright 2008 Google Inc.
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.j2se;
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
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;
36 import java.io.IOException;
37 import java.net.MalformedURLException;
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>
43 * @author srowen@google.com (Sean Owen)
45 public final class GUIRunner extends JFrame {
47 private final JLabel imageLabel;
48 private final JTextArea textArea;
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);
61 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62 setContentPane(panel);
63 setLocationRelativeTo(null);
66 public static void main(String[] args) throws Exception {
67 GUIRunner runner = new GUIRunner();
68 runner.setVisible(true);
72 private void chooseImage() {
73 JFileChooser fileChooser = new JFileChooser();
74 fileChooser.showOpenDialog(GUIRunner.this);
75 File file = fileChooser.getSelectedFile();
78 imageIcon = new ImageIcon(file.toURL());
79 } catch (MalformedURLException mue) {
80 throw new RuntimeException(mue);
82 setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight() + 100);
83 imageLabel.setIcon(imageIcon);
84 String decodeText = getDecodeText(file);
85 textArea.setText(decodeText);
88 private static String getDecodeText(File file) {
91 image = ImageIO.read(file);
92 } catch (IOException ioe) {
93 return ioe.toString();
96 return "Could not decode image";
98 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
101 result = new MultiFormatReader().decode(source);
102 } catch (ReaderException re) {
103 return re.toString();
105 return result.getText();