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.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;
26 import java.awt.Container;
27 import java.awt.Dimension;
28 import java.awt.FlowLayout;
29 import java.awt.image.BufferedImage;
31 import java.io.IOException;
32 import java.net.MalformedURLException;
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;
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>
49 public final class GUIRunner extends JFrame {
51 private final JLabel imageLabel;
52 private final JTextArea textArea;
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);
65 setDefaultCloseOperation(EXIT_ON_CLOSE);
66 setContentPane(panel);
67 setLocationRelativeTo(null);
70 public static void main(String[] args) throws MalformedURLException {
71 GUIRunner runner = new GUIRunner();
72 runner.setVisible(true);
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);
87 private static String getDecodeText(File file) {
90 image = ImageIO.read(file);
91 } catch (IOException ioe) {
92 return ioe.toString();
95 return "Could not decode image";
97 LuminanceSource source = new BufferedImageLuminanceSource(image);
98 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
101 result = new MultiFormatReader().decode(bitmap);
102 } catch (ReaderException re) {
103 return re.toString();
105 return result.getText();