就几段代码就实现了,干嘛去用啥子三方啊
筹字数 把oc,swift一起写上了,要加什么动画效果再添加,,记到还有获取相机权限的操作处理
oc
#import "ScanViewController.h"
#import <AVFoundation/AVFoundation.h>
#import<AVFoundation/AVCaptureDevice.h>
#import<AVFoundation/AVMediaFormat.h>
@interface ScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic ,strong) AVCaptureSession *captureSession;
@property (nonatomic ,strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@end
@implementation ScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//获取摄像设备
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
//设置代理 在主线程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置识别区域
output.rectOfInterest = [self calculateScanRect];
//初始化链接对象
self.captureSession = [[AVCaptureSession alloc]init];
//高质量采集率
[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];
[self.captureSession addInput:input];
[self.captureSession addOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code];
self.videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
self.videoPreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
self.videoPreviewLayer.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
[self.view.layer insertSublayer:self.videoPreviewLayer atIndex:0];
//开始捕获
[self.captureSession startRunning];
}
-(CGRect)calculateScanRect {
CGSize previewSize = self.videoPreviewLayer.frame.size;
CGSize scanSize = CGSizeMake(previewSize.width * 3/4, previewSize.height * 3/4);
CGRect scanRect = CGRectMake((previewSize.width-scanSize.width)/2,
(previewSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
scanRect = CGRectMake(scanRect.origin.y/previewSize.height,
scanRect.origin.x/previewSize.width,
scanRect.size.height/previewSize.height,
scanRect.size.width/previewSize.width);
return scanRect;
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count>0) {
[self.captureSession stopRunning];//停止扫描
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
//输出扫描字符串
NSLog(@"%@",metadataObject.stringValue);
}
}
@end
然后是swift
/**
显示扫描视图
*/
private func showScanCode() {
if self.captureSession == nil {
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var input: AnyObject!
do {
input = try AVCaptureDeviceInput(device: captureDevice)
}
catch {
return
}
self.captureSession = AVCaptureSession()
self.captureSession?.addInput(input as! AVCaptureInput)
let captureMetadataOutput = AVCaptureMetadataOutput()
self.captureSession?.addOutput(captureMetadataOutput)
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code]
self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
self.videoPreviewLayer?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
captureMetadataOutput.rectOfInterest = self.calculateScanRect()
self.view.layer.addSublayer(self.videoPreviewLayer!)
// 放大
do {
try captureDevice!.lockForConfiguration()
} catch {
return
}
let maxZoomFactor = captureDevice!.activeFormat.videoMaxZoomFactor
let zoomFactor = maxZoomFactor < 2.0 ? maxZoomFactor : 2.0
captureDevice!.videoZoomFactor = zoomFactor
captureDevice!.unlockForConfiguration()
}
self.captureSession?.startRunning()
}
private func calculateScanRect() -> CGRect {
let previewSize: CGSize = self.videoPreviewLayer!.frame.size
let scanSize: CGSize = CGSizeMake(previewSize.width * 3/4, previewSize.height * 3/4)
var scanRect:CGRect = CGRectMake((previewSize.width-scanSize.width)/2,
(previewSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
// AVCapture输出的图片大小都是横着的,而iPhone的屏幕是竖着的,那么我把它旋转90°
scanRect = CGRectMake(scanRect.origin.y/previewSize.height,
scanRect.origin.x/previewSize.width,
scanRect.size.height/previewSize.height,
scanRect.size.width/previewSize.width);
return scanRect
}
// MARK: - AVCaptureMetadataOutputObjectsDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
if let metadatas = metadataObjects {
if metadatas.count > 0 {
let metadata = metadatas[0] as? AVMetadataMachineReadableCodeObject
if let barCode = metadata?.stringValue {
print("barCode: \(barCode)")
}
}
}
}
网友评论