JavaCV/OpenCV 二維碼掃描功能
怎樣配置工程就不再贅述,不清楚的讀者可以網上查找資料,二維碼掃描功能通過JavaCV實作起來還是挺簡單的,主要OpenCV中QRCodeDetector提供強大的API,識別速度個人感覺以微信(經常掃不到)來作對比還是快許多,

//本地安裝的攝像頭驅動可能有多個,選擇合適的
VideoInputFrameGrabber grabber = VideoInputFrameGrabber.createDefault(1);
grabber.start();
QRCodeDetector qrCodeDetector = new QRCodeDetector();
OpenCVFrameConverter.ToMat frameConverter = new OpenCVFrameConverter.ToMat();
String windowTitle = "QR SCAN";
Frame frame = null;
int count = 0;
while ((frame = grabber.grabFrame()) != null) {
Mat img = frameConverter.convertToMat(frame);//如果需要擴展可以在影像前面添加掃描框
int imageWidth = img.size().width();
int imageHeight = img.size().height();
int hStartPosition = Float.valueOf(imageHeight * 0.2f).intValue();
int maxPostion = Float.valueOf(imageHeight * 0.8f).intValue();
int lineHeight = hStartPosition + Float.valueOf(count++ * 0.05f * imageHeight).intValue();
if (lineHeight > maxPostion) {
lineHeight = hStartPosition;
count = 0;
}
Point pt1 = new Point(Float.valueOf(imageWidth * 0.2f).intValue(), lineHeight);
Point pt2 = new Point(Float.valueOf(imageWidth * 0.8f).intValue(), lineHeight);
opencv_imgproc.line(img, pt1, pt2, Scalar.WHITE, 5, opencv_imgproc.LINE_8, 0);
opencv_highgui.imshow(windowTitle, img);
StringVector decoded_info = new StringVector();
boolean success = qrCodeDetector.detectAndDecodeMulti(img, decoded_info);
if (success) {
BytePointer bytePointer = decoded_info.get(0);
this.output("結果:" + bytePointer.getString());
break;
}
int cvWaitKey = opencv_highgui.cvWaitKey(50);
if (cvWaitKey == 27) {
break;
}
}
grabber.close();
qrCodeDetector.close();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/163132.html
標籤:其他
下一篇:模電課程設計——三級電路2
