java使用電腦攝像頭識別二維碼
本文實例為大家分享了java使用電腦攝像頭識別二維碼的具體代碼,供大家參考,具體內(nèi)容如下
要想攝像頭識別二維碼,需要兩個基本功能:
1、從攝像頭獲取圖像,2、根據(jù)圖片解析出二維碼信息。
在上一篇java攝像頭截圖已經(jīng)實現(xiàn)了攝像頭截圖,只要再加上zxing(或其它能從圖片中解析二維碼的組件),就能從圖像中解析出二維碼,實現(xiàn)代碼如下:
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("攝像頭截圖應(yīng)用");
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); //彈出攝像頭設(shè)備選擇
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以上,活動二維碼簽到、物品掃描,只需扛臺手提,再加個高清攝像頭就行了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot3請求參數(shù)種類及接口測試案例小結(jié)
這篇文章主要介紹了springboot3請求參數(shù)種類及接口測試案例小結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10
詳解Spring Cloud中Hystrix 線程隔離導(dǎo)致ThreadLocal數(shù)據(jù)丟失
這篇文章主要介紹了詳解Spring Cloud中Hystrix 線程隔離導(dǎo)致ThreadLocal數(shù)據(jù)丟失,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
學(xué)習(xí)Java的static與final關(guān)鍵字
本篇文章給大家詳細(xì)分析了Java的static與final關(guān)鍵字知識點以及相關(guān)代碼分享,有需要的讀者跟著學(xué)習(xí)下吧。2018-03-03
SpringBoot2.4.2下使用Redis配置Lettuce的示例
這篇文章主要介紹了SpringBoot2.4.2下使用Redis配置Lettuce,Springboot2.4.2下默認(rèn)使用的就是Lettuce而不是Jedis因此無需在依賴進(jìn)行排除Jedis,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2022-01-01
java定時任務(wù)實現(xiàn)的4種方式小結(jié)
這篇文章主要介紹了java定時任務(wù)實現(xiàn)的4種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
idea插件之mybatis log plugin控制臺sql的問題
這篇文章主要介紹了idea插件之mybatis log plugin控制臺sql,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

