沒有用三方蚌卤,因?yàn)橐{(diào)用攝像頭陨晶,所以首先就是導(dǎo)入#import <AVFoundation/AVFoundation.h>框架锅减。下面是要用到的幾個(gè)最主要的:
// 攝像頭
@property (nonatomic, strong) AVCaptureDevice *device;
// 設(shè)備輸入
@property (nonatomic, strong) AVCaptureDeviceInput *input;
// 設(shè)備輸出
@property (nonatomic, strong) AVCaptureMetadataOutput *output;
// 創(chuàng)建會(huì)話對(duì)象
@property (nonatomic, strong) AVCaptureSession *session;
// 呈現(xiàn)數(shù)據(jù)的圖層
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
然后是初始化這些屬性印屁,我寫的是一個(gè)簡易的demo审胸,所以全部寫在viewDidLoad里面的,因?yàn)橐{(diào)用AVCaptureMetadataOutput的一個(gè)代理方法所以要遵守<AVCaptureMetadataOutputObjectsDelegate>代理協(xié)議献起。
// 初始化
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
_output = [[AVCaptureMetadataOutput alloc] init];
// 設(shè)置好掃描成功回調(diào)代理以及回調(diào)線程<AVCaptureMetadataOutputObjectsDelegate>
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
_session = [[AVCaptureSession alloc] init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];// 高質(zhì)量采集
// 加入輸入輸出對(duì)象
if ([_session canAddInput:self.input]) {
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output]) {
[_session addOutput:self.output];
}
// 設(shè)置掃描的條碼類型
_output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
//增加條形碼掃描
// _output.metadataObjectTypes = @[AVMetadataObjectTypeEAN13Code,
// AVMetadataObjectTypeEAN8Code,
// AVMetadataObjectTypeCode128Code,
// AVMetadataObjectTypeQRCode];
_previewLayer =[AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity =AVLayerVideoGravityResize;
_previewLayer.frame =self.view.layer.bounds;
[self.view.layer insertSublayer:_previewLayer atIndex:0];
// 創(chuàng)建一個(gè)view展示
UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
view.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
view.backgroundColor = [UIColor clearColor];
[self.view addSubview:view];
//修正掃描區(qū)域
CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;
CGRect cropRect = CGRectMake((screenWidth - 200) / 2,(screenHeight - 200) / 2,200,200);
[_output setRectOfInterest:CGRectMake(cropRect.origin.y / screenHeight,
cropRect.origin.x / screenWidth,
cropRect.size.height / screenHeight,
cropRect.size.width / screenWidth)];
// 開啟會(huì)話
[_session startRunning];
下面就是獲得掃描的結(jié)果铭腕,上面提到的遵守的<AVCaptureMetadataOutputObjectsDelegate>協(xié)議里面有一個(gè)代理方法柿究,能夠得到掃描的結(jié)果
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSString *stringValue;
if ([metadataObjects count] >0)
{
//停止掃描
// [_session stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
}
stringValue就是掃描后得到的一個(gè)網(wǎng)址券时,是一個(gè)網(wǎng)址字符串孤里。拿到字符串后通過自己的需求就可以將鏈接打開。我是用的系統(tǒng)的#import <SafariServices/SafariServices.h>創(chuàng)建的一個(gè)控制器跳轉(zhuǎn)過去的橘洞。如果你想跳轉(zhuǎn)后返回掃描界面還能掃描捌袜,那個(gè)停止掃描就不要,不是你返回去就不能再次掃描炸枣。