ios二維碼生成
//創(chuàng)建濾鏡對象
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢復(fù)濾鏡的默認(rèn)屬性
[filter setDefaults];
NSData *infoData = [data dataUsingEncoding:NSUTF8StringEncoding];
//設(shè)置filter的InputMessages數(shù)據(jù)
[filter setValue:infoData forKeyPath:@"inputMessage"];
CIImage *outputImage = [filter outputImage];
UIImage *image = [self createUIImageFromCIImage:outputImage withWidth:width];
return image;
}
根據(jù)CIImage生成指定大小的UIImage
+ (UIImage *)createUIImageFromCIImage:(CIImage *)CIImage withWidth:(CGFloat)imageWidth{
CGRect extent = CGRectIntegral(CIImage.extent);
CGFloat scale = MIN(imageWidth / CGRectGetWidth(extent), imageWidth / CGRectGetHeight(extent));
//創(chuàng)建bitMap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:CIImage fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
+ (UIImage *)generateLogoQRCode:(NSString *)data imageWidth:(CGFloat)imageWidth logoImageName:(NSString *)logoImageName logoScaleToSuperView:(CGFloat)logoScale{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢復(fù)濾鏡的默認(rèn)屬性
[filter setDefaults];
NSData *infoData = [data dataUsingEncoding:NSUTF8StringEncoding];
//設(shè)置filter的InputMessages數(shù)據(jù)
[filter setValue:infoData forKeyPath:@"inputMessage"];
CIImage *outputImage = [filter outputImage];
// outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
//將CIImage轉(zhuǎn)化為UIImage類型
UIImage *start_image = [self createUIImageFromCIImage:outputImage withWidth:imageWidth];
//添加logo
UIGraphicsBeginImageContext(start_image.size);
[start_image drawInRect:CGRectMake(0, 0, start_image.size.width, start_image.size.height)];
//畫logo
UIImage *logoImage = [UIImage imageNamed:logoImageName];
CGFloat logoWidth = start_image.size.width * logoScale;
CGFloat logoHeight = start_image.size.height * logoScale;
CGFloat logoX = (start_image.size.width - logoWidth) / 2;
CGFloat logoY = (start_image.size.width - logoHeight) / 2;
[logoImage drawInRect:CGRectMake(logoX, logoY, logoWidth, logoHeight)];
//獲取最后的圖片
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
掃描
掃描前需要申請調(diào)用攝像頭的權(quán)限
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//創(chuàng)建輸入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//創(chuàng)建輸出流
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
//設(shè)置代理签夭,在主線程中刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//設(shè)置掃描范圍(取值范圍0-1,一屏幕右上角為坐標(biāo)原點(diǎn))
output.rectOfInterest = CGRectMake(0.2, 0.2, 0.6, 0.6);
_session = [[AVCaptureSession alloc] init];
//設(shè)置采集率
[_session setSessionPreset:AVCaptureSessionPresetHigh];
//添加會話輸入輸出
[_session addInput:input];
[_session addOutput:output];
//設(shè)置輸出數(shù)據(jù)類型弟灼,需要將元數(shù)據(jù)輸出添加到會話中,才能指定元數(shù)據(jù)類型
//設(shè)置掃碼支持的編碼格式
output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeCode128Code];
//初始化previewLayer鹤树,傳遞給session將要顯示什么
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.frame = self.view.layer.bounds;
//將圖層插入當(dāng)前視圖
[self.view.layer insertSublayer:_previewLayer atIndex:0];
//啟動會話
[_session startRunning];
具體代碼見GitHub:https://github.com/huyong668/HYQRCode