功能类:二维码扫描(相机扫描,相册扫描)
来源:互联网 发布:linux修改网络配置 编辑:程序博客网 时间:2024/06/06 05:19
#import <UIKit/UIKit.h>@interface SaoMiaoViewController : UIViewController@end#import "SaoMiaoViewController.h"#import <AVFoundation/AVFoundation.h>#define IS_VAILABLE_IOS8 ([[[UIDevice currentDevice] systemVersion] intValue] >= 8)@interface SaoMiaoViewController ()<AVCaptureMetadataOutputObjectsDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>@property (strong, nonatomic) AVCaptureSession *captureSession;typedef void (^XMSweepBlock)(NSString *result);@property(nonatomic,copy)XMSweepBlock didRecoiveBlock;-(void)setDidRecoiveBlock:(XMSweepBlock)didRecoiveBlock;@end@implementation SaoMiaoViewController- (void)viewDidLoad { [super viewDidLoad];// UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];// [btn setTitle:@"相册" forState: UIControlStateNormal];// [btn addTarget:self action:@selector(clickToXiangCe:) forControlEvents:UIControlEventTouchUpOutside];// UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];// self.navigationItem.rightBarButtonItem = rightItem; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (error) { NSLog(@"启动镜头失败"); return; }; if ([self.captureSession canAddInput:deviceInput]) { [self.captureSession addInput:deviceInput]; } AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc]init]; if ([self.captureSession canAddOutput:metadataOutput]) { [self.captureSession addOutput:metadataOutput]; } [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // NSArray *type = metadataOutput.availableMetadataObjectTypes; // NSLog(@"%@",type); metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; [self.view.layer addSublayer:layer]; layer.frame = self.view.bounds; // 解析范围, (输出), 有效的扫描范围 CGFloat wh = 300; // 有效范围 300 * 300, 居中 // 参数值是 0.0 ~ 1.0, Rect:(y,x,height,width) CGFloat width = wh / self.view.bounds.size.width; CGFloat height = wh / self.view.bounds.size.height; CGFloat x = (self.view.bounds.size.width - wh) * 0.5 / self.view.bounds.size.width; CGFloat y = (self.view.bounds.size.height - wh) * 0.5 / self.view.bounds.size.height; // Rect:(y,x,height,width) metadataOutput.rectOfInterest = CGRectMake(y, x, height, width); [self.captureSession startRunning]; [self addOtherLay:CGRectMake((self.view.bounds.size.width - wh) * 0.5, (self.view.bounds.size.height - wh) * 0.5, wh, wh)];}- (IBAction)clicktoxiangce:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc]init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil];}//选中单张照片- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { [picker dismissViewControllerAnimated:YES completion:nil]; __block UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; if (!image) { image = [info objectForKey:UIImagePickerControllerOriginalImage]; } //系统自带的识别方法 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }]; CGImageRef ref = (CGImageRef)image.CGImage; CIImage *cii = [CIImage imageWithCGImage:ref]; NSArray *feacture = [detector featuresInImage:cii]; if (feacture.count >= 1) { CIQRCodeFeature *feature = [feacture objectAtIndex:0]; NSString *scanResult = feature.messageString; if (_didRecoiveBlock) { self.didRecoiveBlock(scanResult);// [self selfRemoveFromSuperview]; } else { [self.captureSession stopRunning]; NSURL *url = [NSURL URLWithString:scanResult]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame]; [self.view addSubview:webView]; [webView loadRequest:request]; } }}#pragma mark - AVCaptureMetadataOutputObjectsDelegate-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.lastObject; NSString *resultStr = metadataObject.stringValue; NSLog(@"%@",resultStr); [self.captureSession stopRunning]; NSURL *url = [NSURL URLWithString:resultStr]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame]; [self.view addSubview:webView]; [webView loadRequest:request];}- (void)addOtherLay:(CGRect)rect{ // Rect为保留的矩形frame值 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; CGFloat leftWidth = (screenWidth - rect.size.width) / 2; CAShapeLayer* layerTop = [[CAShapeLayer alloc] init]; layerTop.fillColor = [UIColor blackColor].CGColor; layerTop.opacity = 0.5; layerTop.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, screenWidth, rect.origin.y)].CGPath; [self.view.layer addSublayer:layerTop]; CAShapeLayer* layerLeft = [[CAShapeLayer alloc] init]; layerLeft.fillColor = [UIColor blackColor].CGColor; layerLeft.opacity = 0.5; layerLeft.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, rect.origin.y, leftWidth, rect.size.height)].CGPath; [self.view.layer addSublayer:layerLeft]; CAShapeLayer* layerRight = [[CAShapeLayer alloc] init]; layerRight.fillColor = [UIColor blackColor].CGColor; layerRight.opacity = 0.5; layerRight.path = [UIBezierPath bezierPathWithRect:CGRectMake(screenWidth - leftWidth, rect.origin.y, rect.size.width, rect.size.height)].CGPath; [self.view.layer addSublayer:layerRight]; CAShapeLayer* layerBottom = [[CAShapeLayer alloc] init]; layerBottom.fillColor = [UIColor blackColor].CGColor; layerBottom.opacity = 0.5; layerBottom.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, CGRectGetMaxY(rect), screenWidth, screenHeight - CGRectGetMaxY(rect))].CGPath; [self.view.layer addSublayer:layerBottom];}- (AVCaptureSession *)captureSession { if (_captureSession == nil) { _captureSession = [[AVCaptureSession alloc]init]; } return _captureSession;}@end
0 0
- 功能类:二维码扫描(相机扫描,相册扫描)
- 二维码(生成,扫描,扫描手机相册的,shenLog头像)
- zxing二维码扫描功能
- H5+ 二维码扫描功能
- 二维码扫描功能
- 集成二维码扫描功能
- iOS_相机扫描二维码、条形码
- Swift_相机扫描二维码、条形码
- Android实现二维码扫描功能(四)-ZXing识别图片二维码,相册选图
- 二维码(扫描,生成)
- 二维码扫描(生成)
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 佛家辞典
- POJ 1961 (KMP)
- 编译内核
- java学习总结(16.07.18-16.07.21)集合与泛型
- ServletContext 总结
- 功能类:二维码扫描(相机扫描,相册扫描)
- 五大常用算法之一:分治算法
- nginx 正则表达式匹配
- 提高listview性能的几个方法
- #码神心得_13# java基础类库3
- ping通百度却在浏览器中打不开 netsh winsock reset
- Windows 下 Apache HTTP Server 安装、配置以及与 Tomcat 的整合(附图)
- 毕业生网站出错:筛选以后,下一个管理员可以看到上一个管理员筛选的消息
- Java修饰符