http://www.bubuko.com/infodetail-1142586.html
http://blog.csdn.net/lc_obj/article/details/41549469?utm_source=tuicool&utm_medium=referral
http://my.oschina.net/u/2340880/blog/405847
https://github.com/lcddhr/QRWeiXinDemo
使用 AVFoundation系統庫來進行二維碼掃描并且限制掃描二維碼的範圍。(因為預設的是全屏掃描)
-(void)beginCode
{
//1.攝像頭裝置
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//2.設定輸入
NSError *error = nil;
AVCaptureInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (error) {
NSLog(@"沒有攝像頭%@",error);
return;
}
//3.設定輸出(Metdata中繼資料)
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
//3.1輸出的代理 捕獲二維碼的圖像
//dispatch_get_main_queue()使用主線程隊列,響應比較同步,使用其他隊列響應不同步。
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//3.2 設定掃描區域 預設是全屏掃描
//這個CGRectMake(Y,X,H,W) 1代表最大值 原點是導航右下角 為起始點
// [output setRectOfInterest:CGRectMake(0, 0.5, 0.5, 0.5)];//左上角 1/4 螢幕
// [output setRectOfInterest:CGRectMake(0.5, 0.5, 0.5, 0.5)];//左下角 1/4 螢幕
// [output setRectOfInterest:CGRectMake(0.5, 0, 0.5, 0.5)]; //右下角 1/4 螢幕
// [output setRectOfInterest:CGRectMake(0, 0, 0.5, 0.5)]; //右上角 1/4 螢幕
// [output setRectOfInterest:CGRectMake((124)/ScreenHigh, ((ScreenWidth220)/2)/ScreenWidth,220/ScreenHigh,220/ScreenWidth)]; //設定自定義像素點的 位置
[output setRectOfInterest:CGRectMake(0.25,0.25, 0.5, 0.5)]; //貌似 中間的感覺!!!
//4.拍攝會話
AVCaptureSession *session = [[AVCaptureSession alloc] init];
_session = session;
//添加session的輸入和輸出
[session addInput:input];
[session addOutput:output];
//4.1設定輸出的格式
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//5.設定預覽圖層 (讓使用者看到掃描結果)
AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:session];
_previewLayer = preview;
//5.1設定preview的屬性
preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
//5.2設定preview的圖層的大小
[preview setFrame:self.view.bounds];
//5.3将圖層添加到視圖的圖層
[self.view.layer insertSublayer:preview atIndex:0];
//6.啟動會話
[session startRunning];
}
#pragma mark 輸出的代理方法
//此方法是在識别到QRCode,并且完成轉換
//如果QRCode的内容越大,轉換需要的時間就越長
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//會頻繁的掃描
//如果掃描完成就停止
[_session stopRunning];
//删除預覽的圖層
[_previewLayer removeFromSuperlayer];
//設定界面顯示掃描結果
NSString *reslutStr = [[NSString alloc] init];
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
reslutStr = [obj stringValue];//這個就是 掃描的結果
//如果需要對URL 名片 等資訊進行掃描 再次進行擴充
}
NSLog(@"%@",metadataObjects);
}