iOS 扫一扫功能实现.docx

上传人:b****8 文档编号:30266979 上传时间:2023-08-13 格式:DOCX 页数:18 大小:190.50KB
下载 相关 举报
iOS 扫一扫功能实现.docx_第1页
第1页 / 共18页
iOS 扫一扫功能实现.docx_第2页
第2页 / 共18页
iOS 扫一扫功能实现.docx_第3页
第3页 / 共18页
iOS 扫一扫功能实现.docx_第4页
第4页 / 共18页
iOS 扫一扫功能实现.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

iOS 扫一扫功能实现.docx

《iOS 扫一扫功能实现.docx》由会员分享,可在线阅读,更多相关《iOS 扫一扫功能实现.docx(18页珍藏版)》请在冰豆网上搜索。

iOS 扫一扫功能实现.docx

iOS扫一扫功能实现

iOS扫一扫功能实现

下面贴上代码:

AppDelegate.m:

[objc]viewplaincopy在CODE上查看代码片派生到我的代码片

-(BOOL)application:

(UIApplication*)applicationdidFinishLaunchingWithOptions:

(NSDictionary*)launchOptions{

self.window=[[UIWindowalloc]initWithFrame:

[[UIScreenmainScreen]bounds]];

self.window.rootViewController=[[UINavigationControlleralloc]initWithRootViewController:

[[ViewControlleralloc]init]];

self.window.backgroundColor=[UIColorwhiteColor];

[self.windowmakeKeyAndVisible];

returnYES;

}

ViewController:

[objc]viewplaincopy在CODE上查看代码片派生到我的代码片

#import

@interfaceViewController:

UIViewController

@end

/***---------------分割线---------------***/

#import"ViewController.h"

#import"HWScanViewController.h"

@implementationViewController

-(void)viewDidLoad{

[superviewDidLoad];

self.view.backgroundColor=[UIColorwhiteColor];

//创建控件

[selfcreatControl];

}

-(void)creatControl

{

//扫一扫按钮

UIButton*scanBtn=[[UIButtonalloc]initWithFrame:

CGRectMake(100,100,100,44)];

scanBtn.backgroundColor=[UIColororangeColor];

[scanBtnsetTitle:

@"扫一扫"forState:

UIControlStateNormal];

[scanBtnaddTarget:

selfaction:

@selector(scanBtnOnClick)forControlEvents:

UIControlEventTouchUpInside];

[self.viewaddSubview:

scanBtn];

}

-(void)scanBtnOnClick

{

HWScanViewController*vc=[[HWScanViewControlleralloc]init];

[self.navigationControllerpushViewController:

vcanimated:

YES];

}

@end

HWScanViewController:

[objc]viewplaincopy在CODE上查看代码片派生到我的代码片

#import

@interfaceHWScanViewController:

UIViewController

@end

/***---------------分割线---------------***/

#import"HWScanViewController.h"

#import

#defineKMainW[UIScreenmainScreen].bounds.size.width

#defineKMainH[UIScreenmainScreen].bounds.size.height

@interfaceHWScanViewController()

@property(nonatomic,strong)NSTimer*timer;

@property(nonatomic,strong)AVCaptureDevice*device;

@property(nonatomic,strong)AVCaptureSession*session;

@property(nonatomic,strong)AVCaptureVideoPreviewLayer*preview;

@property(nonatomic,weak)UIImageView*line;

@property(nonatomic,assign)NSIntegerdistance;

@end

@implementationHWScanViewController

-(void)viewDidLoad{

[superviewDidLoad];

//初始化信息

[selfinitInfo];

//创建控件

[selfcreatControl];

//设置参数

[selfsetupCamera];

//添加定时器

[selfaddTimer];

}

-(void)viewWillDisappear:

(BOOL)animated

{

[superviewWillDisappear:

animated];

[selfstopScanning];

}

-(void)initInfo

{

//背景色

self.view.backgroundColor=[UIColorblackColor];

//导航标题

self.navigationItem.title=@"二维码/条形码";

//导航右侧相册按钮

self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:

@"相册"style:

UIBarButtonItemStylePlaintarget:

selfaction:

@selector(photoBtnOnClick)];

}

