iOS开发:使用苹果自带框架实现二维码和条形码扫描
来源:互联网 发布:11月份宏观经济数据 编辑:程序博客网 时间:2024/05/21 11:12
扫描二维码和条形码,用的是AVFoundation,判断如果是URL就打开web页面。
预览
思路
- AVFoundation框架很强大,调摄像头各种硬件
- 判断URL用正则表达式
主要代码
//// CaptureViewController.m// ScanCode//// Created by yxhe on 16/6/2.// Copyright © 2016年 yxhe. All rights reserved.//#import <AVFoundation/AVFoundation.h>#import "CaptureViewController.h"#import "BrowserViewController.h"const char *kScanCodeQueueName = "ScanCodeQueue"; //the new process queue indentifier@interface CaptureViewController ()<AVCaptureMetadataOutputObjectsDelegate>{ AVCaptureSession *session; //the session of captrue BOOL isLightOpen;}@property (nonatomic, strong) UIView *cameraView; //the camera frame view@property (nonatomic, strong) UIButton *openLightBtn; //open the light@end@implementation CaptureViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. //add the cameara view _cameraView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width)]; _cameraView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:_cameraView]; //add a button to switch light isLightOpen = NO; UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeCustom]; lightButton.frame = CGRectMake(self.view.frame.size.width/2 - 50, 500, 100, 50); [lightButton setTitle:@"open_light" forState:UIControlStateNormal]; [lightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [lightButton setTitleColor:[UIColor greenColor] forState:UIControlEventTouchDown]; [lightButton addTarget: self action:@selector(switchLight) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:lightButton]; //start capture [self initCapture]; [session startRunning]; }- (void)loadView{ [super loadView];// [self initCapture];}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated];// [self initCapture];}- (void)switchLight{ AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //toggle the light isLightOpen = !isLightOpen; //turn on or off the light if([device hasTorch]) { [device lockForConfiguration:nil]; if(isLightOpen) [device setTorchMode:AVCaptureTorchModeOn]; else [device setTorchMode:AVCaptureTorchModeOff]; [device unlockForConfiguration]; } }- (void)initCapture{ //get the camera device AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //create the session session = [[AVCaptureSession alloc]init]; //create input NSError *error; AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if(!input) { NSLog(@"%@", [error localizedDescription]); return; } [session addInput:input]; //create output AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; [session addOutput:output]; //make it run at a new process dispatch_queue_t dispatchQueue; dispatchQueue = dispatch_queue_create(kScanCodeQueueName, NULL); [output setMetadataObjectsDelegate:self queue:dispatchQueue]; //set the sampling rate [session setSessionPreset:AVCaptureSessionPresetHigh]; //set the code type ,ex:QR and bar code if([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) [output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, nil]]; //add the camera layer AVCaptureVideoPreviewLayer *captureLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; captureLayer.videoGravity=AVLayerVideoGravityResizeAspectFill; captureLayer.frame=self.cameraView.layer.bounds; [self.cameraView.layer insertSublayer:captureLayer atIndex:0]; //or use the addsublayer// [self.cameraView.layer addSublayer:captureLayer]; }#pragma mark - capture delegate-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects && metadataObjects.count>0) { //stop the capture [session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; //do sth to handle the capture result [self performSelectorOnMainThread:@selector(handleResult:) withObject:metadataObject waitUntilDone:NO]; }}- (void)handleResult:(AVMetadataMachineReadableCodeObject *)metadataObject{ NSLog(@"%@", metadataObject.type); if([metadataObject.type isEqualToString:AVMetadataObjectTypeQRCode]) NSLog(@"QR code"); else NSLog(@"other code"); NSString *captureStr = metadataObject.stringValue; NSLog(@"%@",captureStr); //if the string is URL, then push a view and open the website, otherwise just pop a dialog to show the msg if([self isURL:captureStr]) { BrowserViewController *browserViewController = [[BrowserViewController alloc] init]; browserViewController.urlStr = captureStr; [self.navigationController pushViewController:browserViewController animated:YES]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"scan result" message:captureStr delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show]; }}//use regular expression to recognize URL- (BOOL)isURL:(NSString *)str{ NSString *regex = @"[a-zA-z]+://[^s]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; return [predicate evaluateWithObject:str];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
其中二维码识别较灵敏,条形码稍显迟钝。
源代码下载
csdn:二维码扫码github:ScanCode
0 0
- iOS开发:使用苹果自带框架实现二维码和条形码扫描
- IOS下使用AVFoundation实现条形码和二维码扫描
- iOS 使用ZBarSDK扫描条形码和二维码
- iOS 使用ZBarSDK扫描条形码和二维码
- iOS 使用ZBarSDK扫描条形码和二维码
- iOS 使用ZBarSDK扫描条形码和二维码
- iOS7自带扫描二维码、条形码功能实现
- iOS7自带扫描二维码、条形码功能实现
- 使用苹果原生API进行二维码和条形码的扫描
- 使用苹果原生API进行二维码和条形码的扫描
- 使用 IOS 自带 API 扫描二维码
- iOS自带扫描 和 生成二维码
- iOS自带扫描 和 生成二维码
- iOS自带扫描 和 生成二维码
- iOS自带扫描 和 生成二维码
- iOS自带扫描和生成二维码
- iOS开发--二维码/条形码(扫描(可区域)和生成)
- [IPHONE] iOS使用ZbarSDK实现扫描二维码以及条形码功能
- CSS 实现导航条
- VirtualBox中的Centos安装增强功能包VBoxLinuxAdditions和共享本机文件夹
- MathType的三个使用技巧
- 高德地图定位相关API
- tcltk实时读写文件【创建log-debug】
- iOS开发:使用苹果自带框架实现二维码和条形码扫描
- Java 集合框架
- ORACLE 表如何count最快?
- 你有什么道理后悔没有早点知道?
- hadoop实践(一)环境准备
- Walker 之侧滑菜单的实现
- (Builder)建造者模式的Java实现
- VC获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
- 对AlertDialog.Builder进行封装