Qt for iOS scan QR code feature

Source: Internet
Author: User
<span id="Label3"></p><p><p>Problem:</p></p><p><p>Company Project considering the Cross-platform has been done with qt, due to project needs, the project to add a QR code scanning function, in the Android can use Qvideoprobe to achieve the capture camera video frame, with qzxing decoding the picture, so as to achieve QR code scanning, but on ios, Qvideprobe does not support, so have to choose other methods to grab video frames, consider using OPENCV to implement capturing video frames, but when viewing iOS documents, iOS7 directly support the QR code scanning function, so abandon the use of OpenCV crawl + Zxing decoding Method. Thus take the official iOS QR code decoding Function.</p></p><p><p></p></p><p><p>Realize:</p></p><p><p>Since our project UI has been implemented with qml, it is necessary to call the Avfoundation method in order to implement the scan QR code function, and to display the UI display camera and the return QML key in ios. so here you need to combine OC and QT programming.</p></p><p><p>Directly on the Code:</p></p><p><p>Pro File added</p></p><p><p>iOS {</p></p><p><p>Objective_sources + = iosview.mm \ # Object C + + file</p></p><p><p>ioscamera.mm</p></p><p><p>HEADER + = IOSView.h \</p></p><p><p>IOSCAMERA.H \</p></p><p><p>IOSCameraViewProtocol.h</p></p><p><p></p></p><p><p>Qmake_lflags + =-framework avfoundation #add avfoundation Framework</p></p><p><p></p></p><p><p>QT + = GUI Private</p></p><p><p>}</p></p><p><p>Re-qmake Build Xcode Project</p></p><p><p>Iosview. #include <QQuickItem></p></p><pre class="brush:cpp;gutter:true;"><pre class="brush:cpp;gutter:true;">Class Iosview:public qquickitem{ q_object q_property (QString qrcodetext READ qrcodetext WRITE setqrcodetext NOTIFY qrcodetextchanged) Public : explicit Iosview (qquickitem *parent = 0); QString qrcodetext () { return m_qrcodetext; } void Setqrcodetext (QString Text) { m_qrcodetext = text; Emit qrcodetextchanged (); } QString m_qrcodetext; Public slots: void Startscan ();  For open iOS camera scan and UI private: void *m_delegate;  For communication with QT signals: void qrcodetextchanged ();   void Stopcamerascan (); Show qml};<br><br></pre></pre><p><p>Iosview. Mm</p></p><pre class="brush:cpp;gutter:true;">#include <UIKit/UIKit.h> #include <QtQuick> #include <QtGui> #include <qtgui/qpa/ qplatformnativeinterface.h> #include "IOSView.h" #include "IOSCamera.h" @interface Ioscameradelegate:nsobject < Ioscameraprotocol> {iosview *m_iosview;} @end @implementation ioscameradelegate-(id) initwithioscamera: (iosview *) iosview{self = [super init]; If (self) {m_iosview = iosview; } return self;} -(void) Scancancel{emit M_iosview->stopcamerascan ();} -(void) scanresult:(nsstring *) result{m_iosview->setqrcodetext (qstring::fromnsstring (result));} @endIOSView:: iosview (qquickitem *parent): qquickitem (parent), m_delegate ([[ioscameradelegate alloc] Initwithioscamera:this]) {} void Iosview::startscan () {//Get The UIView that backs our Qquickwindow:uiview *view = Static_cast<uiview *> (qguiapplication::p latformnativeinterface ()->nativeresourceforwindow ("UIView", Window ())); Uiviewcontroller *qtcontroller = [[view window] Rootviewcontroller]; Ioscamera *ioscamera = [[[[ioscameraview alloc] init]autorelease]; Ioscamera.delegate = (id) m_delegate; Tell the imagecontroller-animate on top: [qtcontroller presentviewcontroller:ioscamera animated:yes Completion:ni l]; [ioscamera startscan];}</pre><p><p>  </p></p><p><p>IOSCameraViewProtocol.h</p></p><pre class="brush:cpp;gutter:true;"><pre class="brush:cpp;gutter:true;">#import <Foundation/Foundation.h> @protocol camerascanviewprotocol <NSObject> @required-(void) Scancancel;-(void) scanresult:(nsstring *) result; @end</pre></pre><p><p>  </p></p><p><p>IOSCamera.h</p></p><pre class="brush:cpp;gutter:true;"><pre class="brush:cpp;gutter:true;">#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import "CameraViewProtocol.h" @interface Ioscamera:uiviewcontroller <avcapturemetadataoutputobjectsdelegate>{ id<camerascanviewprotocol> delegate;} @property (retain, nonatomic) iboutlet UIView *viewpreview;-(ibaction) backqtapp: (id) sender;-(void) startscan;@ Property (retain) id<camerascanviewprotocol> delegate; @end</pre></pre><p><p>  </p></p><p><p>Ioscamera.cpp#import <span style="color: #800000;">"IOS</span><span style="color: #800000;">Camera.h</span><span style="color: #800000;">"</span></p></p><pre><em><span style="color: #000000;"><span style="color: #000000;">@interface ioscamera () @property (nonatomic,strong) avcapturesession</span></span>*<span style="color: #000000;"><span style="color: #000000;">capturesession, @property (nonatomic,strong) avcapturevideopreviewlayer</span></span>*<span style="color: #000000;"><span style="color: #000000;">videopreviewlayer;</span></span>-<span style="color: #000000;"><span style="color: #000000;">(BOOL) startreading;</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) stopreading;</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) openqtlayer;</span></span><span style="color: #000000;"><span style="color: #000000;">@end @implementation camerascanview@synthesize</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">; Sync delegate for interactive with Qt</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">Do any <span style="color: #008000;">additional setup after loading the view from its nib. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">initially make the Capturesession object Nil.</span></span>_capturesession =<span style="color: #000000;"><span style="color: #000000;">nil; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set The initial value of the flag to NO.</span></span>_isreading =<span style="color: #000000;"><span style="color: #000000;">NO; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">Begin loading the effect so to has it ready for <span style="color: #008000;">playback when it ' s Needed.</span></span><span style="color: #000000;"><span style="color: #000000;">[self loadbeepsound];<br></span></span><span style="color: #000000;"><span style="color: #000000;"> }</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) didreceivememorywarning {[super didreceivememorywarning]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dispose of any resources the can be Recreated.</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">#pragma mark-navigation</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Dealloc {[_viewpreview release]; [super dealloc];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidunload {[self setviewpreview:nil]; [super viewdidunload];}</span></span></em>-(ibaction) backqtapp: (id) Sender {<em id="__mceDel"><em id="__mceDel"><em id="__mceDel">[delegate scancancel];</em></em></em></pre><em id="__mceDel"><em id="__mceDel"><em id="__mceDel">[self stopreading];<br>}<br><br>-(void) openqtlayer{<br>Bring back Qt ' s view controller:<br>Uiviewcontroller *RVC = [[[uiapplication sharedapplication] keywindow] rootviewcontroller];<br>[RVC Dismissviewcontrolleranimated:yes completion:nil];<br>}<br><br>-(void) startscan{<br>If (!_isreading) {<br>The the case where the app should read a QR code when the Start button is Tapped.<br>If ([self startreading]) {<br>If the Startreading methods returns YES and the capture session is successfully<br>running, then change the Start button title and the status Message.<br>NSLog (@ "Start Reading!!");<br>}<br>}<br>}<br><br>#pragma mark-private method implementation<br><br>-(BOOL) startreading {<br>Nserror *error;<br><br>Get an instance of the Avcapturedevice class to initialize a device object and provide the video<br>As the media type Parameter.<br>Avcapturedevice *capturedevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo];<br><br>Get an instance of the Avcapturedeviceinput class using the previous device Object.<br>Avcapturedeviceinput *input = [avcapturedeviceinput Deviceinputwithdevice:capturedevice error:&error];<br><br>If (!input) {<br>If any error occurs, simply logs the description of it and don ' t continue any more.<br>NSLog (@ "%@", [error localizeddescription]);<br>Return NO;<br>}<br><br>Initialize the Capturesession object.<br>_capturesession = [[avcapturesession alloc] init];<br>Set the input device on the capture Session.<br>[_capturesession addinput:input];<br><br><br>Initialize a Avcapturemetadataoutput object and set it as the output device to the capture Session.<br>Avcapturemetadataoutput *capturemetadataoutput = [[avcapturemetadataoutput alloc] init];<br>[_capturesession addoutput:capturemetadataoutput];<br><br>Create a new serial dispatch Queue.<br>dispatch_queue_t dispatchqueue;<br>Dispatchqueue = Dispatch_queue_create ("myqueue", NULL);<br>[capturemetadataoutput setmetadataobjectsdelegate:self queue:dispatchqueue];<br>[capturemetadataoutput Setmetadataobjecttypes:[nsarray arraywithobject:avmetadataobjecttypeqrcode];<br><br>Initialize the video preview layer and add it as a sublayer to the Viewpreview view ' s layer.<br>_videopreviewlayer = [[avcapturevideopreviewlayer alloc] initwithsession:_capturesession];<br>[_videopreviewlayer setvideogravity:avlayervideogravityresizeaspectfill];<br>[_videopreviewlayer setframe:_viewpreview.layer.bounds];<br>[_viewpreview.layer addsublayer:_videopreviewlayer];<br><br><br>Start Video Capture.<br>[_capturesession startrunning];<br>_isreading = YES;<br>Return YES;<br>}<br><br><br>-(void) stopreading{<br>Stop video capture and make the capture session object Nil.<br>[_capturesession stoprunning];<br>_capturesession = nil;<br><br>Remove the video preview layer from the Viewpreview view ' s layer.<br>[_videopreviewlayer removefromsuperlayer];<br>_isreading = NO;<br>[self openqtlayer];<br>}<br><br>#pragma mark-avcapturemetadataoutputobjectsdelegate method implementation<br><br>-(void) captureoutput: (avcaptureoutput *) captureoutput didoutputmetadataobjects: (nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{<br><br>Check if the metadataobjects array is not nil and it contains at least one Object.<br>If (metadataobjects! = nil && [metadataobjects count] > 0) {<br>Get the metadata Object.<br>Avmetadatamachinereadablecodeobject *metadataobj = [metadataobjects objectatindex:0];<br>If ([[[metadataobj type] isequaltostring:avmetadataobjecttypeqrcode]) {<br>If the found metadata is equal to the QR code metadata then update the status label ' s text,<br>Stop reading and change the bar button item ' s title and the flag ' s Value.<br>Everything is do on the main thread.<br>[delegate Scanresult:[metadataobj stringvalue]; Send scan result to QT Show<br><br>[self performselectoronmainthread: @selector (stopreading) withobject:nil waituntildone:no];<br><br>_isreading = NO;<br>}<br>}<br>}<br><br>@end</em></em></em><pre><pre><em id="__mceDel"><span style="color: #000000;"></span></em></pre></pre><p><p>OK. That's Probably the process, and the addition of xib files will not be Introduced.</p></p><p><p></p></p><p><p>Qt for iOS scan QR code feature</p></p></span>

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.