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)文章
Java中關(guān)于isEmpty方法、null以及““的區(qū)別
這篇文章主要介紹了Java中關(guān)于isEmpty方法、null以及““的區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring Cloud Zipkin服務(wù)端追蹤服務(wù)
這篇文章主要介紹了Spring Cloud Zipkin服務(wù)端追蹤服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
java實現(xiàn)zip,gzip,7z,zlib格式的壓縮打包
本文是利用Java原生類和apache的commons實現(xiàn)zip,gzip,7z,zlib的壓縮打包,如果你要是感興趣可以進(jìn)來了解一下。2016-10-10

