posts - 59, comments - 244, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

使用电脑摄像头识别二维码

Posted on 2012-09-10 23:50 penngo 阅读(6473) 评论(2)  编辑  收藏 所属分类: 练手作品
要想摄像头识别二维码,需要两个基本功能:1、从摄像头获取图像,2、根据图片解析出二维码信息。在上一篇java摄像头截图已经实现了摄像头截图,只要再加上zxing(或其它能从图片中解析二维码的组件),就能从图像中解析出二维码,实现代码如下:

 1 package com.pengo.capture;
 2 
 3 import javax.swing.JFrame;
 4 import java.awt.BorderLayout;
 5 import java.awt.Dimension;
 6 import java.awt.Graphics2D;
 7 import java.awt.image.BufferedImage;
 8 import java.io.InputStream;
 9 import javax.media.MediaLocator;
10 import javax.swing.JPanel;
11 import javazoom.jl.player.Player;
12 import com.google.zxing.BinaryBitmap;
13 import com.google.zxing.LuminanceSource;
14 import com.google.zxing.MultiFormatReader;
15 import com.google.zxing.Result;
16 import com.google.zxing.common.HybridBinarizer;
17 
18 import net.sf.fmj.ui.application.CaptureDeviceBrowser;
19 import net.sf.fmj.ui.application.ContainerPlayer;
20 import net.sf.fmj.ui.application.PlayerPanelPrefs;
21 public class CameraFrame2 extends JFrame{
22     private static int num = 0;
23     public CameraFrame2() throws Exception{
24         this.setTitle("摄像头截图应用");
25         this.setSize(480, 500);
26         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27         final JPanel cameraPanel = new JPanel();
28         this.getContentPane().setLayout(new BorderLayout());
29         this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
30         ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
31         MediaLocator locator = CaptureDeviceBrowser.run(null);   //弹出摄像头设备选择
32 
33         PlayerPanelPrefs prefs = new PlayerPanelPrefs();
34         containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
35         
36         new Thread() {
37             public void run() {
38                 while (true) {
39                     try {
40                         Thread.sleep(1000);
41                         Dimension imageSize = cameraPanel.getSize();
42                         BufferedImage image = new BufferedImage(
43                                 imageSize.width, imageSize.height,
44                                 BufferedImage.TYPE_INT_ARGB);
45                         Graphics2D g = image.createGraphics();
46                         cameraPanel.paint(g);
47                         g.dispose();
48                         LuminanceSource source = new BufferedImageLuminanceSource(
49                                 image);
50                         BinaryBitmap bitmap = new BinaryBitmap(
51                                 new HybridBinarizer(source));
52                         Result result;
53                         result = new MultiFormatReader().decode(bitmap);
54                         System.out.println("二维码====:" + result.getText());
55                         InputStream is = CameraFrame.class.getClassLoader().getResourceAsStream("resource/beep.mp3");
56                         Player player = new Player(is);
57                         player.play();
58                     } catch (Exception re) {
59                         re.printStackTrace();
60                     }
61                 }
62             }
63         }.start();
64     }
65     
66     public static void main(String[] args) throws Exception{
67         CameraFrame2 camera = new CameraFrame2();
68         camera.setVisible(true);
69     }
70 }

最后来张效果图(本图仅供参考)


要想识别效果好点,摄像头像素最好500W以上,
活动二维码签到、物品扫描,只需扛台手提,再加个高清摄像头就行了。

评论

# re: 使用电脑摄像头识别二维码  回复  更多评论   

2012-09-11 11:22 by greatghoul
好棒的代码。

# re: 使用电脑摄像头识别二维码  回复  更多评论   

2012-09-27 17:04 by kelven
好代码!

修改第53行解决中文乱码问题:

Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap, hints);

只有注册用户登录后才能发表评论。


网站导航: