您好,登錄后才能下訂單哦!
java中怎么利用電腦攝像頭識別二維碼?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1、從攝像頭獲取圖像,2、根據圖片解析出二維碼信息。
在上一篇java攝像頭截圖已經實現了攝像頭截圖,只要再加上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); } }
關于java中怎么利用電腦攝像頭識別二維碼問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。