IOS筆記061之二維碼的產生和掃描_IOS

來源:互聯網
上載者:User

如今二維碼隨處可見,無論是實物商品還是各種禮券都少不了二維碼的身影。而手機等行動裝置又成為二維碼的一個很好的應用平台,不管是產生二維碼還是掃碼二維碼。本篇文章從產生二維碼、掃描二維碼展開分析,通過內容分析二維碼用起來也很easy了。

首先說下產生二維碼

       二維碼可以存放純文字、名片或者URL

其次產生二維碼的步驟:

       匯入CoreImage架構

再次通過濾鏡CIFilter產生二維碼

1、建立過濾器

2、恢複濾鏡的預設屬性

3、設定內容

4、擷取輸出檔案

5、顯示二維碼

代碼實現 CoreImage

 // 二維碼的產生 // 1、建立過濾器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2、恢複濾鏡的預設屬性 [filter setDefaults]; // 3、設定內容 NSString *str = @"這是一個二維碼的產生結果"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; // 使用KVO設定屬性 [filter setValue:data forKey:@"inputMessage"]; // 4、擷取輸出檔案 CIImage *outputImage = [filter outputImage]; // 5、顯示二維碼 self.imageView.image = [UIImage imageWithCIImage:outputImage];

這樣顯示的圖片不是很清晰,可以自己重繪圖片

重建高清圖片:網上找即可,具體過程可暫時不關心

/** * 根據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.建立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];}

還有就是設定內容為網址時,如果帶有協議頭的話,會自動開啟網頁。

NSString *str = @http://www.baidu.com;

必須帶有協議頭才能開啟

回到頂部

掃描二維碼

AVFoundation架構

二維碼的掃描過程

1、建立捕捉會話AVCaptureSession

添加輸入裝置(資料從網路攝影機輸入) AVCaptureDevice AVCaptureDeviceInput

2、添加輸出資料(樣本對象-->類對象-->元類對象-->根元類對象) AVCaptureMetadataOutput

2.1.設定輸入中繼資料的類型(類型是二維碼資料) setMetadataObjectTypes

3、添加掃描圖層 AVCaptureVideoPreviewLayer

4、開始掃描 startRunning

5、實現回調代理方法,擷取掃描結果 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、建立捕捉會話 AVCaptureSession *session = [[AVCaptureSession alloc] init]; self.session = session; // 2.添加輸入裝置(資料從網路攝影機輸入) AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:input]; // 3、添加輸出資料(樣本對象-->類對象-->元類對象-->根元類對象) AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [session addOutput:output]; // 3.1.設定輸入中繼資料的類型(類型是二維碼資料) [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];}/** * 實現output的回調方法 */- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ // 數組metadataObjects中存放結果資料 if (metadataObjects.count > 0) { // 擷取最終的讀取結果 AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; NSLog(@"%@",object.stringValue); [self.session stopRunning]; [self.layer removeFromSuperlayer]; } else { NSLog(@"沒有掃描到資料"); }}

總結一句話:這個二維碼使用起來也不難

本文就到此為止,IOS筆記061之二維碼的產生和掃描希望在今後的工作和學習能夠協助到大家。下面文章給大家分享如何在蘋果iOS裝置上使用二維碼,需要瞭解的朋友點擊這裡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.