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