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

接上文

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

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

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


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:

page625image17984

_metadataOutput= [[AVCaptureMetadataOutputalloc]init];

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

[_metadataOutputsetMetadataObjectsDelegate:selfqueue:metadataQueue];

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

}

page626image7184

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.


下一篇 点击打开链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值