日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

QML for Android 实现二维码扫描(QZXing)

發布時間:2025/1/21 Android 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QML for Android 实现二维码扫描(QZXing) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

由于項目要求,需要用 Qt 做一個Android 程序用于掃描二維碼并獲取掃碼內容,之前參考了很多資料,很多都是基于桌面端的二維碼識別,如一去丶二三里大神的Qt之二維碼掃描,原理是直接導入一張二維碼圖片然后進行識別,而移動端會稍微麻煩一點點,并且關于 Qt 來做一個 Android 程序掃描二維碼的資料太少,最終通過各種嘗試,采用 QML+QZXing 的結合來實現該功能,這里做一個簡單的總結。

原理

程序實現原理大概分為幾個步驟:

1.Camera啟動手機攝像頭 2.VideoOutput獲取一個攝像頭的預覽視頻幀 3.QZXing對視頻幀進行解析并返回內容

效果圖

界面做得很簡陋,但是功能實現,后期只要提供界面圖標來重新實現界面就完美了。

左邊是程序界面,中間的掃描線是動態的,右邊是掃描后的效果,content 是掃描到的二維碼內容,Detected count是檢測次數。

源碼

來看看 QML 端關鍵的代碼

import QtQuick 2.6 import QtQuick.Window 2.0 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.1 import QtMultimedia 5.5 import QZXing 2.3ApplicationWindow {id: windowvisible: truewidth: 1080height: 1920title: "Qt QZXing Filter Test"property int detectedTags: 0property string lastTag: ""property int nWidth: Math.min(width,height)Rectangle{id: bgRectcolor: "white"anchors.fill: videoOutput}Label{id: text1wrapMode: Text.Wrapcolor:"white"z: 50text: "Detected count: " + detectedTags}Camera{id:camerafocus {focusMode: CameraFocus.FocusContinuousfocusPointMode: CameraFocus.FocusPointAuto}}VideoOutput{id: videoOutputsource: cameraanchors.fill: parentautoOrientation: truefillMode: VideoOutput.Stretchfilters: [ zxingFilter ]MouseArea {anchors.fill: parentonClicked: {camera.focus.customFocusPoint = Qt.point(mouse.x / width, mouse.y / height);camera.focus.focusMode = CameraFocus.FocusMacro;camera.focus.focusPointMode = CameraFocus.FocusPointCustom;}}Rectangle {id: captureZonewidth: nWidth*0.6height: widthanchors.centerIn: parentcolor:"transparent"border.color: "lightgreen"border.width: 3Rectangle{id:linewidth: parent.width*0.9height: 12color: "lightgreen"anchors.horizontalCenter: parent.horizontalCenterradius: 8PropertyAnimation{id:anitarget:lineproperties: "y"duration: 1500from:0running: trueto:captureZone.heightonStopped: {y=0ani.start()}}}}}QZXingFilter{id: zxingFiltercaptureRect: {// setup bindingsvideoOutput.contentRect;videoOutput.sourceRect;//識別的區域,這里稍微比掃描框大一些return videoOutput.mapRectToSource(videoOutput.mapNormalizedRectToItem(Qt.rect(0.2, 0.2, 0.6, 0.6)));}decoder {enabledDecoders: QZXing.DecoderFormat_EAN_13 | QZXing.DecoderFormat_CODE_39 | QZXing.DecoderFormat_QR_CODEonTagFound: {console.log(tag + " | " + decoder.foundedFormat() + " | " + decoder.charSet());window.detectedTags++;window.lastTag = tag;}tryHarder: false}onDecodingStarted:{console.log("started");}property int framesDecoded: 0property real timePerFrameDecode: 0onDecodingFinished:{timePerFrameDecode = (decodeTime + framesDecoded * timePerFrameDecode) / (framesDecoded + 1);framesDecoded++;console.log("frame finished: " + succeeded, decodeTime, timePerFrameDecode, framesDecoded);}}Label{wrapMode: Text.Wrapcolor:"white"anchors.bottom: parent.bottomz: 50text: "content: " + lastTag} }

權限

在 Android 上運行,開啟攝像頭需要先添加權限,在 AndroidManifest.xml文件中添加:

<uses-permission android:name="android.permission.CAMERA"/>

關于 QZXing

QML 中用到的二維碼解碼庫,需要去官網下載源碼并導入到工程中, 官網:QZXing ,詳情請參考官網介紹,這里不再贅述。

源碼

鑒于有好幾位童鞋遇到問題,在開始玩 GitHub 后,就整理了一份完整的代碼,有疑問的可以直接直接下載源碼進行編譯。
代碼地址:https://github.com/luoyayun361/QML-Android-ScanCode
歡迎大家提出自己更好的想法,共同進步和交流。

總結

以上是生活随笔為你收集整理的QML for Android 实现二维码扫描(QZXing)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。