您的位置:首页 > 移动开发 > IOS开发

iOS 7 What’s New in AV Foundation之二维码扫描(中)

2013-10-18 20:21 447 查看
接上文

英语不是很好有些专业的东西还是不给翻译了 自己百度吧

没有太多难懂的东西 慢慢来

对了 声明一下 这两篇的内容都是官方文档上弄来的 鸟悄的啊 我也是花钱买的 我是觉得写得真心好才共享给大家的

Detecting machine readable codes

In addition to processing and displaying video, AV Foundation detects and decodesa comprehensive list of 1-D and 2-D barcodes:

1. QR code2. Aztec

3. EAN134. EAN8

5. UPC-E

6. PDF417

7. Code 93

8. Code 39

9. Code 39 mod 43

You have probably seen some of these codes in action, even though you may nothave known their names. EAN13 is a common standard in Europe for markingitems,
whereas UPC-E is favored in the United States. PDF417 is used by some mailservices and is also the standard for airline boarding passes.

However, the QR code is probably pretty familiar to mobile users; that’s the type ofcode you’ll read in your app.

OpenViewController.mand
add the following instance variable to theimplementation block:

AVCaptureMetadataOutput*_metadataOutput;

AVCaptureMetadataOutputprovides
a callback to the application when metadata isdetected in a video frame. AV Foundation supports two types of metadata: machinereadable codes and face detection.

You need a way to capture and process that metadata. Add the following code tothe end ofsetupCaptureSessioninViewController.m:

_metadataOutput= [[AVCaptureMetadataOutputalloc]init];

dispatch_queue_tmetadataQueue =dispatch_queue_create("com.razeware.ColloQR.metadata",0); 

[_metadataOutputsetMetadataObjectsDelegate:selfqueue:metadataQueue];

if([_captureSessioncanAddOutput:_metadataOutput])
{[_captureSessionaddOutput:_metadataOutput];

}

Here you initialize the output with a delegate and then add it to the session. Just asyou did with the
inputs, you need to query canAddOutput:first
to see if it’s okay toadd an output to your session.

AV Foundation is designed for high throughput and low latency; therefore anyprocessing and analysis tasks should be moved off of the main thread if
at allpossible.

Similar to the delegate object,AVCaptureMetadataOutputrequires
that you providea dispatch queue to make delegate callbacks. This frees the media subsystem tocontinue processing frames from the camera. If callbacks were executed in asynchronous manner, a long-running delegate callback would block frameprocessing until
it was complete.

This might not be an issue in a simple one-input one-output system. However,asynchronous processing of callbacks is key in a multi-output system so
that long-running callbacks on one output don’t cause frame drops in other outputs.

InViewController.m,
modify the class continuation category declaration as shownbelow:

@interfaceViewController()<AVCaptureMetadataOutputObjectsDelegate> 

Next, add the following method toViewController.mto
implement the delegatedeclared:

- (void)captureOutput:(AVCaptureOutput*)captureOutputdidOutputMetadataObjects:(NSArray*)metadataObjects

fromConnection:(AVCaptureConnection*)connection

{
[metadataObjects enumerateObjectsUsingBlock:^(AVMetadataObject*obj,NSUIntegeridx,BOOL*stop){

NSLog(@"Metadata:
%@", obj);}];



 

The metadata output calls the above method every time it detects new metadata.At this point, the code simply logs each piece of metadata as it’s detected.
This issufficient for testing purposes; you’ll flesh out the rest of this implementation later.

There’s one last tweak before you can build and run. Add the following line tostartRunninginViewController.m,immediately
underneath the line[_captureSession startRunning];: 

_metadataOutput.metadataObjectTypes=_metadataOutput.availableMetadataObjectTypes; 

This sets the types of metadata in which your app is interested; in this case you’lldetect all available types of metadata.

Build and run your app; pass your camera over the following QR code while keepingan eye on the Xcode console: 

                                      


Once your device processes the QR code above, you should see the following outputdisplayed:

2013-07-03 21:57:03.758 ColloQR[1262:1303] Metadata:<AVMetadataMachineReadableCodeObject: 0x15e515d0> type"org.iso.QRCode", bounds { 0.2,0.1 0.5x0.8 },
corners { 0.2,0.90.2,0.1 0.7,0.1 0.7,0.9 }, time 31399588077583, stringValue "W00t! AQR code!"

There you are — your first scanned QR code. Feel free to try this out on any otherQR codes nearby, or perform a quick Internet search.

Look closely at the output above and you’ll see the metadata includes moreinformation than just the contents of the code instringValue,
including thebounds and corners of the QR image. 

You’re not done yet! Many code readers provide visual feedback to the user toindicate if the code is properly positioned and capable of being read; next
you’ll usethe bounds and corner metadata to draw an overlay on the camera view to achievethis.

下一篇点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 7 二维码