二维码生成和扫一扫实现
1、AVCaptureSession 实现识别二维码功能
@property(nonatomic, strong)AVCaptureMetadataOutput *metadataOutput;//!< 输出
@property(nonatomic, strong)AVCaptureSession *session;//!< 会话
@property(nonatomic, strong)AVCaptureVideoPreviewLayer *previewLayer;//!< 预览
@property(nonatomic, strong)JWDPreView *preView;//!< <#value#>
@end
@implementation JWDQRCodeViewController
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super init];
if (self) {
self.view.frame = frame;
[self initUiConfig];
}
return self;
}
-(void)initUiConfig {
// 默认为后置摄像头
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:NULL];
// 解析输入的数据
self.metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 会话
self.session = [[AVCaptureSession alloc] init];
if ([self.session canAddInput:self.deviceInput]){
[self.session addInput:self.deviceInput];
}
if([self.session canAddOutput:self.metadataOutput]){
[self.session addOutput:self.metadataOutput];
}
// 设置数据采集质量
self.session.sessionPreset = AVCaptureSessionPresetHigh;
// 设置需要解析的数据类型,二维码
self.metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
JWDPreView *preView = [[JWDPreView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.preView = preView;
[self.view addSubview:preView];
preView.session = self.session;
preView.backPreView = ^(JWDPreView *backPreView){
[self dismissViewControllerAnimated:YES completion:nil];
// 销毁定时器
if (backPreView.timer){
[backPreView.timer invalidate];
backPreView.timer = nil;
}
};
[self.session startRunning];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:obj.stringValue]];
[self presentViewController:safariVC animated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
2、CIFilter 实现二维码生成
- (void)initUiConfigWithQRCodeString:(NSString *)qRCodeString withQRCodeCenterImage:(NSString *)qRCodeCenterImage {
// 1 创建二维码过滤器
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setDefaults];
// 2 生成二维码
NSData *data = [qRCodeString dataUsingEncoding:NSUTF8StringEncoding];
[qrFilter setValue:data forKey:@"inputMessage"];
CIImage *codeImage = qrFilter.outputImage;
// 3 拉伸
codeImage = [codeImage imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
// 4 绘制二维码
UIImage *qrImage = [UIImage imageWithCIImage:codeImage];
UIImage *meImage = [UIImage imageNamed:qRCodeCenterImage];
UIGraphicsBeginImageContext(qrImage.size);
[qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];
CGFloat meImageX = (qrImage.size.width - KmeImageW) * 0.5;
CGFloat meImageY = (qrImage.size.height - KmeImageH) * 0.5;
[meImage drawInRect:CGRectMake(meImageX, meImageY, KmeImageW, KmeImageH)];
UIImage *filnalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 5 获取绘制好的二维码
self.image = filnalImage;
}
2、CIFilter 实现条形码生成
/**
生成条形码
*/
(IBAction)generateBarCode:(id)sender {
CIImageciImage = [self generateBarCodeImage:@"1234567890"];
UIImage image = [self resizeCodeImage:ciImage withSize:CGSizeMake((self.view.frame.size.width - 80), 80)];
_barCodeImageView.image = image;
}
/
生成条形码
@param source
@return 生成条形码的CIImage对象
*/
(CIImage *) generateBarCodeImage:(NSString *)source
{
// iOS 8.0以上的系统才支持条形码的生成,iOS8.0以下使用第三方控件生成
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 注意生成条形码的编码方式
NSData *data = [source dataUsingEncoding: NSASCIIStringEncoding];
CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
// 设置生成的条形码的上,下,左,右的margins的值
[filter setValue:[NSNumber numberWithInteger:0] forKey:@"inputQuietSpace"];
return filter.outputImage;
}else{
return nil;
}
}
/**
调整生成的图片的大小
@param image CIImage对象
@param size 需要的UIImage的大小
@return size大小的UIImage对象
*/
(UIImage *) resizeCodeImage:(CIImage *)image withSize:(CGSize)size
{
if (image) {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
size_t width = CGRectGetWidth(extent) * scaleWidth;
size_t height = CGRectGetHeight(extent) * scaleHeight;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
CGContextDrawImage(contentRef, extent, imageRef);
CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
CGContextRelease(contentRef);
CGImageRelease(imageRef);
return [UIImage imageWithCGImage:imageRefResized];
}else{
return nil;
}
}