1、生成二维码(使用coreImage.framework)session
- (CIImage *)createQRForString:(NSString *)qrString {
// Need to convert the string to a UTF-8 encoded NSData object
NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// Create the filter
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// Set the message content and error-correction level
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
// Send the image back
return qrFilter.outputImage;
}ide
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// create a bitmap image that we'll draw into a bitmap context at the desired size;
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);
// Create an image with the contents of our bitmap
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
// Cleanup
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [self imageBlackToTransparent:[UIImage imageWithCGImage:scaledImage] withRed:20.0f andGreen:70.0f andBlue:60.0f];
}
void ProviderReleaseData (void *info, const void *data, size_t size){
free((void*)data);
}atom
而后用一句话调用spa
[self.imageview setImage:[self createNonInterpolatedUIImageFormCIImage:[self createQRForString:@"今天"] withSize:size]];代理
就可生成二维码。rest
2、扫描二维码、条形码(使用AVFoundation.framework)code
1.导入#import <AVFoundation/AVFoundation.h>,遵循AVCaptureMetadataOutputObjectsDelegate协议
,并实现- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection这个方法。orm
2.声明图片
@property (strong,nonatomic)AVCaptureDevice * device; //设备
@property (strong,nonatomic)AVCaptureDeviceInput * input; //输入流
@property (strong,nonatomic)AVCaptureMetadataOutput * output; //输出流
@property (strong,nonatomic)AVCaptureSession * session;//捕捉会话
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview; //预览图层ci
在viewDidLoad中实现
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setRectOfInterest:CGRectMake((Height/3.0-10)/Height,0,(Height/3.0+20)/Height,1)];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
self.session = [[AVCaptureSession alloc]init];
if ([_session canSetSessionPreset:AVCaptureSessionPreset1920x1080])
{
[_session setSessionPreset:AVCaptureSessionPreset1920x1080];
}
else if ([_session canSetSessionPreset:AVCaptureSessionPreset1280x720])
{
[_session setSessionPreset:AVCaptureSessionPreset1280x720];
}
else
{
[_session setSessionPreset:AVCaptureSessionPresetPhoto];
}
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
// _output.metadataObjectTypes =@[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code];
_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame =CGRectMake(0,0,Width,Height);
[self.view.layer insertSublayer:self.preview atIndex:0];
// 开启
[_session startRunning];
在代理方法中实现下面代码
if ([metadataObjects count] >0)
{
//结束
[_session stopRunning];
//扫描到之后
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
NSLog(@"%@",metadataObject.stringValue);
[self playBeep];//添加声音
self.textlabel.text=[NSString stringWithFormat:@"扫描信息:%@",metadataObject.stringValue];
}
使用此方法能够添加扫描到信息成功的声音
- (void)playBeep{
SystemSoundID soundID;
NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",@"lock",@"caf"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
3、识别相册中的二维码(使用coreImage.framework)
1.不用导入文件,遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate两个协议
2.打开相册
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
3.
//选中图片的回调 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *content = @"" ; //取出选中的图片 UIImage *pickImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImagePNGRepresentation(pickImage); CIImage *ciImage = [CIImage imageWithData:imageData]; //建立探测器 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}]; NSArray *feature = [detector featuresInImage:ciImage]; //取出探测到的数据 for (CIQRCodeFeature *result in feature) { content = result.messageString; } NSLog(@"%@",content); [picker dismissViewControllerAnimated:YES completion:nil]; //进行处理(音效、网址分析、页面跳转等) }