因為現(xiàn)在二維碼隨處可見,無論是實物商品還是各種禮券都少不了二維碼的身影宇姚。所以我們開發(fā)的時候有時候也需要用到二維碼的掃描,或者生成夫凸。今天就二維碼我們做一個簡單的實驗浑劳。
一、二維碼的生成
從 iOS 7 開始集成了二維碼的生成和讀取功能夭拌,所以這方法也僅支持 iOS 7.0 以上魔熏。
1、首先導入 CoreImage 框架
2鸽扁、通過濾鏡 CIFilter 生成二維碼
完成之后運行工程蒜绽,我們發(fā)現(xiàn)這樣顯示的圖片不是很清晰,可以自己重繪圖片桶现,重新獲得高清的二維碼圖片躲雅。
這是實現(xiàn)方法,會返回一個 UIImage 的對象:
OK骡和,現(xiàn)在可以看一下這兩種方法生成的二維碼相赁,做一個對比:
打開我們們自己的手機,掃描一下這個生成的二維碼慰于,會發(fā)現(xiàn)我們放在 inputMessage 里面的那句話就顯示在手機上面了钮科,那如果是網址的話,改一下 inputMessage 的內容就可以了婆赠,這樣掃描完會直接打開所對應的網址:
附上上面的完整代碼:
// 1.創(chuàng)建過濾器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢復濾鏡的默認屬性
[filter setDefaults];
// 3.給過濾器添加數據
NSString *dataString = @"這是一個二維碼的生成結果";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
// 4.獲取輸出的二維碼
CIImage *outputImage = [filter outputImage];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 175, 175)];
// 5.顯示二維碼
imageView.image = [UIImage imageWithCIImage:outputImage];
[self.view addSubview:imageView];
// 因為生成的二維碼模糊,重新獲得高清的二維碼圖片
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 175, 175)];
newImageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:175];
[self.view addSubview:newImageView];
實現(xiàn)方法:
/**
* 根據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、注意:
二維碼掃碼需要使用攝像頭,需要真機設備桨嫁,模擬器不行植兰。
2、二維碼掃描的過程
通過攝像頭掃描二維碼
解析二維碼數據
根據二維碼信息執(zhí)行相應的操作
還是同樣的璃吧,先導入所需要的頭文件楣导,遵循代理協(xié)議,聲明所需要的屬性:
接著就是實現(xiàn),可以寫在button的點擊方法里面:
當掃描到結果后會調用的代理方法:
OK巴元,現(xiàn)在先找一個二維碼毡咏,然后運行工程,掃描剛剛找的這個二維碼逮刨,看一下打印結果:
同樣附上掃描的全部代碼:
// 1.創(chuàng)建捕捉會話
self.session = [[AVCaptureSession alloc] init];
// 2.添加輸入設備(數據從攝像頭輸入)
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[self.session addInput:input];
// 3.添加輸出數據
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[self.session addOutput:output];
// 3.1.設置輸入元數據的類型(類型是二維碼數據)
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4.添加掃描圖層
self.layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.layer.frame = self.view.bounds;
[self.view.layer addSublayer:self.layer];
// 5.開始掃描
[self.session startRunning];
代理方法:
// 當掃描到數據時就會執(zhí)行該代理方法,會頻繁的掃描呕缭,調用代理方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count > 0) {
//獲得掃描數據,最后一個時最新掃描的數據
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
NSLog(@"%@", object.stringValue);
// 停止掃描
[self.session stopRunning];
// 將預覽圖層移除
[self.layer removeFromSuperlayer];
} else {
NSLog(@"沒有掃描到數據");
}
}