在iOS7以前,还没有系统自带的二维码扫描的SDK,那时候搞个二维码扫描复杂的要死,用的那些第三方库,什么zxing,zbar,既麻烦,又不好用。不过现在不同了,iOS7以后,已经有了自带的条形码和二维码扫描。
下面我直接上代码,非常简单。
先是.h文件:
#import <UIKit/UIKit.h>
@import AVFoundation;
@interface ViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
{
AVCaptureDevice *_device;
AVCaptureDeviceInput *_input;
AVCaptureMetadataOutput *_output;
AVCaptureSession *_session;
AVCaptureVideoPreviewLayer *_preview;
}
@end
.m文件:
#import "ViewController.h"
// 屏幕宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
// 屏幕高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
_output = [[AVCaptureMetadataOutput alloc] init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// _output.rectOfInterest = CGRectMake (( 124 )/ ScreenHeight ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHeight , 220 / ScreenWidth );
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetHigh;
if ([_session canAddInput:_input]) {
[_session addInput:_input];
}
if ([_session canAddOutput:_output]) {
[_session addOutput:_output];
}
_output.metadataObjectTypes = [NSArray arrayWithObjects:AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypeInterleaved2of5Code,
AVMetadataObjectTypeITF14Code,
AVMetadataObjectTypeDataMatrixCode, nil];
_preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame = self.view.layer.bounds;
[self.view.layer insertSublayer:_preview atIndex:0];
[_session startRunning];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"more_qr"];
[self.view addSubview:imageView];
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
NSString *stringValue;
if ([metadataObjects count] > 0) {
[_session stopRunning];
AVMetadataMachineReadableCodeObject *metadataCode = [metadataObjects objectAtIndex:0];
NSLog(@"%@",metadataCode);
stringValue = metadataCode.stringValue;
NSLog(@"扫描得到的:%@",stringValue);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
再来解释一下上面的代码吧。
在这里面,我把所有的扫码类型都加入进去了,其中
AVMetadataObjectTypeUPCECode、AVMetadataObjectTypeCode39Code、AVMetadataObjectTypeCode39Mod43Code、AVMetadataObjectTypeEAN13Code、AVMetadataObjectTypeEAN8Code、AVMetadataObjectTypeCode93Code、AVMetadataObjectTypeCode128Code、AVMetadataObjectTypePDF417Code、AVMetadataObjectTypeInterleaved2of5Code、AVMetadataObjectTypeITF14Code这些都属于不同的条形码类型,而AVMetadataObjectTypeQRCode、AVMetadataObjectTypeAztecCode、AVMetadataObjectTypeDataMatrixCode这几种属于二维码类型的。
_output.rectOfInterest = CGRectMake (( 124 )/ ScreenHeight ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHeight , 220 / ScreenWidth );
这是设置扫码框的大小,默认是全屏的。