java使用电脑摄像头识别二维码
程序员文章站
2024-02-26 23:07:52
本文实例为大家分享了java使用电脑摄像头识别二维码的具体代码,供大家参考,具体内容如下
要想摄像头识别二维码,需要两个基本功能:
1、从摄像头获取图像,2、根据图片解...
本文实例为大家分享了java使用电脑摄像头识别二维码的具体代码,供大家参考,具体内容如下
要想摄像头识别二维码,需要两个基本功能:
1、从摄像头获取图像,2、根据图片解析出二维码信息。
在上一篇已经实现了摄像头截图,只要再加上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以上,活动二维码签到、物品扫描,只需扛台手提,再加个高清摄像头就行了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。