package net.reini;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamEvent;
import com.github.sarxos.webcam.WebcamListener;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.oned.EAN13Reader;

public class BarcodeScanner {
	public static void main(String[] args) throws Exception {
		ImageIcon imageIcon = new ImageIcon();
		JLabel imageLabel = new JLabel(imageIcon);
		JFrame testFrame = new JFrame("Test");
		testFrame.add(imageLabel, BorderLayout.CENTER);
		testFrame.setVisible(true);

		Webcam webcam = Webcam.getDefault();
		webcam.addWebcamListener(new WebcamListener() {

			@Override
			public void webcamOpen(WebcamEvent we) {
			}

			@Override
			public void webcamImageObtained(WebcamEvent we) {
				BufferedImage image = we.getImage();
				imageIcon.setImage(image);
				imageLabel.repaint();

				LuminanceSource source = new BufferedImageLuminanceSource(image);
				BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
						source));
				Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
				hints.put(DecodeHintType.CHARACTER_SET, "GBK");
				try {
					Reader reader = new EAN13Reader();
					Result result = reader.decode(bitmap, hints);
					System.out.println(result.getText());
				} catch (NotFoundException | ChecksumException | FormatException e) {
				}
			}

			@Override
			public void webcamDisposed(WebcamEvent we) {
			}

			@Override
			public void webcamClosed(WebcamEvent we) {
			}
		});
		webcam.open(true);
	}
}

@@ -192,16 +192,52 @@
 .JLabel;
+%0Aimport javax.swing.WindowConstants;
 %0A%0Aimport
@@ -570,51 +570,8 @@
 ce;%0A
-import com.google.zxing.MultiFormatReader;%0A
 impo
@@ -919,16 +919,108 @@
 ption %7B%0A
+%09%09for (Webcam webcam : Webcam.getWebcams()) %7B%0A%09%09%09System.out.println(webcam.getName());%0A%09%09%7D%0A%0A
 %09%09ImageI
@@ -1103,164 +1103,95 @@
 ;%0A%09%09
-JFrame testFr
+Webcam webc
 am
-e
  = 
-new JFrame(%22Test%22);%0A%09%09testFrame.add(imageLabel, BorderLayout.CENTER);%0A%09%09testFrame.setVisible(true);%0A%0A%09%09Webcam webcam = Webcam.getDefault(
+Webcam.getDefault();%0A%09%09imageLabel.setPreferredSize(webcam.getViewSize()
 );%0A%09
@@ -1239,17 +1239,16 @@
 ner() %7B%0A
-%0A
 %09%09%09@Over
@@ -1928,17 +1928,23 @@
 xception
- 
+%0A%09%09%09%09%09%09
 %7C Format
@@ -2131,10 +2131,220 @@
 ue);
+%0A%0A%09%09JFrame testFrame = new JFrame(%22Test%22);%0A%09%09testFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);%0A%09%09testFrame.add(imageLabel, BorderLayout.CENTER);%0A%09%09testFrame.pack();%0A%09%09testFrame.setVisible(true);
 %0A%09%7D%0A%7D
-%0A

set preferred image label size