YH_ScanUtility.m 2.95 KB
//
//  YH_ScanUtility.m
//  YH_Mall
//
//  Created by Cloud on 15/4/10.
//  Copyright (c) 2015年 YOHO. All rights reserved.
//

#import "YH_ScanUtility.h"
#import <UIKit/UIKit.h>

@interface YH_ScanUtility ()

@property (copy, nonatomic) NSString *scanResultString;
@property (strong, nonatomic) StringBlock scanResultBlock;

@end

@implementation YH_ScanUtility

- (instancetype)init
{
    self = [super init];
    if (self) {
        _enableFlashLight = NO;
    }
    return self;
}

- (void)addScanCallBackHandle:(StringBlock)scanCallBack
{
    self.scanResultBlock = scanCallBack;
}

- (void)resetCamera
{
    [self setupAVCamera];
}


- (void)setupAVCamera
{
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (self.enableFlashLight) {
        if ([_device lockForConfiguration:nil]) {
//            if ([_device respondsToSelector:@selector(setFlashMode:)]) {
//                [_device setFlashMode:AVCaptureFlashModeOn];
//            }
            if ([_device respondsToSelector:@selector(setTorchMode:)]) {
                [_device setTorchMode:AVCaptureTorchModeOn];
            }
            [_device setTorchModeOnWithLevel:AVCaptureMaxAvailableTorchLevel error:nil];
            [_device unlockForConfiguration];
        }
    }
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    self.output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    self.session = [[AVCaptureSession alloc]init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input]){
        [_session addInput:self.input];
    } else {
        return;
    }
    if ([_session canAddOutput:self.output]){
        [_session addOutput:self.output];
    } else {
        return;
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.previewLayer.frame  = [[UIScreen mainScreen] bounds];
    _output.rectOfInterest = CGRectMake(0, 0, (175.f+220.f)*kScreenPointScale/kScreenHeight, (190.f+220.f)*kScreenPointScale/kScreenWidth);

    [_session startRunning];
}

#pragma mark -
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    [_session stopRunning];
    NSString *stringValue = nil;
    if ([metadataObjects count] <= 0){
        return;
    }
    
    AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
    stringValue = metadataObject.stringValue;
    self.scanResultBlock(stringValue,nil);
}

@end