博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios原生扫描二维码功能,中文不乱码,支持ios7及以上
阅读量:6709 次
发布时间:2019-06-25

本文共 11889 字,大约阅读时间需要 39 分钟。

hot3.png

代码结构:只需要有EUExIosScanner.h和EUExIosScanner.m,几张图片,一个声音文件,以及引入AVFoundation的framework。 EUExIosScanner.h代码如下:

#import 
#import
@interface EUExIosScanner { UIView *view;}@property (nonatomic,retain) UIView *view;@property (nonatomic,copy)UIImageView * readLineView;@property (nonatomic,assign)BOOL is_Anmotion;@property (nonatomic,assign)BOOL is_AnmotionFinished;//开启关闭扫描- (void)start;- (void)stop;- (void)loopDrawLine;//初始化扫描线@end

EUExIosScanner.m代码如下:

////  EUExIosScanner.m//  AppCanPlugin////  Created by zhangda on 15/12/17.//  Copyright © 2015年 zywx. All rights reserved.//#import "EUExIosScanner.h"#import "EUtility.h"#import 
#import
#define DeviceMaxHeight ([UIScreen mainScreen].bounds.size.height)#define DeviceMaxWidth ([UIScreen mainScreen].bounds.size.width)#define widthRate DeviceMaxWidth/320#define contentTitleColorStr @"666666" //正文颜色较深#define titleColorStr @"00A1EA" //标题栏颜色#define IOS8 ([[UIDevice currentDevice].systemVersion intValue] >= 8 ? YES : NO)#define IOS7 ([[UIDevice currentDevice].systemVersion intValue] >= 7 ? YES : NO)@interface EUExIosScanner ()
{ UIView * readview;//二维码扫描对象 AVCaptureSession * session; NSTimer * countTime;}@property (nonatomic, strong) CAShapeLayer *overlay;@property (strong, nonatomic) CIDetector *detector;@end@implementation EUExIosScanner@synthesize view;-(id) initWithBrwView:(EBrowserView *)eInBrwView{ self = [super initWithBrwView:eInBrwView]; if(self){ } return self;}-(void) open:(NSMutableArray *)array{ [self InitScan];}- (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated.}#pragma mark - 返回- (void)backButtonEvent{}#pragma mark 初始化扫描- (void)InitScan{ [self instanceDevice]; readview.backgroundColor = [UIColor clearColor]; [EUtility brwView:meBrwView addSubview:readview];}//关闭页面- (void) closePage{ [self stop]; if (readview) { [readview removeFromSuperview]; if (readview) { readview = nil; } }}//返回结果#pragma mark -QRCodeReaderViewDelegate- (void)readerScanResult:(NSString *)result{ [self stop]; //播放扫描二维码的声音 SystemSoundID soundID; NSString *strSoundFile = [[NSBundle mainBundle] pathForResource:@"noticeMusic" ofType:@"wav"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:strSoundFile],&soundID); AudioServicesPlaySystemSound(soundID); if (readview) { [readview removeFromSuperview]; if (readview) { readview = nil; } } [self accordingQcode:result];}#pragma mark - 扫描结果处理- (void)accordingQcode:(NSString *)str{ [self jsSuccessWithName:@"uexIosScanner.cbOpen" opId:@"234" dataType:0 strData:str];// UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];// [alertView show];}- (void)instanceDevice{ readview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DeviceMaxWidth, DeviceMaxHeight)]; readview.backgroundColor = [UIColor whiteColor]; //扫描区域 UIImage *hbImage=[UIImage imageNamed:@"scanscanBg"]; UIImageView * scanZomeBack=[[UIImageView alloc] init]; scanZomeBack.backgroundColor = [UIColor clearColor]; scanZomeBack.layer.borderColor = [UIColor whiteColor].CGColor; scanZomeBack.layer.borderWidth = 2.5; scanZomeBack.image = hbImage; //添加一个背景图片 CGRect mImagerect = CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2, 200*widthRate, 200*widthRate); [scanZomeBack setFrame:mImagerect]; CGRect scanCrop=[self getScanCrop:mImagerect readerViewBounds:readview.frame]; [readview addSubview:scanZomeBack]; //获取摄像设备 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 = scanCrop; //初始化链接对象 session = [[AVCaptureSession alloc]init]; //高质量采集率 [session setSessionPreset:AVCaptureSessionPresetHigh]; if (input) { [session addInput:input]; } if (output) { [session addOutput:output]; //设置扫码支持的编码格式(如下设置条形码和二维码兼容) NSMutableArray *a = [[NSMutableArray alloc] init]; if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) { [a addObject:AVMetadataObjectTypeQRCode]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) { [a addObject:AVMetadataObjectTypeEAN13Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) { [a addObject:AVMetadataObjectTypeEAN8Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) { [a addObject:AVMetadataObjectTypeCode128Code]; } output.metadataObjectTypes=a; } AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; layer.frame=readview.layer.bounds; [readview.layer insertSublayer:layer atIndex:0]; [self setOverlayPickerView:readview]; [self loopDrawLine]; //开始捕获 [session startRunning]; }-(void)loopDrawLine{ _is_AnmotionFinished = NO; CGRect rect = CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2, 200*widthRate, 2); if (_readLineView) { _readLineView.alpha = 1; _readLineView.frame = rect; } else{ _readLineView = [[UIImageView alloc] initWithFrame:rect]; [_readLineView setImage:[UIImage imageNamed:@"scanLine"]]; [readview addSubview:_readLineView]; } [UIView animateWithDuration:1.5 animations:^{ //修改fream的代码写在这里 _readLineView.frame =CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2+200*widthRate-5, 200*widthRate, 2); } completion:^(BOOL finished) { if (!_is_Anmotion) { [self loopDrawLine]; } _is_AnmotionFinished = YES; }];}- (void)setOverlayPickerView:(UIView *)reader{ CGFloat wid = 60*widthRate; CGFloat heih = (DeviceMaxHeight-200*widthRate)/2; //最上部view CGFloat alpha = 0.6; UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceMaxWidth, 55)]; upView.backgroundColor = [self colorFromHexRGB:titleColorStr]; //头部标题 UILabel * titleLabel= [[UILabel alloc] init]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.frame=CGRectMake((DeviceMaxWidth/2)-80, 20, 160, 35); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor=[UIColor whiteColor]; titleLabel.text=@"请扫描二维码"; [upView addSubview:titleLabel]; //关闭按钮 UIButton * closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 60, 35)]; closeBtn.backgroundColor = [UIColor clearColor]; [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];// 添加文字 [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [closeBtn addTarget:self action:@selector(closePage) forControlEvents:UIControlEventTouchUpInside]; [upView addSubview:closeBtn]; [reader addSubview:upView]; //中上部view UIView* middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, DeviceMaxWidth, heih-55)]; middleView.alpha = alpha; middleView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:middleView]; //左侧的view UIView * cLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, heih, wid, 200*widthRate)]; cLeftView.alpha = alpha; cLeftView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:cLeftView]; //右侧的view UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(DeviceMaxWidth-wid, heih, wid, 200*widthRate)]; rightView.alpha = alpha; rightView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:rightView]; //底部view UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, heih+200*widthRate, DeviceMaxWidth, DeviceMaxHeight - heih-200*widthRate)]; downView.alpha = alpha; downView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:downView]; //开关灯button UIButton * turnBtn = [UIButton buttonWithType:UIButtonTypeCustom]; turnBtn.backgroundColor = [UIColor clearColor]; [turnBtn setBackgroundImage:[UIImage imageNamed:@"lightSelect"] forState:UIControlStateNormal]; [turnBtn setBackgroundImage:[UIImage imageNamed:@"lightNormal"] forState:UIControlStateSelected]; turnBtn.frame=CGRectMake((DeviceMaxWidth-50*widthRate)/2, (CGRectGetHeight(downView.frame)-50*widthRate)/2, 50*widthRate, 50*widthRate); [turnBtn addTarget:self action:@selector(turnBtnEvent:) forControlEvents:UIControlEventTouchUpInside]; [downView addSubview:turnBtn]; }- (void)turnBtnEvent:(UIButton *)button_{ button_.selected = !button_.selected; if (button_.selected) { [self turnTorchOn:YES]; } else{ [self turnTorchOn:NO]; } }- (void)turnTorchOn:(bool)on{ Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; if (on) { [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; } [device unlockForConfiguration]; } }}-(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); }- (void)start{ [session startRunning];}- (void)stop{ [session stopRunning];}#pragma mark - 扫描结果- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects && metadataObjects.count>0) { AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; //输出结果并返回 [self readerScanResult:metadataObject.stringValue]; }}#pragma mark - 颜色//获取颜色- (UIColor *)colorFromHexRGB:(NSString *)inColorString{ UIColor *result = nil; unsigned int colorCode = 0; unsigned char redByte, greenByte, blueByte; if (nil != inColorString) { NSScanner *scanner = [NSScanner scannerWithString:inColorString]; (void) [scanner scanHexInt:&colorCode]; // ignore error } redByte = (unsigned char) (colorCode >> 16); greenByte = (unsigned char) (colorCode >> 8); blueByte = (unsigned char) (colorCode); // masks off high bits result = [UIColor colorWithRed: (float)redByte / 0xff green: (float)greenByte/ 0xff blue: (float)blueByte / 0xff alpha:1.0]; return result;}@end

其中,open为入口方法,我这里是写的是appcan的ios插件,所有有些地方需要微调

转载于:https://my.oschina.net/u/1011854/blog/551100

你可能感兴趣的文章
我的友情链接
查看>>
kafka 的原理介绍
查看>>
轻松学会Java高并发第一课-并发的基本概念
查看>>
Flutter 底部导航——BottomNavigationBar
查看>>
linux内核协议栈TCP time_wait原理、配置、副作用
查看>>
IT兄弟连 JavaWeb教程 jQuery中其他AJAX支持的函数
查看>>
android之数据库和Content Provider(二)
查看>>
Java调用Https
查看>>
验证码cnn模型
查看>>
quartz CronTrigger表达式整理
查看>>
用户定位 User Location
查看>>
图文混排 文字垂直居中对齐
查看>>
Qt Socket简单通信
查看>>
如何优雅地过滤敏感词
查看>>
windows EFS加密
查看>>
那些年遇到过的面试题
查看>>
2018.1.31 8周3次课
查看>>
sublime汉化
查看>>
Netfilter/iptables的一些新进展
查看>>
Linux下使用虚拟网卡的ingress流控(入口流控)
查看>>