IOS scan function development, ios Scan
In the past, many QR code scans were based on zxing, but zxing was really troublesome and never updated. With iOS6 exiting the historical stage, you can use iOS7 and then use the system's AVFoundation QR code scanner.
Initialize the camera and scanner
-(Void) setupCamera {dispatch_async (idle, 0), ^ {// time-consuming operation // Device _ device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; // Input _ input = [AVCaptureDeviceInput deviceInputWithDevice: self. device error: nil]; // Output _ output = [[AVCaptureMetadataOutput alloc] init]; // [_ output setMetadataObjectsDelegate: self queue: 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];} // bar code AVMetadataObjectTypeQRCode _ output. metadataObjectTypes = @ [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); _ preview. frame = self. 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 above timer is a scanning animation timer, which can be skipped.
Process scan results
- (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);}
Scan your own QR code with a QR code scanner
NSString * url = [NSURL URLWithString: @ "html/judgement.html" relativeToURL: [ZXApiClient sharedClient]. baseURL]. absoluteString; if ([stringValue hasPrefix: url]) {// if the scanned url starts with your own domain name, perform the following processing. }
Scan others' QR codes with a QR code scanner
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:stringValue]];
Open the url directly using the browser that comes with the openUrl system, or write a built-in browser to open it.
Scan your QR code with someone else's Scanner
First, define your own QR codehttp://www.xxx.com/xxxxx
This is the url of your own domain name.
After the third-party QR code is scanned, it will jump to this URL.
Next, deploy this page on the server and add the following code:
<Script language = "javascript"> if (navigator. userAgent. match (/(iPhone | iPod | iPad );? /I) {var loadDateTime = new Date (); window. setTimeout (function () {var timeOutDateTime = new Date (); if (timeOutDateTime-loadDateTime <5000) {window. location = "URL of the page 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 = "URL of the page to jump" ;}</script>
This Code is based on the url schemes principle. If the url schemes exists in your app (test: // in this example), the url will be opened immediately. If it does not exist, it takes more than 25 milliseconds to direct to another page, which is generally a download page.
Then, set it in the url schemes of the app, such as test.
Paste_Image.png
At this time, the browser sendstest://
When the request is sent, the app can be opened immediately.
Finally, if you do not want to scan the QR code, you can only open the app and want to perform some operations on the content in the QR code, you can:
- Define the content of the QR code
http://www.xxx.com/xxxxx?uid=xxx
In this way, the following parameters must be encrypted.
- Obtain this parameter in the js Code and add it to the url schemes, as shown in figure
test://uid=xxx
.
- Add the following code to appDelegate.
-(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url {if ([url. absoluteString hasPrefix: @ "test: // uid ="]) {NSString * uid = [url. absoluteString substringFromIndex: 11]; NSLog (@ "uid = % @", uid); // perform operations on uid} else {// url thrown from other places, for example, return [WXApi handleOpenURL: url delegate: self];} return YES ;}
Scan other people's QR codes with another person's Scanner
34ebbc5ccf91e9deffe7f8d1fead2675.png
2015-04-08 09_52_30.gif