功能类:二维码扫描(相机扫描,相册扫描)

来源:互联网 发布: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
原创粉丝点击