采集与相册二维码提取-iOS
//    [self leftNavButtonType:NavLeftButtonTypeBack];
[self basicConfig];
[self resumeAnimation];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupMaskView];
[self setupScanWindow];
[self beginScanning];
[kNotificationCenter addObserver:self
selector:@selector(resumeAnimation)
name:@"EnterForeground"
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//    self.navigationController.navigationBar.hidden = NO;
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)basicConfig {
//    self.tabBarController.hidesBottomBarWhenPushed = YES;
//    [self setupNavView]; // ⾃定义导航
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.anslucent = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]]                                                  forBarMetrics:UIBarMetricsDefault];
最新射击网游[self leftNavButtonType:NavButtonStyleBackWhite];
[self settingNavRightButton];
}
- (void)settingNavRightButton {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont systemFontOfSize:14.f];
[btn setTitle:@"相册" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClickSelPhoto:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = item;
}
- (void)btnClickSelPhoto:(UIButton *)btn {
NSLog(@"调⽤系统相册");
YHCameraManager *cameraMag = [YHCameraManager shareInstance];
cameraMag.delegate = self;
[cameraMag openPhotoLibraryWithController:self];
}
- (void)dealloc {
[kNotificationCenter removeObserver:self name:@"EnterForeground" object:nil];
}
#pragma mark - ⾃定义导航视图 Btn(注:若不需要相册⼆维码识别,也可使⽤该⽅法)
-(void)setupNavView {
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)];很酷的签名
bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
bgView.userInteractionEnabled = YES;
//1.返回
UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
粘鼠板backBtn.frame = CGRectMake(20, 30, 25, 25);
[backBtn setBackgroundImage:[[UIImage imageNamed:@"navBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]                      forState:UIControlStateNormal];
//    tMode = UIViewContentModeScaleAspectFit;
[backBtn addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:backBtn];
[self.view addSubview:bgView];
}
#pragma mark - UI
- (void)setupMaskView {
CGFloat Margin = 30; //
CGFloat scanWindowH = self.view.width - 30 * 2;
//    CGFloat scanWindowW = self.view.height - 30 * 2;
//    UIView *topBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.width, 100)]; // ⽆系统导航版本
UIView *topBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 100)];
topBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[self.view addSubview:topBackgroundView];
UIView *leftBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topBackgroundView.frame), Margin, scanWindowH)];
leftBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[self.view addSubview:leftBackgroundView];
UIView *rightBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(self.view.width - Margin,
leftBackgroundView.y, leftBackgroundView.width, leftBackgroundView    rightBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[self.view addSubview:rightBackgroundView];
UIView *bottmBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(rightBackgroundView.frame), self.view.width, self.view.height - CGRectGe    bottmBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[self.view addSubview:bottmBackgroundView];
}
#pragma mark - 设置相机 UI
- (void)setupScanWindow {
CGFloat kMargin = 30;
CGFloat scanWindowH = self.view.width - kMargin * 2;
CGFloat scanWindowW = self.view.width - kMargin * 2;
_scanWindow = [[UIView alloc] initWithFrame:CGRectMake(kMargin, 100, scanWindowW, scanWindowH)];
_scanWindow.clipsToBounds = YES;
[self.view addSubview:_scanWindow];
self.scanLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan_net"]];
CGFloat buttonWH = 18;
UIButton *topLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWH, buttonWH)];
[topLeft setImage:[UIImage imageNamed:@"scan_1"] forState:UIControlStateNormal];
[_scanWindow addSubview:topLeft];
UIButton *topRight = [[UIButton alloc] initWithFrame:CGRectMake(scanWindowW - buttonWH, 0, but
tonWH, buttonWH)];
[topRight setImage:[UIImage imageNamed:@"scan_2"] forState:UIControlStateNormal];
[_scanWindow addSubview:topRight];
UIButton *bottomLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, scanWindowH - buttonWH, buttonWH, buttonWH)];
[bottomLeft setImage:[UIImage imageNamed:@"scan_3"] forState:UIControlStateNormal];
[_scanWindow addSubview:bottomLeft];
UIButton *bottomRight = [[UIButton alloc] initWithFrame:CGRectMake(topRight.x, bottomLeft.y, buttonWH, buttonWH)];
dnf网吧特权[bottomRight setImage:[UIImage imageNamed:@"scan_4"] forState:UIControlStateNormal];
[_scanWindow addSubview:bottomRight];
UILabel * tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_scanWindow.
frame) + 20, self.view.bounds.size.width, 12)];
< = @"将取景框对准⼆维码,即可⾃动扫描";
tipLabel.lineBreakMode = NSLineBreakByWordWrapping;
tipLabel.numberOfLines = 2;
tipLabel.font=[UIFont systemFontOfSize:12];
tipLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:tipLabel];
}
- (void)resumeAnimation {
CAAnimation *anim = [self.scanLineImageView.layer animationForKey:@"translationAnimation"];
if(anim){
// 1. 将动画的时间偏移量作为暂停时的时间点
CFTimeInterval pauseTime = self.scanLineImageView.layer.timeOffset;
// 2. 根据媒体时间计算出准确的启动动画时间,对之前暂停动画的时间进⾏修正
CFTimeInterval beginTime = CACurrentMediaTime() - pauseTime;
CFTimeInterval beginTime = CACurrentMediaTime() - pauseTime;
// 3. 要把偏移时间清零
[self.scanLineImageView.layer setTimeOffset:0.0];
// 4. 设置图层的开始动画时间
牛肉汤做法和配方
[self.scanLineImageView.layer setBeginTime:beginTime];
[self.scanLineImageView.layer setSpeed:1.0];
} else {
CGFloat scanNetImageViewH = 241;
CGFloat scanWindowH = self.view.width - 30 * 2;
CGFloat scanNetImageViewW = _scanWindow.width;
self.scanLineImageView.frame = CGRectMake(0, -scanNetImageViewH, scanNetImageViewW, scanNetImageViewH);        CABasicAnimation *scanNetAnimation = [CABasicAnimation animation];
scanNetAnimation.keyPath = @"anslation.y";
scanNetAnimation.byValue = @(scanWindowH);
scanNetAnimation.duration = 1.5;
[self.scanLineImageView.layer addAnimation:scanNetAnimation forKey:@"translationAnimation"];
[_scanWindow addSubview:self.scanLineImageView];
}
}
#pragma mark - 扫描
- (void)beginScanning {
//获取摄像设备
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输⼊流
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if (!input) return;
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
//设置代理在主线程⾥刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置有效扫描区域
CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];
//初始化链接对象
_session = [[AVCaptureSession alloc]init];
//⾼质量采集率两寸照片尺寸多大
[_session setSessionPreset:AVCaptureSessionPresetHigh];
[_session addInput:input];
[_session addOutput:output];
//设置扫码⽀持的编码格式(如下设置条形码和⼆维码兼容)
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode128Code
];
AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
layer.frame=self.view.layer.bounds;
[self.view.layer insertSublayer:layer atIndex:0];
/
/开始捕获
[_session startRunning];
}
#pragma mark - 获取扫描区域的⽐例关系
- (CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds {
CGFloat x,y,width,height;
x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);
y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);
width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);
height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);
return CGRectMake(x, y, width, height);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。