标题:
iOS利用AVCaptureSession实现二维码扫描(2)
[打印本页]
作者:
look_w
时间:
2019-3-4 21:02
标题:
iOS利用AVCaptureSession实现二维码扫描(2)
4 开始扫描
#pragma mark 开始
- (void)startRunning {
if (self.captureSession) {
self.isReading = YES;
[self.captureSession startRunning];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector
selector(moveUpAndDownLine) userInfo:nil repeats: YES];
}
}
5 扫描线移动
- (void)moveUpAndDownLine {
CGRect frame = self.scanLayer.frame;
if (_boxView.frame.size.height < self.scanLayer.frame.origin.y) {
frame.origin.y = 0;
self.scanLayer.frame = frame;
} else {
frame.origin.y += 5;
[UIView animateWithDuration:0.2 animations:^{
self.scanLayer.frame = frame;
}];
}
}
6 AVCaptureMetadataOutputObjectsDelegate
这个是里面用到的最重要的方法,在这个代理方法里我们可以获得扫描二维码解析出来的数据,然后在里面进行操作
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput
AVCaptureOutput *)output didOutputMetadataObjects
NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection
AVCaptureConnection *)connection {
//判断是否有数据
if (!_isReading) {
return;
}
if (metadataObjects.count > 0) {
_isReading = NO;
AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects[0];
NSString *result = metadataObject.stringValue;
if (self.resultBlock) {
self.resultBlock(result?
"");
}
[self.navigationController popViewControllerAnimated:NO];
}
}
7 退出时记得关闭
#pragma mark 结束
- (void)stopRunning {
if ([_timer isValid]) {
[_timer invalidate];
_timer = nil ;
}
[self.captureSession stopRunning];
[_scanLayer removeFromSuperlayer];
[_videoPreviewLayer removeFromSuperlayer];
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0