123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //
- // SWQRCodeManager.m
- // SWQRCode_Objc
- //
- // Created by zhuku on 2018/4/4.
- // Copyright © 2018年 selwyn. All rights reserved.
- //
- #import "SWQRCodeManager.h"
- #import <Photos/PHPhotoLibrary.h>
- @implementation SWQRCodeManager
- /** 校验是否有相机权限 */
- + (void)sw_checkCameraAuthorizationStatusWithGrand:(void(^)(BOOL granted))permissionGranted
- {
- AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
-
- switch (videoAuthStatus) {
- // 已授权
- case AVAuthorizationStatusAuthorized:
- {
- permissionGranted(YES);
- }
- break;
- // 未询问用户是否授权
- case AVAuthorizationStatusNotDetermined:
- {
- // 提示用户授权
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
- permissionGranted(granted);
- }];
- }
- break;
- // 用户拒绝授权或权限受限
- case AVAuthorizationStatusRestricted:
- case AVAuthorizationStatusDenied:
- {
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请在”设置-隐私-相机”选项中,允许访问你的相机" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
- [alert show];
- permissionGranted(NO);
- }
- break;
- default:
- break;
- }
- }
- /** 校验是否有相册权限 */
- + (void)sw_checkAlbumAuthorizationStatusWithGrand:(void(^)(BOOL granted))permissionGranted {
-
- PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
- switch (photoAuthStatus) {
- // 已授权
- case PHAuthorizationStatusAuthorized:
- {
- permissionGranted(YES);
- }
- break;
- // 未询问用户是否授权
- case PHAuthorizationStatusNotDetermined:
- {
- [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
- permissionGranted(status == PHAuthorizationStatusAuthorized);
- }];
- }
- break;
- // 用户拒绝授权或权限受限
- case PHAuthorizationStatusRestricted:
- case PHAuthorizationStatusDenied:
- {
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请在”设置-隐私-相片”选项中,允许访问你的相册" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
- [alert show];
- permissionGranted(NO);
- }
- break;
- default:
- break;
- }
-
- }
- /** 根据扫描器类型配置支持编码格式 */
- + (NSArray *)sw_metadataObjectTypesWithType:(SWScannerType)scannerType {
- switch (scannerType) {
- case SWScannerTypeQRCode:
- {
- return @[AVMetadataObjectTypeQRCode];
- }
- break;
- case SWScannerTypeBarCode:
- {
- return @[AVMetadataObjectTypeEAN13Code,
- AVMetadataObjectTypeEAN8Code,
- AVMetadataObjectTypeUPCECode,
- AVMetadataObjectTypeCode39Code,
- AVMetadataObjectTypeCode39Mod43Code,
- AVMetadataObjectTypeCode93Code,
- AVMetadataObjectTypeCode128Code,
- AVMetadataObjectTypePDF417Code];
- }
- break;
- case SWScannerTypeBoth:
- {
- return @[AVMetadataObjectTypeQRCode,
- AVMetadataObjectTypeEAN13Code,
- AVMetadataObjectTypeEAN8Code,
- AVMetadataObjectTypeUPCECode,
- AVMetadataObjectTypeCode39Code,
- AVMetadataObjectTypeCode39Mod43Code,
- AVMetadataObjectTypeCode93Code,
- AVMetadataObjectTypeCode128Code,
- AVMetadataObjectTypePDF417Code];
- }
- break;
- default:
- break;
- }
- }
- /** 根据扫描器类型配置导航栏标题 */
- + (NSString *)sw_navigationItemTitleWithType:(SWScannerType)scannerType {
- switch (scannerType) {
- case SWScannerTypeQRCode:
- {
- return @"二维码";
- }
- break;
- case SWScannerTypeBarCode:
- {
- return @"条码";
- }
- break;
- case SWScannerTypeBoth:
- {
- return @"二维码/条码";
- }
- break;
- default:
- break;
- }
- }
- /** 手电筒开关 */
- + (void)sw_FlashlightOn:(BOOL)on {
- AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
- if ([captureDevice hasTorch] && [captureDevice hasFlash]) {
- [captureDevice lockForConfiguration:nil];
- if (on) {
- [captureDevice setTorchMode:AVCaptureTorchModeOn];
- [captureDevice setFlashMode:AVCaptureFlashModeOn];
- }else
- {
- [captureDevice setTorchMode:AVCaptureTorchModeOff];
- [captureDevice setFlashMode:AVCaptureFlashModeOff];
- }
- [captureDevice unlockForConfiguration];
- }
- }
- @end
|