首页 > 编程 > Java > 正文

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

2019-11-26 09:36:28
字体:
来源:转载
供稿:网友

本文实例为大家分享了java使用电脑摄像头识别二维码的具体代码,供大家参考,具体内容如下

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

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以上,活动二维码签到、物品扫描,只需扛台手提,再加个高清摄像头就行了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表