温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中怎么利用电脑摄像头识别二维码

发布时间:2021-06-09 17:28:59 来源:亿速云 阅读:415 作者:Leah 栏目:编程语言

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);
  }
 }

关于java中怎么利用电脑摄像头识别二维码问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI