本文实例为大家分享了java使用电脑摄像头识别二维码的具体代码,供大家参考,具体内容如下
要想摄像头识别二维码,需要两个基本功能:
1、从摄像头获取图像,2、根据图片解析出二维码信息。
在上一篇java摄像头截图已经实现了摄像头截图,只要再加上zxing(或其它能从图片中解析二维码的组件),就能从图像中解析出二维码,实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
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以上,活动二维码签到、物品扫描,只需扛台手提,再加个高清摄像头就行了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/luckyboyguo/article/details/37691217?utm_source=blogkpcl3