#import "HHBarcodeViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface HHBarcodeViewController ()
@property (nonatomic , strong ) AVCaptureSession *session;
@property (nonatomic , weak ) AVCaptureVideoPreviewLayer *previewLayer;
@end @implementation HHBarcodeViewController - (void )viewDidLoad {[super viewDidLoad];CADisplayLink *disp = [CADisplayLink displayLinkWithTarget:self selector:@selector (update)];[disp addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];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()];AVCaptureSession *session = [[AVCaptureSession alloc] init];self .session = session;if ([session canAddInput:input]) {[session addInput:input];}if ([session canAddOutput:output]) {[session addOutput:output];}[output setMetadataObjectTypes:@[AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeITF14Code,AVMetadataObjectTypeDataMatrixCode,]];AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];previewLayer.frame = self .view .bounds ;self .previewLayer = previewLayer;[self .view .layer insertSublayer:previewLayer atIndex:0 ];[_session startRunning];}#pragma 代理
- (void )captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{if (metadataObjects.count > 0 ) {[self .session stopRunning];[self .previewLayer removeFromSuperlayer];AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];HHLog(@"%@" , obj.stringValue );}}- (void )update{_barcodeConsY.constant += 3 ;if (_barcodeConsY.constant >= 224 ){_barcodeConsY.constant = 0 ;}
}- (void )didReceiveMemoryWarning {[super didReceiveMemoryWarning];
}
@end