-(void)creatControl

{

CGFloatscanW=KMainW*0.65;

CGFloatpadding=10.0f;

CGFloatlabelH=20.0f;

CGFloattabBarH=64.0f;

CGFloatcornerW=26.0f;

CGFloatmarginX=(KMainW-scanW)*0.5;

CGFloatmarginY=(KMainH-scanW-padding-labelH)*0.5;

//遮盖视图

for(inti=0;i<4;i++){

UIView*cover=[[UIViewalloc]initWithFrame:

CGRectMake(0,(marginY+scanW)*i,KMainW,marginY+(padding+labelH)*i)];

if(i==2||i==3){

cover.frame=CGRectMake((marginX+scanW)*(i-2),marginY,marginX,scanW);

}

cover.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:

0.5f];

[self.viewaddSubview:

cover];

}

//扫描视图

UIView*scanView=[[UIViewalloc]initWithFrame:

CGRectMake(marginX,marginY,scanW,scanW)];

[self.viewaddSubview:

scanView];

//扫描线

UIImageView*line=[[UIImageViewalloc]initWithFrame:

CGRectMake(0,0,scanW,2)];

[selfdrawLineForImageView:

line];

[scanViewaddSubview:

line];

self.line=line;

//边框

UIView*borderView=[[UIViewalloc]initWithFrame:

CGRectMake(0,0,scanW,scanW)];

borderView.layer.borderColor=[[UIColorwhiteColor]CGColor];

borderView.layer.borderWidth=1.0f;

[scanViewaddSubview:

borderView];

//扫描视图四个角

for(inti=0;i<4;i++){

CGFloatimgViewX=(scanW-cornerW)*(i%2);

CGFloatimgViewY=(scanW-cornerW)*(i/2);

UIImageView*imgView=[[UIImageViewalloc]initWithFrame:

CGRectMake(imgViewX,imgViewY,cornerW,cornerW)];

if(i==0||i==1){

imgView.transform=CGAffineTransformRotate(imgView.transform,M_PI_2*i);

}else{

imgView.transform=CGAffineTransformRotate(imgView.transform,-M_PI_2*(i-1));

}

[selfdrawImageForImageView:

imgView];

[scanViewaddSubview:

imgView];

}

//提示标签

UILabel*label=[[UILabelalloc]initWithFrame:

CGRectMake(0,CGRectGetMaxY(scanView.frame)+padding,KMainW,labelH)];

label.text=@"将二维码/条形码放入框内,即可自动扫描";

label.font=[UIFontsystemFontOfSize:

16.0f];

label.textAlignment=NSTextAlignmentCenter;

label.textColor=[UIColorwhiteColor];

[self.viewaddSubview:

label];

//选项栏

UIView*tabBarView=[[UIViewalloc]initWithFrame:

CGRectMake(0,KMainH-tabBarH,KMainW,tabBarH)];

tabBarView.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:

0.8f];

[self.viewaddSubview:

tabBarView];

//开启照明按钮

UIButton*lightBtn=[[UIButtonalloc]initWithFrame:

CGRectMake(KMainW-100,0,100,tabBarH)];

lightBtn.titleLabel.font=[UIFontsystemFontOfSize:

16.0f];

[lightBtnsetTitle:

@"开启照明"forState:

UIControlStateNormal];

[lightBtnsetTitle:

@"关闭照明"forState:

UIControlStateSelected];

[lightBtnaddTarget:

selfaction:

@selector(lightBtnOnClick:

)forControlEvents:

UIControlEventTouchUpInside];

[tabBarViewaddSubview:

lightBtn];

}

-(void)setupCamera

