IOS筆記061之二維碼的生成和掃描
如今二維碼隨處可見,無論是實物商品還是各種禮券都少不了二維碼的身影。而手機等移動設(shè)備又成為二維碼的一個很好的應(yīng)用平臺,不管是生成二維碼還是掃碼二維碼。本篇文章從生成二維碼、掃描二維碼展開分析,通過內(nèi)容分析二維碼用起來也很easy了。
首先說下生成二維碼
二維碼可以存放純文本、名片或者URL
其次生成二維碼的步驟:
導入CoreImage框架
再次通過濾鏡CIFilter生成二維碼
1、創(chuàng)建過濾器
2、恢復濾鏡的默認屬性
3、設(shè)置內(nèi)容
4、獲取輸出文件
5、顯示二維碼
代碼實現(xiàn) CoreImage
// 二維碼的生成 // 1、創(chuàng)建過濾器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2、恢復濾鏡的默認屬性 [filter setDefaults]; // 3、設(shè)置內(nèi)容 NSString *str = @"這是一個二維碼的生成結(jié)果"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; // 使用KVO設(shè)置屬性 [filter setValue:data forKey:@"inputMessage"]; // 4、獲取輸出文件 CIImage *outputImage = [filter outputImage]; // 5、顯示二維碼 self.imageView.image = [UIImage imageWithCIImage:outputImage];
這樣顯示的圖片不是很清晰,可以自己重繪圖片
重新生成高清圖片:網(wǎng)上找即可,具體過程可暫時不關(guān)心
/** * 根據(jù)CIImage生成指定大小的UIImage * * @param image CIImage * @param size 圖片寬度 */ - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 1.創(chuàng)建bitmap; size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 2.保存bitmap到圖片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; }
還有就是設(shè)置內(nèi)容為網(wǎng)址時,如果帶有協(xié)議頭的話,會自動打開網(wǎng)頁。
NSString *str = @http://www.baidu.com;
必須帶有協(xié)議頭才能打開
回到頂部
掃描二維碼
AVFoundation框架
二維碼的掃描過程
1、創(chuàng)建捕捉會話AVCaptureSession
添加輸入設(shè)備(數(shù)據(jù)從攝像頭輸入) AVCaptureDevice AVCaptureDeviceInput
2、添加輸出數(shù)據(jù)(示例對象-->類對象-->元類對象-->根元類對象) AVCaptureMetadataOutput
2.1.設(shè)置輸入元數(shù)據(jù)的類型(類型是二維碼數(shù)據(jù)) setMetadataObjectTypes
3、添加掃描圖層 AVCaptureVideoPreviewLayer
4、開始掃描 startRunning
5、實現(xiàn)回調(diào)代理方法,獲取掃描結(jié)果 captureOutput: :
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> /**顯示圖層*/ @property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer; /**捕捉會話*/ @property (nonatomic, strong) AVCaptureSession *session; @end @implementation ViewController - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 二維碼的掃描 // 1、創(chuàng)建捕捉會話 AVCaptureSession *session = [[AVCaptureSession alloc] init]; self.session = session; // 2.添加輸入設(shè)備(數(shù)據(jù)從攝像頭輸入) AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:input]; // 3、添加輸出數(shù)據(jù)(示例對象-->類對象-->元類對象-->根元類對象) AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [session addOutput:output]; // 3.1.設(shè)置輸入元數(shù)據(jù)的類型(類型是二維碼數(shù)據(jù)) [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; // 4、添加掃描圖層 AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.frame = self.view.bounds; [self.view.layer addSublayer:layer]; self.layer = layer; // 5、開始掃描 [session startRunning]; } /** * 實現(xiàn)output的回調(diào)方法 */ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { // 數(shù)組metadataObjects中存放結(jié)果數(shù)據(jù) if (metadataObjects.count > 0) { // 獲取最終的讀取結(jié)果 AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; NSLog(@"%@",object.stringValue); [self.session stopRunning]; [self.layer removeFromSuperlayer]; } else { NSLog(@"沒有掃描到數(shù)據(jù)"); } }
總結(jié)一句話:這個二維碼使用起來也不難
本文就到此為止,IOS筆記061之二維碼的生成和掃描希望在今后的工作和學習能夠幫助到大家。下面文章給大家分享如何在蘋果iOS設(shè)備上使用二維碼,需要了解的朋友點擊這里。
相關(guān)文章
IOS UITableViewCell詳解及按鈕點擊事件處理實例
這篇文章主要介紹了IOS UITableViewCell詳解及按鈕點擊事件處理實例的相關(guān)資料,這里附有示例代碼,大家可以看下如何實現(xiàn)按鍵點擊事件,需要的朋友可以參考下2016-12-12iOS應(yīng)用開發(fā)中UITableView的分割線的一些設(shè)置技巧
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UITableView分割線的一些設(shè)置技巧,包括消除分割線的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03