2 * Copyright 2008 ZXing authors
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.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;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
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>
46 public final class GUIRunner extends JFrame {
48 private final JLabel imageLabel;
49 private final JTextArea textArea;
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);
62 setDefaultCloseOperation(EXIT_ON_CLOSE);
63 setContentPane(panel);
64 setLocationRelativeTo(null);
67 public static void main(String[] args) throws MalformedURLException {
68 GUIRunner runner = new GUIRunner();
69 runner.setVisible(true);
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);
84 private static String getDecodeText(File file) {
87 image = ImageIO.read(file);
88 } catch (IOException ioe) {
89 return ioe.toString();
92 return "Could not decode image";
94 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
97 result = new MultiFormatReader().decode(source);
98 } catch (ReaderException re) {
101 return result.getText();