#import "BaseViewController.h"
@interface ScanCodeVC : BaseViewController
@property (strong, nonatomic) CALayer *scanLayer;
@end
#import "ScanCodeVC.h"#import#import "UIViewController+Helper.h"@interface ScanCodeVC ()@property (nonatomic,strong) AVCaptureDevice *device;//设备
@property (nonatomic,strong) AVCaptureDeviceInput *input;//输入
@property (nonatomic,strong) AVCaptureMetadataOutput *output;//输出
@property (nonatomic,strong) AVCaptureSession *session;//桥梁
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;//摄像头显示图层
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UIView *lineView;
@end
@implementation ScanCodeVC
{
UIView * view;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setTitle:SRT_SCAN_CODE];
[self gennerateNavigationItemReturnBtn:@selector(returnClick)];
[self createInstanceView];//创建覆盖层
[self setCamera];
}
- (void)returnClick {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)createInstanceView
{
view = [[UIView alloc]initWithFrame:CGRectMake(UISCREEN_SIZE.width*0.31/2, UISCREEN_SIZE.height/2-125, UISCREEN_SIZE.width*0.69,250)];
view.backgroundColor = [UIColor clearColor];
view.layer.borderColor = COLOR_WHITE.CGColor;
view.layer.borderWidth = 0.5f;
[self.view addSubview:view];
UIImageView * imageV = [[UIImageView alloc]init];
imageV.image = [UIImage imageNamed:@"ic_scan"];
[view addSubview:imageV];
[imageV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(view.mas_left).offset(0);
make.right.equalTo(view.mas_right).offset(0);
make.top.equalTo(view.mas_top).offset(0);
make.bottom.equalTo(view.mas_bottom).offset(0);
}];
UIView *upView = [[UIView alloc] init];
upView.backgroundColor = COLOR_COVER_VIEW;
[self.view addSubview:upView];
[upView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset(0);
make.right.equalTo(self.view.mas_right).offset(0);
make.top.equalTo(self.view.mas_top).offset(0);
make.bottom.equalTo(view.mas_top).offset(0);
}];
UIView *leftView = [[UIView alloc] init];
leftView.backgroundColor =COLOR_COVER_VIEW;
[self.view addSubview:leftView];
[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset(0);
make.right.equalTo(view.mas_left).offset(0);
make.top.equalTo(upView.mas_bottom).offset(0);
make.height.equalTo(@(view.frame.size.height));
}];
UIView *rightView = [[UIView alloc] init];
rightView.backgroundColor = COLOR_COVER_VIEW;
[self.view addSubview:rightView];
[rightView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(view.mas_right).offset(0);
make.right.equalTo(self.view.mas_right).offset(0);
make.top.equalTo(upView.mas_bottom).offset(0);
make.height.equalTo(@(view.frame.size.height));
}];
UIView *downView = [[UIView alloc] init];
downView.backgroundColor = COLOR_COVER_VIEW;
[self.view addSubview:downView];
[downView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset(0);
make.right.equalTo(self.view.mas_right).offset(0);
make.top.equalTo(view.mas_bottom).offset(0);
make.bottom.equalTo(self.view.mas_bottom).offset(0);
}];
UILabel * alertLabel = [[UILabel alloc]init];
alertLabel.text = SRT_ALERT_SCAN_CODE_INFO;
alertLabel.textColor = COLOR_WHITE;
alertLabel.textAlignment = NSTextAlignmentCenter;
alertLabel.font = FONT_ROW_LABEL;
[downView addSubview:alertLabel];
[alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(downView.mas_left).offset(0);
make.right.equalTo(downView.mas_right).offset(0);
make.top.equalTo(downView.mas_top).offset(20);
make.height.equalTo(@30);
}];
}
- (void)setCamera
{
//Device
if(!self.device){
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
// Input
if (!self.input) {
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
}
// Output
if (!self.output) {
self.output = [[AVCaptureMetadataOutput alloc]init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
}
// Session
if (!self.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,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
// Preview
if(!self.previewLayer){
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.previewLayer.frame = self.view.frame;
[view.layer addSublayer:self.previewLayer];
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
}
//扫描线
_scanLayer = [[CALayer alloc] init];
_scanLayer.frame = CGRectMake(5, 0, view.frame.size.width-10, 1);
_scanLayer.backgroundColor = [UIColor whiteColor].CGColor;
[view.layer addSublayer:_scanLayer];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
[_timer fire];
[self.session startRunning];
}
- (void)moveScanLayer:(NSTimer *)timer
{
CGRect frame = _scanLayer.frame;
if (245 < _scanLayer.frame.origin.y) {
frame.origin.y = 0;
_scanLayer.frame = frame;
}else{
frame.origin.y += 5;
[UIView animateWithDuration:0.1 animations:^{
_scanLayer.frame = frame;
}];
}
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count>0) {
[self.session stopRunning];
[self.timer invalidate];
[_scanLayer removeFromSuperlayer];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
//输出扫描字符串
// _NumberString = metadataObject.stringValue;
//创建一个消息对象
NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"number":metadataObject.stringValue}];
//发送消息
[[NSNotificationCenter defaultCenter]postNotification:notice];
[self.navigationController popViewControllerAnimated:YES];
}
}
//注意,在界面消失的时候关闭session
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.session stopRunning];
[self.timer invalidate];
[_scanLayer removeFromSuperlayer];
}
网友评论