ISO7 二維碼詳細(xì)介紹及使用方法
使用系統(tǒng)自帶生成/掃描二維碼
iOS7開始蘋果集成了二維碼的生成的掃描 ### 生成二維碼的步驟
導(dǎo)入CoreImage框架 #import <CoreImage/CoreImage.h>
通過濾鏡CIFilte生成二維碼 ### 二維碼的內(nèi)容(傳統(tǒng)的條形碼只能放數(shù)字)
純文本
名片
URL
生成二維碼
// 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來獲得高清的二維碼圖片
// 5.顯示二維碼
self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:300];
/**
* 根據(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];
掃描會(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ù)");
}
}
以上就是對(duì)IOS 二維碼的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
- 使用jQuery.Qrcode插件在客戶端動(dòng)態(tài)生成二維碼并添加自定義Logo
- 基于Bootstrap的Metronic框架實(shí)現(xiàn)條碼和二維碼的生成及打印處理操作
- jQuery實(shí)現(xiàn)微信長(zhǎng)按識(shí)別二維碼功能
- Java利用Zxing生成二維碼的簡(jiǎn)單實(shí)例
- 掃描二維碼控件的封裝iOS實(shí)現(xiàn)
- jQuery 生成svg矢量二維碼
- Java 二維碼,QR碼,J4L-QRCode 的資料整理
- Java 生成二維碼的工具資料整理
- java 二維碼的生成與解析示例代碼
- Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼與示例解析
- Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
相關(guān)文章
Objective-C方法的聲明實(shí)現(xiàn)及調(diào)用方法
這篇文章主要介紹了Objective-C方法的聲明實(shí)現(xiàn)及調(diào)用方法,包括五參數(shù)的方法和單個(gè)參數(shù)的方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-02-02
iOS 下拉刷新動(dòng)畫的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了iOS 下拉刷新動(dòng)畫的實(shí)現(xiàn)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
iOS開發(fā)實(shí)現(xiàn)搜索框(UISearchController)
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)搜索框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
ios UITableView實(shí)現(xiàn)無數(shù)據(jù)加載占位圖片
這篇文章主要介紹了ios UITableView實(shí)現(xiàn)無數(shù)據(jù)占位圖片,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

