二维码+遮罩
首先是要用到的几个类
@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;
下面分别创建他们
// Device
_device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
_input = [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];
// Output
_output = [[AVCaptureMetadataOutputalloc]init];
[_outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
// Session
_session = [[AVCaptureSessionalloc]init];
[_sessionsetSessionPreset:AVCaptureSessionPresetHigh];
if ([_sessioncanAddInput:self.input])
{
[_sessionaddInput:self.input];
}
if ([_sessioncanAddOutput:self.output])
{
[_sessionaddOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
// Preview
_preview =[AVCaptureVideoPreviewLayerlayerWithSession:_session];
_preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
_preview.frame =self.view.layer.bounds;
[self.view.layerinsertSublayer:_previewatIndex:0];
// Start
[_sessionstartRunning];
然后实现 AVCaptureMetadataOutputObjectsDelegate
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//判断是否有数据
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
// NSLog(@"------ 二维码数据 %@ %@",[metadataObj stringValue],metadataObjects);
// NSDictionary *dic = (NSDictionary *)[metadataObj stringValue];
NSData *jsonData = [[metadataObj stringValue] dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
NSLog(@"------ 二维码数据 dic %@",dic);
NSLog(@"------ 二维码数据 code %@",[dic objectForKey:@"code"]);
ScanManager *manager = [ScanManager manager];
manager.tableDic = dic;
NSLog(@" ======= tableDic %@",manager.tableDic);
//判断回传的数据类型
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
_isReading = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
-(void)stopReading
{
[_session stopRunning];
[_scanImg removeFromSuperview];
}
-------------------------- 遮罩
#import <UIKit/UIKit.h>
#define kMaskViewBorderWidth 2.0f
@interface MaskView : UIView
@property (unsafe_unretained, nonatomic)CGRect cropRect;
@property (strong, nonatomic) UIColor *borderColor;
@end
#import "MaskView.h"
@implementation MaskView
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.4);
CGContextFillRect(ctx, self.bounds);
CGContextSetStrokeColorWithColor(ctx, self.borderColor.CGColor);
CGContextStrokeRectWithWidth(ctx, _cropRect, kMaskViewBorderWidth);
CGContextClearRect(ctx, _cropRect);
}
@end