Using qzxing to identify two-dimensional codes in Qt5 QML

Source: Internet
Author: User

Objective

Zxing Library is a library to identify the QR code, qzxing is a QT-based Qt Wrapper library, in this article we use it and qml to develop a small Android app.

Add Qzxing Project

Qzxing is most commonly used as a subproject included in our project, in the pro file of our project, add the following sentence:

include(./QZXing/QZXing.pri)

Qzxing and our pro files are in the same directory.

QML Program
Import QtQuick2.0Import Qtquick.controls1.3Import Qtquick.window2.2Import Qtquick.dialogs1.2Import qzxing2.3Applicationwindow {title:qstr ("Hello World") Width:640Height480VisibletrueProperty Real Dpi:Screen.pixelDensity.toFixed (2) Menubar:menubar {Menu {title:qstr ("&file") MenuItem {text:qstr ("&open") OnTriggered:messageDialog.show (Qstr ("Open action triggered")); } MenuItem {Text:qstr ("E&xit") OnTriggered:Qt.quit (); }}} Image {Id:qr_code Source:"Qrc:/image/qrcode.png"Height -* DPI Width: -* dpi fillMode:Image.PreserveAspectFit anchors {top:parent.top; TopMargin:2* Dpi;horizontalcenter:parent.horizontalcenter}} Button {Text:qstr ("O") Height:Ten* DPI anchors {top:qr_code.bottom; TopMargin:2* DPI; Left:parent.left; LeftMargin:2* DPI; Right:parent.right; RightMargin:2* DPI} onclicked: {decoder.decodeimageqml (Qr_code)}} qzxing {Id:decoder E NabledDecoders:QZXing.DecoderFormat_QR_CODE ondecodingstarted: {console.log ("qzxing decode start!")} ondecodingfinished: {if(succeeded) {Console.log ("Success")            }Else{Console.log ("Fail")}} Ontagfound: {messagedialog.show ("Qr_code:"+ tag)}} messagedialog {Id:messagedialog title:qstr ("May I had your attention, please?") function show(caption) {Messagedialog.text = caption;        Messagedialog.open (); }    }}

Then we compile our program. This is qzxing will report two errors.
1.147 lines in the CameraImageWrapper.cpp file

#if __cplusplus > 199711L        memcpy(m, tmpRow->values()..data(), width);#else        memcpy(m, &tmpRow->values()[0], width);#endif

Just delete a point.
2. Maybe my compilation environment does not have a iconv.h file, I add the following address in the Pro file

    DEFINES += NO_ICONV

So our program is compiled and passed.

QML Image to Qimage

Then we test, no failure of the QR code, I debug into the DECODEIMAGEQML function, this sentence

    QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);

Failed, item is null, no conversion succeeded.
Find information on the Internet to find that it is possible to run the following versions of QT5. Then see QT5 start QML the backend implementation is different. Then I looked at Qt5 's document and found that item in QML was an instance of the Qquickitem class, inherited from Qobject and Qqmlparserstatus. In Qt4.8, QML's item is an instance of Qdeclarativeitem, and Qdeclarativeitem inherits Qgraphicsobject and Qdeclarativeparserstatus, so the following versions of QT5 can run, and QT5 will not 。 Qquickitem has a window function that returns the form that the item renders, which is a Qquickwindow object, and Qquickwindow has a gradwindow function, which is described as follows:
grabs the contents of the window and returns it as an image.
So I modified the imagehandler::extractqimage function.

Qimage imagehandler::extractqimage (Qobject *imageobj,Const DoubleOffsetX,Const DoubleOffsetY,Const DoubleWidthConst DoubleHeight) {#If qt_version >= 0x050000Qquickitem *item = qobject_cast<qquickitem*> (imageobj);#Else Qgraphicsobject *item = qobject_cast<qgraphicsobject*> (imageobj);#endif     if(!item) {Qdebug () <<"Item is NULL";returnQimage (); }#If qt_version >= 0x050000Qquickwindow *window = Item->window (); Qimage img = Window->grabwindow ();#Else Qimage img (item->boundingrect (). Size (). Tosize (), QIMAGE::FORMAT_RGB32); Img.fill (Qcolor (255,255,255). RGB ());    qpainter painter (&img);    Qstyleoptiongraphicsitem styleoption; Item->paint (&painter, &styleoption);#endif     if(OffsetX = =0&& OffsetY = =0&& width = =0&& height = =0)returnimgElse{returnImg.copy (OffsetX, OffsetY, width, height); }}

OK, identify the success, there is a picture for the card

Forgot
To use qzxing in QML, you need to register it in the main function

#include <QApplication>#include <QQmlApplicationEngine>#include "QZXing.h"int main(intchar *argv[]){    QApplication app(argc, argv);    QZXing::registerQMLTypes();    QQmlApplicationEngine engine;    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));    return app.exec();}

Using qzxing to identify two-dimensional codes in Qt5 QML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.