亲宝软件园·资讯

展开

java使用电脑摄像头识别二维码 java使用电脑摄像头识别二维码

a19880813 人气:0

要想摄像头识别二维码,需要两个基本功能:

1、从摄像头获取图像,2、根据图片解析出二维码信息。

在上一篇java摄像头截图已经实现了摄像头截图,只要再加上zxing(或其它能从图片中解析二维码的组件),就能从图像中解析出二维码,实现代码如下:

package com.pengo.capture;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.media.MediaLocator;
import javax.swing.JPanel;
import javazoom.jl.player.Player;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
 
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.ui.application.ContainerPlayer;
import net.sf.fmj.ui.application.PlayerPanelPrefs;
public class CameraFrame2 extends JFrame{
  private static int num = 0;
  public CameraFrame2() throws Exception{
   this.setTitle("摄像头截图应用");
   this.setSize(480, 500);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   final JPanel cameraPanel = new JPanel();
   this.getContentPane().setLayout(new BorderLayout());
   this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
   ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
   MediaLocator locator = CaptureDeviceBrowser.run(null); //弹出摄像头设备选择
 
   PlayerPanelPrefs prefs = new PlayerPanelPrefs();
   containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
   
   new Thread() {
    public void run() {
     while (true) {
      try {
      Thread.sleep(1000);
       Dimension imageSize = cameraPanel.getSize();
       BufferedImage image = new BufferedImage(
         imageSize.width, imageSize.height,
         BufferedImage.TYPE_INT_ARGB);
       Graphics2D g = image.createGraphics();
       cameraPanel.paint(g);
       g.dispose();
       LuminanceSource source = new BufferedImageLuminanceSource(
         image);
       BinaryBitmap bitmap = new BinaryBitmap(
        new HybridBinarizer(source));
       Result result;
       result = new MultiFormatReader().decode(bitmap);
       System.out.println("二维码====:" + result.getText());
       InputStream is = CameraFrame.class.getClassLoader().getResourceAsStream("resource/beep.mp3");
       Player player = new Player(is);
       player.play();
      } catch (Exception re) {
       re.printStackTrace();
      }
     }
    }
   }.start();
  }
  
  public static void main(String[] args) throws Exception{
   CameraFrame2 camera = new CameraFrame2();
   camera.setVisible(true);
  }
 }

最后来张效果图(本图仅供参考)


要想识别效果好点,摄像头像素最好500W以上,活动二维码签到、物品扫描,只需扛台手提,再加个高清摄像头就行了。

加载全部内容

相关教程
猜你喜欢
用户评论