123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- //
- //
- //
- //
- // Created by lbxia on 15/10/21.
- // Copyright © 2015年 lbxia. All rights reserved.
- //
- #import "LBXScanViewController.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import <Photos/Photos.h>
- #import "LBXScanTypes.h"
- #import "LBXScanView.h"
- #import "LBXScanNative.h" //原生扫码封装
- @interface LBXScanViewController ()
- @end
- @implementation LBXScanViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
- self.edgesForExtendedLayout = UIRectEdgeNone;
- }
-
- self.view.backgroundColor = [UIColor blackColor];
-
- self.title = @"二维码/条形码";
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
-
- [self drawScanView];
-
- [self requestCameraPemissionWithResult:^(BOOL granted) {
- if (granted) {
- //不延时,可能会导致界面黑屏并卡住一会
- [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3];
- }else{
- [_qRScanView stopDeviceReadying];
- }
- }];
-
- }
- //绘制扫描区域
- - (void)drawScanView
- {
- if (!_qRScanView)
- {
- CGRect rect = self.view.frame;
- rect.origin = CGPointMake(0, 0);
-
- self.qRScanView = [[LBXScanView alloc]initWithFrame:rect style:_style];
-
- [self.view addSubview:_qRScanView];
- }
-
- if (!_cameraInvokeMsg) {
-
- // _cameraInvokeMsg = NSLocalizedString(@"wating...", nil);
- }
-
- [_qRScanView startDeviceReadyingWithText:_cameraInvokeMsg];
- }
- - (void)reStartDevice
- {
- switch (_libraryType) {
- case SLT_Native:
- {
- [_scanObj startScan];
- }
- break;
- default:
- break;
- }
-
- }
- //启动设备
- - (void)startScan
- {
- UIView *videoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
- videoView.backgroundColor = [UIColor clearColor];
- [self.view insertSubview:videoView atIndex:0];
- __weak __typeof(self) weakSelf = self;
-
- switch (_libraryType) {
- case SLT_Native:
- {
-
- if (!_scanObj )
- {
- CGRect cropRect = CGRectZero;
-
- if (_isOpenInterestRect) {
-
- //设置只识别框内区域
- cropRect = [LBXScanView getScanRectWithPreView:self.view style:_style];
- }
- NSString *strCode = AVMetadataObjectTypeQRCode;
- if (_scanCodeType != SCT_BarCodeITF ) {
-
- strCode = [self nativeCodeWithType:_scanCodeType];
- }
-
- //AVMetadataObjectTypeITF14Code 扫码效果不行,另外只能输入一个码制,虽然接口是可以输入多个码制
- self.scanObj = [[LBXScanNative alloc]initWithPreView:videoView ObjectType:@[strCode] cropRect:cropRect success:^(NSArray<LBXScanResult *> *array) {
-
- [weakSelf scanResultWithArray:array];
- }];
- [_scanObj setNeedCaptureImage:_isNeedScanImage];
- }
- [_scanObj startScan];
- }
- break;
- default:
- break;
- }
-
- [_qRScanView stopDeviceReadying];
- [_qRScanView startScanAnimation];
-
- self.view.backgroundColor = [UIColor clearColor];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
-
- [NSObject cancelPreviousPerformRequestsWithTarget:self];
-
- [self stopScan];
-
- [_qRScanView stopScanAnimation];
- }
- - (void)stopScan
- {
- switch (_libraryType) {
- case SLT_Native:
- {
- [_scanObj stopScan];
- }
- break;
- default:
- break;
- }
- }
- #pragma mark -扫码结果处理
- - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
- {
- //设置了委托的处理
- if (_delegate) {
- [_delegate scanResultWithArray:array];
- }
-
- //也可以通过继承LBXScanViewController,重写本方法即可
- }
- //开关闪光灯
- - (void)openOrCloseFlash
- {
-
- switch (_libraryType) {
- case SLT_Native:
- {
- [_scanObj changeTorch];
- }
- break;
- default:
- break;
- }
- self.isOpenFlash =!self.isOpenFlash;
- }
- #pragma mark --打开相册并识别图片
- /*!
- * 打开本地照片,选择图片识别
- */
- - (void)openLocalPhoto:(BOOL)allowsEditing
- {
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
-
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
-
- picker.delegate = self;
-
- //部分机型有问题
- picker.allowsEditing = allowsEditing;
-
-
- [self presentViewController:picker animated:YES completion:nil];
- }
- //当选择一张图片后进入这里
- -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- [picker dismissViewControllerAnimated:YES completion:nil];
-
- __block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
-
- if (!image){
- image = [info objectForKey:UIImagePickerControllerOriginalImage];
- }
-
- __weak __typeof(self) weakSelf = self;
-
- switch (_libraryType) {
- case SLT_Native:
- {
- if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
- {
- [LBXScanNative recognizeImage:image success:^(NSArray<LBXScanResult *> *array) {
- [weakSelf scanResultWithArray:array];
- }];
- }
- else
- {
- [self showError:@"native低于ios8.0系统不支持识别图片条码"];
- }
- }
- break;
- case SLT_ZXing:
- {
-
- }
- break;
- case SLT_ZBar:
- {
- }
- break;
-
- default:
- break;
- }
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- {
- NSLog(@"cancel");
-
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (NSString*)nativeCodeWithType:(SCANCODETYPE)type
- {
- switch (type) {
- case SCT_QRCode:
- return AVMetadataObjectTypeQRCode;
- break;
- case SCT_BarCode93:
- return AVMetadataObjectTypeCode93Code;
- break;
- case SCT_BarCode128:
- return AVMetadataObjectTypeCode128Code;
- break;
- case SCT_BarCodeITF:
- return @"ITF条码:only ZXing支持";
- break;
- case SCT_BarEAN13:
- return AVMetadataObjectTypeEAN13Code;
- break;
- default:
- return AVMetadataObjectTypeQRCode;
- break;
- }
- }
- - (void)showError:(NSString*)str
- {
-
- }
- - (void)requestCameraPemissionWithResult:(void(^)( BOOL granted))completion
- {
- if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
- {
- AVAuthorizationStatus permission =
- [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
-
- switch (permission) {
- case AVAuthorizationStatusAuthorized:
- completion(YES);
- break;
- case AVAuthorizationStatusDenied:
- case AVAuthorizationStatusRestricted:
- completion(NO);
- break;
- case AVAuthorizationStatusNotDetermined:
- {
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
- completionHandler:^(BOOL granted) {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- completion(true);
- } else {
- completion(false);
- }
- });
-
- }];
- }
- break;
-
- }
- }
-
-
- }
- + (BOOL)photoPermission
- {
- if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
- {
- ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
-
- if ( author == ALAuthorizationStatusDenied ) {
-
- return NO;
- }
- return YES;
- }
-
- PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus];
- if ( authorStatus == PHAuthorizationStatusDenied ) {
-
- return NO;
- }
- return YES;
- }
- @end
|