代碼結構:只須要有EUExIosScanner.h和EUExIosScanner.m,幾張圖片,一個聲音文件,以及引入AVFoundation的framework。 EUExIosScanner.h代碼以下:ios
#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface EUExIosScanner { UIView *view; } @property (nonatomic,retain) UIView *view; @property (nonatomic,copy)UIImageView * readLineView; @property (nonatomic,assign)BOOL is_Anmotion; @property (nonatomic,assign)BOOL is_AnmotionFinished; //開啓關閉掃描 - (void)start; - (void)stop; - (void)loopDrawLine;//初始化掃描線 @end
EUExIosScanner.m代碼以下:session
// // EUExIosScanner.m // AppCanPlugin // // Created by zhangda on 15/12/17. // Copyright © 2015年 zywx. All rights reserved. // #import "EUExIosScanner.h" #import "EUtility.h" #import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioToolbox.h> #define DeviceMaxHeight ([UIScreen mainScreen].bounds.size.height) #define DeviceMaxWidth ([UIScreen mainScreen].bounds.size.width) #define widthRate DeviceMaxWidth/320 #define contentTitleColorStr @"666666" //正文顏色較深 #define titleColorStr @"00A1EA" //標題欄顏色 #define IOS8 ([[UIDevice currentDevice].systemVersion intValue] >= 8 ? YES : NO) #define IOS7 ([[UIDevice currentDevice].systemVersion intValue] >= 7 ? YES : NO) @interface EUExIosScanner () <AVCaptureMetadataOutputObjectsDelegate> { UIView * readview;//二維碼掃描對象 AVCaptureSession * session; NSTimer * countTime; } @property (nonatomic, strong) CAShapeLayer *overlay; @property (strong, nonatomic) CIDetector *detector; @end @implementation EUExIosScanner @synthesize view; -(id) initWithBrwView:(EBrowserView *)eInBrwView{ self = [super initWithBrwView:eInBrwView]; if(self){ } return self; } -(void) open:(NSMutableArray *)array{ [self InitScan]; } - (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated. } #pragma mark - 返回 - (void)backButtonEvent { } #pragma mark 初始化掃描 - (void)InitScan { [self instanceDevice]; readview.backgroundColor = [UIColor clearColor]; [EUtility brwView:meBrwView addSubview:readview]; } //關閉頁面 - (void) closePage { [self stop]; if (readview) { [readview removeFromSuperview]; if (readview) { readview = nil; } } } //返回結果 #pragma mark -QRCodeReaderViewDelegate - (void)readerScanResult:(NSString *)result { [self stop]; //播放掃描二維碼的聲音 SystemSoundID soundID; NSString *strSoundFile = [[NSBundle mainBundle] pathForResource:@"noticeMusic" ofType:@"wav"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:strSoundFile],&soundID); AudioServicesPlaySystemSound(soundID); if (readview) { [readview removeFromSuperview]; if (readview) { readview = nil; } } [self accordingQcode:result]; } #pragma mark - 掃描結果處理 - (void)accordingQcode:(NSString *)str { [self jsSuccessWithName:@"uexIosScanner.cbOpen" opId:@"234" dataType:0 strData:str]; // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"掃描結果" message:str delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; // [alertView show]; } - (void)instanceDevice { readview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DeviceMaxWidth, DeviceMaxHeight)]; readview.backgroundColor = [UIColor whiteColor]; //掃描區域 UIImage *hbImage=[UIImage imageNamed:@"scanscanBg"]; UIImageView * scanZomeBack=[[UIImageView alloc] init]; scanZomeBack.backgroundColor = [UIColor clearColor]; scanZomeBack.layer.borderColor = [UIColor whiteColor].CGColor; scanZomeBack.layer.borderWidth = 2.5; scanZomeBack.image = hbImage; //添加一個背景圖片 CGRect mImagerect = CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2, 200*widthRate, 200*widthRate); [scanZomeBack setFrame:mImagerect]; CGRect scanCrop=[self getScanCrop:mImagerect readerViewBounds:readview.frame]; [readview addSubview:scanZomeBack]; //獲取攝像設備 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //建立輸入流 AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //建立輸出流 AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; //設置代理 在主線程裏刷新 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; output.rectOfInterest = scanCrop; //初始化連接對象 session = [[AVCaptureSession alloc]init]; //高質量採集率 [session setSessionPreset:AVCaptureSessionPresetHigh]; if (input) { [session addInput:input]; } if (output) { [session addOutput:output]; //設置掃碼支持的編碼格式(以下設置條形碼和二維碼兼容) NSMutableArray *a = [[NSMutableArray alloc] init]; if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) { [a addObject:AVMetadataObjectTypeQRCode]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) { [a addObject:AVMetadataObjectTypeEAN13Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) { [a addObject:AVMetadataObjectTypeEAN8Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) { [a addObject:AVMetadataObjectTypeCode128Code]; } output.metadataObjectTypes=a; } AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; layer.frame=readview.layer.bounds; [readview.layer insertSublayer:layer atIndex:0]; [self setOverlayPickerView:readview]; [self loopDrawLine]; //開始捕獲 [session startRunning]; } -(void)loopDrawLine { _is_AnmotionFinished = NO; CGRect rect = CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2, 200*widthRate, 2); if (_readLineView) { _readLineView.alpha = 1; _readLineView.frame = rect; } else{ _readLineView = [[UIImageView alloc] initWithFrame:rect]; [_readLineView setImage:[UIImage imageNamed:@"scanLine"]]; [readview addSubview:_readLineView]; } [UIView animateWithDuration:1.5 animations:^{ //修改fream的代碼寫在這裏 _readLineView.frame =CGRectMake(60*widthRate, (DeviceMaxHeight-200*widthRate)/2+200*widthRate-5, 200*widthRate, 2); } completion:^(BOOL finished) { if (!_is_Anmotion) { [self loopDrawLine]; } _is_AnmotionFinished = YES; }]; } - (void)setOverlayPickerView:(UIView *)reader { CGFloat wid = 60*widthRate; CGFloat heih = (DeviceMaxHeight-200*widthRate)/2; //最上部view CGFloat alpha = 0.6; UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceMaxWidth, 55)]; upView.backgroundColor = [self colorFromHexRGB:titleColorStr]; //頭部標題 UILabel * titleLabel= [[UILabel alloc] init]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.frame=CGRectMake((DeviceMaxWidth/2)-80, 20, 160, 35); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor=[UIColor whiteColor]; titleLabel.text=@"請掃描二維碼"; [upView addSubview:titleLabel]; //關閉按鈕 UIButton * closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 60, 35)]; closeBtn.backgroundColor = [UIColor clearColor]; [closeBtn setTitle:@"關閉" forState:UIControlStateNormal];// 添加文字 [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [closeBtn addTarget:self action:@selector(closePage) forControlEvents:UIControlEventTouchUpInside]; [upView addSubview:closeBtn]; [reader addSubview:upView]; //中上部view UIView* middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, DeviceMaxWidth, heih-55)]; middleView.alpha = alpha; middleView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:middleView]; //左側的view UIView * cLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, heih, wid, 200*widthRate)]; cLeftView.alpha = alpha; cLeftView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:cLeftView]; //右側的view UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(DeviceMaxWidth-wid, heih, wid, 200*widthRate)]; rightView.alpha = alpha; rightView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:rightView]; //底部view UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, heih+200*widthRate, DeviceMaxWidth, DeviceMaxHeight - heih-200*widthRate)]; downView.alpha = alpha; downView.backgroundColor = [self colorFromHexRGB:contentTitleColorStr]; [reader addSubview:downView]; //開關燈button UIButton * turnBtn = [UIButton buttonWithType:UIButtonTypeCustom]; turnBtn.backgroundColor = [UIColor clearColor]; [turnBtn setBackgroundImage:[UIImage imageNamed:@"lightSelect"] forState:UIControlStateNormal]; [turnBtn setBackgroundImage:[UIImage imageNamed:@"lightNormal"] forState:UIControlStateSelected]; turnBtn.frame=CGRectMake((DeviceMaxWidth-50*widthRate)/2, (CGRectGetHeight(downView.frame)-50*widthRate)/2, 50*widthRate, 50*widthRate); [turnBtn addTarget:self action:@selector(turnBtnEvent:) forControlEvents:UIControlEventTouchUpInside]; [downView addSubview:turnBtn]; } - (void)turnBtnEvent:(UIButton *)button_ { button_.selected = !button_.selected; if (button_.selected) { [self turnTorchOn:YES]; } else{ [self turnTorchOn:NO]; } } - (void)turnTorchOn:(bool)on { Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; if (on) { [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; } [device unlockForConfiguration]; } } } -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds { CGFloat x,y,width,height; x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds); y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds); width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds); height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds); return CGRectMake(x, y, width, height); } - (void)start { [session startRunning]; } - (void)stop { [session stopRunning]; } #pragma mark - 掃描結果 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects && metadataObjects.count>0) { AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; //輸出結果並返回 [self readerScanResult:metadataObject.stringValue]; } } #pragma mark - 顏色 //獲取顏色 - (UIColor *)colorFromHexRGB:(NSString *)inColorString { UIColor *result = nil; unsigned int colorCode = 0; unsigned char redByte, greenByte, blueByte; if (nil != inColorString) { NSScanner *scanner = [NSScanner scannerWithString:inColorString]; (void) [scanner scanHexInt:&colorCode]; // ignore error } redByte = (unsigned char) (colorCode >> 16); greenByte = (unsigned char) (colorCode >> 8); blueByte = (unsigned char) (colorCode); // masks off high bits result = [UIColor colorWithRed: (float)redByte / 0xff green: (float)greenByte/ 0xff blue: (float)blueByte / 0xff alpha:1.0]; return result; } @end
其中,open爲入口方法,我這裏是寫的是appcan的ios插件,全部有些地方須要微調app