扫描二维码

#import "BZViewController.h"
#import
<AVFoundation/AVFoundation.h>
@interface BZViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (strong,nonatomic)AVCaptureDevice *device;
@property (strong,nonatomic)AVCaptureDeviceInput *input;//调用所有的输入硬件。例如摄像头和麦克风
@property (strong,nonatomic)AVCaptureMetadataOutput *output;
@property (strong,nonatomic)AVCaptureSession *session;// 控制输入和输出设备之间的数据传递
@property (strong,nonatomic)AVCaptureVideoPreviewLayer *preview;//镜头捕捉到得预览图层
@end

@implementation BZViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    view.
backgroundColor = [UIColor greenColor];
    [
self.view addSubview:view];
   
   
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 40)];
    button.
backgroundColor = [UIColor redColor];
    [button
setTitle:@"btn" forState:UIControlStateNormal];
    [button
addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [view
addSubview:button];
}

- (
void)btnClick{
   
// Device
   
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
   
   
// Input
   
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
   
   
// Output
   
self.output = [[AVCaptureMetadataOutput alloc]init];
    [
self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
   
   
// Session
   
self.session = [[AVCaptureSession alloc]init];
    [
self.session setSessionPreset:AVCaptureSessionPresetHigh];
   
if ([self.session canAddInput:self.input])
    {
        [
self.session addInput:self.input];
    }
   
if ([self.session canAddOutput:self.output])
    {
        [
self.session addOutput:self.output];
    }
   
   
// 条码类型
   
self.output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
   
   
// Preview
   
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
   
self.preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
   
self.preview.frame =CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
    [
self.view.layer addSublayer:self.preview];
   
   
// Start
    [
self.session startRunning];
}

#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
   
NSString *stringValue;
   
   
if ([metadataObjects count] >0) {
       
AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.
stringValue;
    }
   
    [
_session stopRunning];
   
NSLog(@"%@",stringValue);
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值