{

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{

//初始化相机设备

_device=[AVCaptureDevicedefaultDeviceWithMediaType:

AVMediaTypeVideo];

//初始化输入流

AVCaptureDeviceInput*input=[AVCaptureDeviceInputdeviceInputWithDevice:

_deviceerror:

nil];

//初始化输出流

AVCaptureMetadataOutput*output=[[AVCaptureMetadataOutputalloc]init];

//设置代理,主线程刷新

[outputsetMetadataObjectsDelegate:

selfqueue:

dispatch_get_main_queue()];

//初始化链接对象

_session=[[AVCaptureSessionalloc]init];

//高质量采集率

[_sessionsetSessionPreset:

AVCaptureSessionPresetHigh];

if([_sessioncanAddInput:

input])[_sessionaddInput:

input];

if([_sessioncanAddOutput:

output])[_sessionaddOutput:

output];

//条码类型(二维码/条形码)

output.metadataObjectTypes=[NSArrayarrayWithObjects:

AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeQRCode,nilnil];

//更新界面

dispatch_async(dispatch_get_main_queue(),^{

_preview=[AVCaptureVideoPreviewLayerlayerWithSession:

_session];

_preview.videoGravity=AVLayerVideoGravityResizeAspectFill;

_preview.frame=CGRectMake(0,0,KMainW,KMainH);

[self.view.layerinsertSublayer:

_previewatIndex:

0];

[_sessionstartRunning];

});

});

}

-(void)addTimer

{

_distance=0;

self.timer=[NSTimerscheduledTimerWithTimeInterval:

0.01ftarget:

selfselector:

@selector(timerAction)userInfo:

nilrepeats:

YES];

[[NSRunLoopmainRunLoop]addTimer:

self.timerforMode:

NSRunLoopCommonModes];

}

-(void)timerAction

{

if(_distance++>KMainW*0.65)_distance=0;

_line.frame=CGRectMake(0,_distance,KMainW*0.65,2);

}

-(void)removeTimer

{

[_timerinvalidate];

_timer=nil;

}

//照明按钮点击事件

-(void)lightBtnOnClick:

(UIButton*)btn

{

//判断是否有闪光灯

if(!

[_devicehasTorch]){

[selfshowAlertWithTitle:

@"当前设备没有闪光灯,无法开启照明功能"message:

nilsureHandler:

nilcancelHandler:

nil];

return;

}

btn.selected=!

btn.selected;

[_devicelockForConfiguration:

nil];

if(btn.selected){

[_devicesetTorchMode:

AVCaptureTorchModeOn];

}else{

[_devicesetTorchMode:

AVCaptureTorchModeOff];

}

[_deviceunlockForConfiguration];

}

//进入相册

-(void)photoBtnOnClick

{

if([UIImagePickerControllerisSourceTypeAvailable:

UIImagePickerControllerSourceTypePhotoLibrary]){

UIImagePickerController*controller=[[UIImagePickerControlleralloc]init];

controller.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

controller.delegate=lf;

[selfpresentViewController:

controlleranimated:

YEScompletion:

nil];

}else{

[selfshowAlertWithTitle:

@"当前设备不支持访问相册"message:

nilsureHandler:

nilcancelHandler:

nil];

}

}

#pragmamark-AVCaptureMetadataOutputObjectsDelegate

-(void)captureOutput:

(AVCaptureOutput*)captureOutputdidOutputMetadataObjects:

(NSArray*)metadataObjectsfromConnection:

(AVCaptureConnection*)connection

{

//扫描完成

if([metadataObjectscount]>0){

//停止扫描

[selfstopScanning];

//显示结果

[selfshowAlertWithTitle:

@"扫描结果"message:

[[metadataObjectsfirstObject]stringValue]sureHandler:

nilcancelHandler:

nil];

}

}

-(void)stopScanning

{

[_sessionstopRunning];

_session=nil;

[_previewremoveFromSuperlayer];

[selfremoveTimer];

}

#pragmamark-UIImagePickerControllrDelegate

-(void)imagePickerController:

(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:

(NSDictionary*)info

{

[pickerdismissViewControllerAnimated:

YEScompletion:

^{

//获取相册图片

UIImage*image=info[UIImagePickerControllerOriginalImage];

//识别图片

CIDetector*detector=[CIDetectordetectorOfType:

CIDetectorTypeQRCodecontext:

niloptions:

@{CIDetectorAccuracy:

CIDetectorAccuracyHigh}];

NSArray*features=[detectorfeaturesInImage:

[CIImageimageWithCGImage:

image.CGImage]];

//识别结果

if(features.cou

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1