#import "SaoYiSaoController.h"
#import <AVFoundation/AVFoundation.h>
#import "SaoMaTiaoZhuanController.h"
@interface SaoYiSaoController ()<AVCaptureMetadataOutputObjectsDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic ,strong) AVCaptureSession *captureSession;
@property (nonatomic ,strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@end
@implementation SaoYiSaoController
- (void)viewWillAppear:(BOOL)animated{
//开始扫码
[self.captureSession startRunning];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
[self createSaoMiaoErWeiMa];
UIBarButtonItem *ri = [[UIBarButtonItem alloc]initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(xiangCe)];
[self.navigationItem setRightBarButtonItem:ri];
//创建扫码框
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth/2-100, kHeight/2-100, 200, 200)];
imgV.image = [UIImage imageNamed:@"5b2c3a1c9b1488596cc1faf53c35fbd0.png"];
[self.view addSubview:imgV];
}
- (void)createSaoMiaoErWeiMa{
//初始化捕捉设备AVCaptureDevice
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];
if (!input) {
return;
}
//创建输出
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
//创建一个会话,并添加输入和输出
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:input];
[self.captureSession addOutput:captureMetadataOutput];
//设置为二维码类型
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
//创建图层,摄像头捕捉到的画面都会在这个图层显示
self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
//设置图层填充方式
[self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
// 设置图层的frame
[self.videoPreviewLayer setFrame:self.view.layer.bounds];
//将图层添加到预览view的图层上
[self.view.layer addSublayer:self.videoPreviewLayer];
//创建一个串行队列,并设置代理
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
//设置扫码范围
captureMetadataOutput.rectOfInterest = CGRectMake((kHeight/2-100)/kHeight,
kWidth/2/kHeight,
200/kHeight,
200/kWidth);
}
#pragma mark - 扫码完成后调用此方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
NSString *str;
//判断是否有数据
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
//判断回传的数据类型
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
str = [metadataObj stringValue];
}
//跳转
[self pushC:str];
//结束捕获
[self.captureSession stopRunning];
}
}
#pragma mark - 打开系统相簿
- (void)xiangCe{
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
//设置控制器类型系统相簿
photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
photoPicker.view.backgroundColor = [UIColor whiteColor];
[self presentViewController:photoPicker animated:YES completion:NULL];
}
//选取照片/视频或拍照完成完成之后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//退出相簿控制器
[picker dismissViewControllerAnimated:NO completion:nil];
NSString *str;
//取出选中的图片
UIImage *srcImage = [info objectForKey:UIImagePickerControllerOriginalImage];
//转换为CIImage
CIImage *ciIma = [CIImage imageWithCGImage:srcImage.CGImage];
//创建探测器
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
NSArray *ary = [detector featuresInImage:ciIma];
//取出探测到的数据
for (CIQRCodeFeature *fe in ary) {
str = fe.messageString;
}
[self pushC:str];
}
#pragma mark - 跳转控制器
- (void)pushC:(NSString *)str{
//跳转控制器
self.hidesBottomBarWhenPushed = YES;
SaoMaTiaoZhuanController *smtz = [[SaoMaTiaoZhuanController alloc]init];
smtz.url = str;
dispatch_sync(dispatch_get_main_queue(), ^{
[self.navigationController pushViewController:smtz animated:YES];
});
}
iOS 原生二维码扫描和扫描相簿中的二维码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 对于现在的App应用来说,扫描二维码这个功能是再正常不过的一个功能了,在早期开发这些功能的时候,大家或多或少的都接...
- 表白? 一定有人会说,痴心妄想,你们程序员哪来的对象! 然而,作为程序员,给我一个类,我可以创建无数个对象。 在i...