生成二維碼
通過濾鏡生成二維碼====》具體代碼如下
#import <CoreImage/CoreImage.h>
- (void)creatQRCodeImage {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//創(chuàng)建過濾器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢復默認設(shè)置
[filter setDefaults];
//添加數(shù)據(jù)
NSData *data = [@"wangzhepeng" dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
//生成二維碼(有點模糊)
CIImage *ciImage = [filter outputImage];
/**生成高清圖片 */
CGAffineTransform transform = CGAffineTransformMakeScale(10, 10);
CIImage *transformedImage = [ciImage imageByApplyingTransform:transform];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:transformedImage fromRect:transformedImage.extent];
UIImage *qrImage = [UIImage imageWithCGImage:cgImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGImageRelease(cgImage);
if (!qrImage) {
//二維碼中添加頭像
qrImage = [self qrcodeImage:qrImage addAvatar:nil scale:0.2];
}
dispatch_async(dispatch_get_main_queue(), ^{
img = qrImage;
self.qrImageView.image = qrImage;
});
});
}
- (UIImage *)qrcodeImage:(UIImage *)qrImage addAvatar:(UIImage *)avatar scale:(CGFloat)scale {
CGFloat screenScale = [UIScreen mainScreen].scale;
CGRect rect = CGRectMake(0, 0, qrImage.size.width * screenScale, qrImage.size.height * screenScale);
UIGraphicsBeginImageContextWithOptions(rect.size, YES, screenScale);
[qrImage drawInRect:rect];
CGSize avatarSize = CGSizeMake(rect.size.width * scale, rect.size.height * scale);
CGFloat x = (rect.size.width - avatarSize.width) * 0.5;
CGFloat y = (rect.size.height - avatarSize.height) * 0.5;
[avatar drawInRect:CGRectMake(x, y, avatarSize.width, avatarSize.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIImage imageWithCGImage:result.CGImage scale:screenScale orientation:UIImageOrientationUp];
}
- (UIImageView *)qrImageView {
if (_qrImageView == nil) {
_qrImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 80, kScreenW-160, kScreenW-160)];
[self.view addSubview:_qrImageView];
}
return _qrImageView;
}
掃描二維碼
- (void)captureSessionScanQRcodeImage {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if ([session canAddInput:input]) {
[session addInput:input];
}
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
if ([session canAddOutput:output]) {
[session addOutput:output];
}
// 可識別的類型
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
AVCaptureVideoPreviewLayer *preLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
preLayer.frame = CGRectMake(80, CGRectGetMaxY(self.qrImageView.frame)+20, kScreenW-160, kScreenW-160);
//填充方式這步很重要
preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:preLayer];
[session startRunning];
}
#pragma mark -- 掃碼成功的回調(diào)方法
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
NSString *content = @"";
AVMetadataMachineReadableCodeObject *metadata = metadataObjects.firstObject;
content = metadata.stringValue;
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:content]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:content]];
}
}
//========分割線 下面是掃碼成功的提示 可選擇添加=========
- (void)playBeep{
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"滴-2"ofType:@"mp3"]], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
??????如果覺得對您有幫助狡赐,麻煩請點個??哦