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.GlobalHistogramBinarizer;
27 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
32 import javax.imageio.ImageIO;
36 * <p>Simple GUI frontend to the library. Right now, only decodes a local file.
37 * This definitely needs some improvement. Just throwing something down to start.</p>
41 public final class GUIRunner extends JFrame {
43 private final JLabel imageLabel;
44 private final JTextArea textArea;
47 imageLabel = new JLabel();
48 textArea = new JTextArea();
49 textArea.setEditable(false);
50 textArea.setMaximumSize(new Dimension(400, 200));
51 Container panel = new JPanel();
52 panel.setLayout(new FlowLayout());
53 panel.add(imageLabel);
57 setDefaultCloseOperation(EXIT_ON_CLOSE);
58 setContentPane(panel);
59 setLocationRelativeTo(null);
62 public static void main(String[] args) throws MalformedURLException {
63 GUIRunner runner = new GUIRunner();
64 runner.setVisible(true);
68 private void chooseImage() throws MalformedURLException {
69 JFileChooser fileChooser = new JFileChooser();
70 fileChooser.showOpenDialog(this);
71 File file = fileChooser.getSelectedFile();
72 Icon imageIcon = new ImageIcon(file.toURI().toURL());
73 setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight() + 100);
74 imageLabel.setIcon(imageIcon);
75 String decodeText = getDecodeText(file);
76 textArea.setText(decodeText);
79 private static String getDecodeText(File file) {
82 image = ImageIO.read(file);
83 } catch (IOException ioe) {
84 return ioe.toString();
87 return "Could not decode image";
89 LuminanceSource source = new BufferedImageLuminanceSource(image);
90 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
93 result = new MultiFormatReader().decode(bitmap);
94 } catch (ReaderException re) {
97 return result.getText();