生成二维码 导入CoreImage 库
#import <CoreImage/CoreImage.h> -(UIImage*)getImageFromStr:(NSString*)str{ //创建过滤器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; //恢复默认设置 [filter setDefaults]; //给字符串加密 str = [self encryptionStr:str]; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"]; //获取输出的二维码 CIImage *outputImage = [filter outputImage]; //对图片处理,使其高清 UIImage *image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200]; return image; } //使图片高清 -(UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size{ CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));; // 1.创建bitmap; 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); // 2.保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; }扫描二维码 导入AVFoundation 库 遵守
#define AUTH_ALERT_TAG (int)281821 #define ScreenHeight [UIScreen mainScreen].bounds.size.height #define ScreenWidth [UIScreen mainScreen].bounds.size.width @interface QRCodeController ()<AVCaptureMetadataOutputObjectsDelegate, UIAlertViewDelegate> { AVCaptureSession * session;//输入输出的中间桥梁 int line_tag; UIView *highlightView; NSDictionary * _resultDic; NSArray *_buddies; } @end @implementation QRCodeController /** * @author Whde * * viewDidLoad */ - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self instanceDevice]; [VCUtil addTitleView:self :@"添加好友"]; //[VCUtil addBackBtnInNavigation:self]; } - (void)instanceDevice{ line_tag = 1872637; //获取摄像设备 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //创建输入流 AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //创建输出流 AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; //设置代理 在主线程里刷新 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //初始化链接对象 session = [[AVCaptureSession alloc]init]; //高质量采集率 [session setSessionPreset:AVCaptureSessionPresetHigh]; if (input) { [session addInput:input]; } if (output) { [session addOutput:output]; //设置扫码支持的编码格式(如下设置条形码和二维码兼容) NSMutableArray *a = [[NSMutableArray alloc] init]; if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) { [a addObject:AVMetadataObjectTypeQRCode]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) { [a addObject:AVMetadataObjectTypeEAN13Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) { [a addObject:AVMetadataObjectTypeEAN8Code]; } if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) { [a addObject:AVMetadataObjectTypeCode128Code]; } output.metadataObjectTypes=a; } AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; layer.frame=self.view.layer.bounds; [self.view.layer insertSublayer:layer atIndex:0]; [self setOverlayPickerView]; [self addAnimation]; //开始捕获 [session startRunning]; } #pragma 扫码结果 delegate -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ DLog(@"%@",metadataObjects); if (metadataObjects.count>0) { [session stopRunning]; [self removeAnimation]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex :0]; NSString *data = metadataObject.stringValue; NSData *strData = [data dataUsingEncoding:NSUTF8StringEncoding]; NSError *error1 = NULL; id result = [NSJSONSerialization JSONObjectWithData:strData options: NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error1]; }创建扫码界面
#define MarginW (ScreenWidth-235)/2 #define MarginH (ScreenHeight-235)/2 - (void)setOverlayPickerView { //左侧的view UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MarginW, ScreenHeight)]; leftView.alpha = 0.5; leftView.backgroundColor = [UIColor blackColor]; [self.view addSubview:leftView]; //右侧的view UIImageView *rightView = [[UIImageView alloc] initWithFrame:CGRectMake(235+MarginW, 0, MarginW, ScreenHeight)]; rightView.alpha = 0.5; rightView.backgroundColor = [UIColor blackColor]; [self.view addSubview:rightView]; //最上部view UIImageView* upView = [[UIImageView alloc] initWithFrame:CGRectMake(MarginW, 0, ScreenWidth-2*MarginW, MarginH)]; upView.alpha = 0.5; upView.backgroundColor = [UIColor blackColor]; [self.view addSubview:upView]; //底部view UIImageView * downView = [[UIImageView alloc] initWithFrame:CGRectMake(MarginW, MarginH+235, (ScreenWidth-2*MarginW), MarginH)]; downView.alpha = 0.5; downView.backgroundColor = [UIColor blackColor]; [self.view addSubview:downView]; UIImageView *centerView = [[UIImageView alloc] initWithFrame:CGRectMake(MarginW, MarginH, 235, 235)]; centerView.image = [UIImage imageNamed:@"scankuang.png"]; centerView.contentMode = UIViewContentModeScaleAspectFill; centerView.backgroundColor = [UIColor clearColor]; [self.view addSubview:centerView]; UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(MarginW, MarginH, 235, 2)]; line.tag = line_tag; line.image = [UIImage imageNamed:@"scanline"]; line.contentMode = UIViewContentModeScaleAspectFill; line.backgroundColor = [UIColor clearColor]; [self.view addSubview:line]; UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(MarginW, MarginH-60, ScreenWidth-2*MarginW, 60)]; msg.backgroundColor = [UIColor clearColor]; msg.textColor = [UIColor whiteColor]; msg.textAlignment = NSTextAlignmentCenter; msg.font = [UIFont systemFontOfSize:16]; msg.text = @"将二维码放入框内,即可自动扫描"; [self.view addSubview:msg]; }