iOS二維碼的生成和掃描
本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
屬性
@property (strong,nonatomic)AVCaptureDevice * device; @property (strong,nonatomic)AVCaptureDeviceInput * input; @property (strong,nonatomic)AVCaptureMetadataOutput * output; @property (strong,nonatomic)AVCaptureSession * session; @property (strong,nonatomic)AVCaptureVideoPreviewLayer * layer; @property (nonatomic, strong)UIImageView *imageView;
二維碼的生成
// 1.創(chuàng)建過濾器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2.恢復(fù)默認(rèn) [filter setDefaults]; // 3.給過濾器添加數(shù)據(jù)(正則表達(dá)式/賬號(hào)和密碼) NSString *dataString = @"http://www.520it.com"; NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKeyPath:@"inputMessage"]; // 4.獲取輸出的二維碼 CIImage *outputImage = [filter outputImage]; //因?yàn)樯傻亩S碼模糊,所以通過createNonInterpolatedUIImageFormCIImage:outputImage來(lái)獲得高清的二維碼圖片 // 5.顯示二維碼 self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
* createNonInterpolatedUIImageFormCIImage:outputImage方法的實(shí)現(xià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]; }
二維碼的掃描
// 1.創(chuàng)建捕捉會(huì)話 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ù)(示例對(duì)象-->類對(duì)象-->元類對(duì)象-->根元類對(duì)象) 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];
*掃描到結(jié)果后會(huì)調(diào)用的方法
// 當(dāng)掃描到數(shù)據(jù)時(shí)就會(huì)執(zhí)行該方法 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects.count > 0) { //獲得掃描數(shù)據(jù),最后一個(gè)時(shí)最新掃描的數(shù)據(jù) AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; NSLog(@"%@", object.stringValue); // 停止掃描 [self.session stopRunning]; // 將預(yù)覽圖層移除 [self.layer removeFromSuperlayer]; } else { NSLog(@"沒有掃描到數(shù)據(jù)"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ios NSNotificationCenter通知的簡(jiǎn)單使用
這篇文章主要介紹了ios NSNotificationCenter通知的簡(jiǎn)單使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-06-06iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵
這篇文章主要介紹了iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵 的相關(guān)資料,需要的朋友可以參考下2016-08-08iOS開發(fā)Quick Actions創(chuàng)建桌面Icon快捷方式
在本文里我們給大家分享了關(guān)于iOS開發(fā)Quick Actions創(chuàng)建桌面Icon快捷方式的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的讀者們可以參考下。2019-05-05全面解析iOS應(yīng)用中自定義UITableViewCell的方法
這篇文章主要介紹了iOS應(yīng)用開發(fā)中自定義UITableViewCell的方法,示例為傳統(tǒng)的Obejective-C語(yǔ)言,需要的朋友可以參考下2016-04-04詳解iOS 關(guān)于字體根據(jù)不同屏幕尺寸等比適配的問題
這篇文章主要介紹了詳解iOS 關(guān)于字體根據(jù)不同屏幕尺寸等比適配的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06iOS定時(shí)器的選擇CADisplayLink NSTimer和GCD使用
這篇文章主要為大家介紹了iOS定時(shí)器的選擇CADisplayLink NSTimer和GCD使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03