filter 濾鏡
使用二維碼要使用這個(gè)框架
生成二維碼
#import"ViewController.h"
#import
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*Qrcode;//Qr code二維碼
@end
@implementationViewController
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
//1.實(shí)例化二維碼濾鏡CIQRCodeGenerator是一個(gè)固定的值
CIFilter*filter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];
//2.恢復(fù)濾鏡的默認(rèn)屬性,因?yàn)榭赡鼙4嬗猩弦淮螢V鏡的屬性
[filtersetDefaults];
//3.將字符串轉(zhuǎn)為NSData數(shù)據(jù)
NSData*data = [@"http://www.baidu.com"dataUsingEncoding:NSUTF8StringEncoding];
//4.通過KVO設(shè)置濾鏡,傳入data ,將來濾鏡就知道通過data來生成二維碼
//inputMessage是一個(gè)固定的值
[filtersetValue:dataforKey:@"inputMessage"];
//5.通過濾鏡生成二維碼
CIImage*ciImage = [filteroutputImage];
//6.將CIImage轉(zhuǎn)為UIImage
UIImage*uiImage = [UIImageimageWithCIImage:ciImage];
//7.展示二維碼
self.Qrcode.image= uiImage;
}
@end
掃描二維碼
#import"ViewController.h"
#import
@interfaceViewController()
@property(nonatomic,strong)AVCaptureSession*session;
@property(nonatomic,strong)AVCaptureVideoPreviewLayer*preview;
@property(weak,nonatomic)IBOutletUILabel*labelView;
@end
@implementationViewController
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
//1.實(shí)例化拍攝設(shè)備
AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
//2.設(shè)置輸入設(shè)備
AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];
//3.設(shè)置元數(shù)據(jù)輸出
//3.1實(shí)例化拍攝元數(shù)據(jù)輸出
AVCaptureMetadataOutput*output = [[AVCaptureMetadataOutputalloc]init];
//3.2設(shè)置輸出數(shù)據(jù)的代理
[outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
//4.添加拍攝會(huì)話
//4.1實(shí)例化拍攝會(huì)話
AVCaptureSession*session = [[AVCaptureSessionalloc]init];
//4.2添加輸出會(huì)話
[sessionaddOutput:output];
//4.3添加輸入會(huì)話
[sessionaddInput:input];
//4.4設(shè)置輸出數(shù)據(jù)類型,必須先將元數(shù)據(jù)輸出添加到會(huì)話中,才能設(shè)置元數(shù)據(jù)類型,否則會(huì)報(bào)錯(cuò)
[outputsetMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
self.session= session;
//5.實(shí)例化預(yù)覽圖層
//5.1實(shí)例化預(yù)覽圖層,傳遞_session是為了告訴預(yù)覽圖層要顯示什么內(nèi)容的
AVCaptureVideoPreviewLayer*preview = [AVCaptureVideoPreviewLayerlayerWithSession:_session];
//5.2設(shè)置預(yù)覽圖層的屬性
preview.videoGravity=AVLayerVideoGravityResizeAspectFill;
preview.frame=self.view.bounds;
//5.3將圖層添加到當(dāng)前視圖
[self.view.layerinsertSublayer:previewatIndex:100];
self.preview= preview;
//6.啟動(dòng)會(huì)話
[_sessionstartRunning];
}
/**
*? AVCaptureMetadataOutputObjectsDelegate方法
*/
-(void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection{
//1.如果掃描成功,停止會(huì)話
[self.sessionstopRunning];
//2.移除預(yù)覽圖層
[self.previewremoveFromSuperlayer];
if(metadataObjects.count>0) {
AVMetadataMachineReadableCodeObject*obj = metadataObjects[0];
self.labelView.text= obj.stringValue;
}
}
@end