// 1、获取摄像设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2、创建设备输入流
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
// 3、创建数据输出流
_scanOutput = [[AVCaptureMetadataOutput alloc] init]; [_scanOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//4、创建会话对象
_scanSesion = [[AVCaptureSession alloc] init];
[_scanSesion setSessionPreset:AVCaptureSessionPresetHigh];
if ([_scanSesion canAddInput:deviceInput])
{ [_scanSesion addInput:deviceInput]; }
if ([_scanSesion canAddOutput:self.scanOutput])
{ [_scanSesion addOutput:self.scanOutput]; }
//5、设置数据输出类型 //5(1).扫描二维码跟条形码
// _scanOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
//5(1).只扫描二维码 _scanOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
CGFloat X = 0.22;
CGFloat Y = 0.45 - 0.28*([UIScreen mainScreen].bounds.size.width/[UIScreen mainScreen].bounds.size.height);
CGFloat W = 0.56;
CGFloat H = 0.56*([UIScreen mainScreen].bounds.size.width/[UIScreen mainScreen].bounds.size.height);
//5(2)、设置扫描范围 ps:不知道为什么要把X,Y 和W,H交换才能得到自己想要的结果 经过多次测试得出结论X,Y 和W,H要交换
[_scanOutput setRectOfInterest:CGRectMake(Y, X, H, W)];
//6、添加扫描画面
_scanPreviewlayer = [AVCaptureVideoPreviewLayer layerWithSession:_scanSesion];
_scanPreviewlayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_scanPreviewlayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
[self.view.layer insertSublayer:_scanPreviewlayer atIndex:0];
//7、开始扫描
[_scanSesion startRunning];
#pragma mark - - - AVCaptureMetadataOutputObjectsDelegate 代理方法
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if ([metadataObjects count] >0){
//停止扫描
[_scanSesion stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue; //stringValue是扫描拿到的内容。
[_scanPreviewlayer removeFromSuperlayer];
NSLog(@"扫描的结果 = %@",stringValue);
}
}
以上就是扫描二维码的核心代码,下面是打开手电筒的代码,还有一个是另外封装的扫描框的scanView,由于时间关系就没有传了,嘻嘻☺️,因为下班了。
#pragma mark 打开手电筒的方法
- (void)turnLightBtnPressed:(UIButton *)sender{
UIButton *light = (UIButton *)sender; light.selected = !light.selected; //切换手电筒按钮的图片 因为在创建button的时候已经设置好了图片,所以只要切换状态就可以切换图片
如果你要获取的类不存在,则会返回一个nil对象,程序不会崩溃,适用于进行你不确定类的初始化。 Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); //因为oc中的类是动态加载的,所以如果你的类中没有某个头文件定义,而你确信这个类是可以用的,那么可以用这个方法。
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) { // 判断是否有闪光灯
// 请求独占访问硬件设备
[device lockForConfiguration:nil];
if (sender.tag == 1) {
sender.tag = 2;
[device setTorchMode:AVCaptureTorchModeOn]; // 手电筒开
}else{
sender.tag = 1;
[device setTorchMode:AVCaptureTorchModeOff]; // 手电筒关
}
// 请求解除独占访问硬件设备
[device unlockForConfiguration];
}
}
网友评论