IOS7原生API進行二維碼條形碼的掃描

cyjjkz1 8年前發布 | 2K 次閱讀 Objective-C IOS

//需要真機

import "ViewController.h"

import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//用于處理采集信息的代理 {     AVCaptureSession * session;//輸入輸出的中間橋梁 } @end @implementation ViewController

- (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view, typically from a nib.     //獲取攝像設備     AVCaptureDevice  device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];     //創建輸入流     AVCaptureDeviceInput  input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];     //創建輸出流     AVCaptureMetadataOutput  output = [[AVCaptureMetadataOutput alloc]init];     //設置代理 在主線程里刷新     [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];          //初始化鏈接對象     session = [[AVCaptureSession alloc]init];     //高質量采集率     [session setSessionPreset:AVCaptureSessionPresetHigh];      //    1、這個CGRect參數和普通的Rect范圍不太一樣,它的四個值的范圍都是0-1,表示比例。 //    2、經過測試發現,這個參數里面的x對應的恰恰是距離左上角的垂直距離,y對應的是距離左上角的水平距離。 //    3、寬度和高度設置的情況也是類似。 //    3、舉個例子如果我們想讓掃描的處理區域是屏幕的下半部分,我們這樣設置 //    output.rectOfInterest=CGRectMake(0.5,0,0.5, 1);          [session addInput:input];     [session addOutput:output];     //設置掃碼支持的編碼格式(如下設置條形碼和二維碼兼容)     output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];          //創建攝像頭顯示圖層     AVCaptureVideoPreviewLayer  layer = [AVCaptureVideoPreviewLayer layerWithSession:session];     layer.videoGravity=AVLayerVideoGravityResizeAspectFill;     layer.frame=self.view.layer.bounds;     [self.view.layer insertSublayer:layer atIndex:0];     //開始捕獲     [session startRunning]; }

pragma mark 信息捕獲代理方法

-(void)captureOutput:(AVCaptureOutput )captureOutput didOutputMetadataObjects:(NSArray )metadataObjects fromConnection:(AVCaptureConnection )connection{     if (metadataObjects.count>0) {         //[session stopRunning];         AVMetadataMachineReadableCodeObject  metadataObject = [metadataObjects objectAtIndex : 0 ];         //輸出掃描字符串         NSLog(@"%@",metadataObject.stringValue);     } }

@end</pre>


 本文由用戶 cyjjkz1 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!