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]; //進行處理(音效、網址分析、頁面跳轉等) }