Before a lot of QR code scanning is based on zxing do, but zxing used to be really troublesome, and has not been updated. With the iOS6 out of the historical stage, finally can use the iOS7 after, with the system of Avfoundation to do the two-dimensional code scanner.
Initialize camera, scanner
- (void) setupcamera{Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{Time-consuming operationsDevice _device = [Avcapturedevice Defaultdevicewithmediatype:Avmediatypevideo];Input _input = [Avcapturedeviceinput Deviceinputwithdevice:Self. Device Error:NIL];Output _output = [[Avcapturemetadataoutput Alloc]init];[_output setmetadataobjectsdelegate:self Queue:dispatch_get_main_queue ()]; [_output setmetadataobjectsdelegate:Self Queue:dispatch_get_main_queue ()];Session _session = [[Avcapturesession Alloc]init]; [_session Setsessionpreset:Avcapturesessionpresethigh];if ([_session canaddinput:Self. Input]) {[_session addinput:Self. input]; }if ([_session canaddoutput:Self. Output]) {[_session addoutput:Self. Output]; }Barcode Type Avmetadataobjecttypeqrcode _output. metadataobjecttypes [Email protected][Avmetadataobjecttypeqrcode];Dispatch_async (Dispatch_get_main_queue (), ^{//Update interface //Preview _preview =[ Avcapturevideopreviewlayer layerwithsession:self.session ]; _preview.videogravity = Avlayervideogravityresizeaspectfill; //_preview.frame =cgrectmake (20,110,280,280); _previewself.view.bounds; [self.view.layer Insertsublayer:self.preview AtIndex: 0]; //Start [_session startrunning];}); });}
Optimize the session in Viewwillappear and Viewwilldisappear.
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if (_session && ![_session isRunning]) { [_session startRunning]; } timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [timer invalidate];}
The timer above is a timer for scanning animations and can be skipped over.
Processing the results of a scan
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ NSString *stringValue; if ([metadataObjects count] >0) { AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0]; stringValue = metadataObject.stringValue; } [_session stopRunning]; [timer invalidate]; NSLog(@"%@",stringValue);}
Sweep your QR code with QR code scanner
NSString *url = [NSURL URLWithString:@"html/judgement.html" relativeToURL:[ZXApiClient sharedClient].baseURL].absoluteString; if ([stringValue hasPrefix:url]) { //如果扫出来的url是自己的域名开头的,那么做如下的处理。 }
Sweep someone else's QR code with QR code scanner
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:stringValue]];
Open the URL directly using the browser that comes with the OpenURL system, or you can write a built-in browser to open it.
Sweep your QR code with someone else's scanner
First, you define your own QR code as the http://www.xxx.com/xxxxx
URL of your own domain name.
Then the third-party QR code is swept out, will jump to this URL.
Next, deploy this page on the server and add the following code
<ScriptLanguage="JavaScript" > if (Navigator.userAgent.match (/(Iphone|ipod|ipad);? /i)) {var loaddatetime =NewDate ();Window.settimeout (function) {var timeoutdatetime =NewDate ();if (Timeoutdatetime-loaddatetime <5000) {Window.location ="Page URL to jump"; }else {window.close (); }, 25); window.location = "test://";} else if (Navigator.userAgent.match ( /android/i)) {var state = null; try {state = window.open ( "apps Custom URL schemes ", ' _blank ');} catch (e) {} if (state) { Window.close (); } else {window.location = " The URL of the page to jump "; }}</SCRIPT>
This code is based on the principle of URL schemes, if your app has this URL schemes (in the example is test://), then will open the URL immediately, if not exist, it will be more than 25 milliseconds, then point to another page, usually the download page.
Next, set in the app's URL schemes, such as test
Paste_image.png
This time, when the browser makes the test://
request, you can open the app immediately.
Finally, if you are not satisfied with scanning QR code can only open the app, want to do something about the content of the QR code, you can:
- The content of the QR code is defined
http://www.xxx.com/xxxxx?uid=xxx
so that, of course, the following parameters need to be encrypted.
- In the JS code to get this parameter, and appended to the URL schemes, such as
test://uid=xxx
.
- Add the following code to the Appdelegate.
-(bool) Application: (uiapplication *) application Handleopenurl: (NSURL *) url{if ([Url.absolutestring hasprefix:@ " Test://uid= "]) {nsstring *uid = [Url.absoluteString Substringfromindex:11]; nslog (@ "uid=%@", UID); //UID Operation} else {//other places to throw URLs, such as return [ Wxapi handleopenurl:url delegate:self]; } return yes;}
Sweep someone else's QR code with someone else's scanner.
34ebbc5ccf91e9deffe7f8d1fead2675.png last one.
2015-04-08 09_52_30.gif
iOS Sweep scan feature development