iOS开发 二维码扫描和生成

xiaoxiao2021-02-28  15

一.二维码扫描

1.第一种方法:需要引入ZXingObjC(系统版本是iOS 8以下的)

/** * 从图片中读取二维码 这是iOS7以后的方法 * * @param qrImage 一张二维码图片 * * @return 二维码信息 */ + (NSString *)scQRReaderForImage:(UIImage *)qrImage { if (qrImage == nil) { return nil; } ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:qrImage.CGImage]; ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]]; NSError *error = nil; ZXDecodeHints *hints = [ZXDecodeHints hints]; ZXMultiFormatReader *reader = [ZXMultiFormatReader reader]; ZXResult *result = [reader decode:bitmap hints:hints error:&error]; // The coded result as a string. The raw data can be accessed with // result.rawBytes and result.length. return result.text; }

2.第二种方法:(系统版本是iOS8以上的)

/** * 从图片中读取二维码 这是iOS8以后的方法 * * @param qrImage 一张二维码图片 * * @return 二维码信息 */ + (NSString *)scQRReaderiOS8ForImage:(UIImage *)qrImage NS_AVAILABLE_IOS(8_0) { if (qrImage == nil) { return nil; } UIImage *srcImage = qrImage; CIContext *context = [CIContext contextWithOptions:nil]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; CIImage *image = [CIImage imageWithCGImage:srcImage.CGImage]; NSArray *features = [detector featuresInImage:image]; CIQRCodeFeature *feature = [features firstObject]; NSString *result = feature.messageString; return result; }

二.二维码生成

1.第一种方法:需要引入ZXingObjC

/** * 根据给定的字符串生成一个给定尺寸的二维码image * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * * @return 二维码 */ + (UIImage *)zxQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize { if (qrString.length == 0) { return nil; } if (imageSize < 10.0) { return nil; } NSError *error = nil; ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer]; ZXBitMatrix *result = [writer encode:qrString format:kBarcodeFormatQRCode width:imageSize height:imageSize error:&error]; if (result) { CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage]; UIImage *qrImage = [UIImage imageWithCGImage:image]; return qrImage; } return nil; }

2.第二种方法:需要调第五种方法

/** * 根据给定的字符串生成一个给定尺寸的二维码image * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * * @return 二维码 */ + (UIImage *)scQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize { return [self scQRCodeForString:qrString size:imageSize fillColor:[UIColor blackColor] backColor:[UIColor whiteColor]]; }

3.第三种方法:需要调第五种方法

/** * 根据给定的字符串生成一个给定尺寸和填充颜色的二维码image * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * @param fillColor 二维码填充颜色 * * @return 二维码 */ + (UIImage *)scQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor { return [self scQRCodeForString:qrString size:imageSize fillColor:fillColor backColor:[UIColor whiteColor]]; }

4.第四种方法:需要调第五种方法

/** * 根据给定的字符串生成一个给定尺寸和背景颜色的二维码image * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * @param fillColor 二维码背景颜色 * * @return 二维码 */ + (UIImage *)scQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize backColor:(UIColor *)backColor { return [self scQRCodeForString:qrString size:imageSize fillColor:[UIColor blackColor] backColor:backColor]; }

5.第五种方法:

/** * 根据给定的字符串生成一个给定尺寸和给定颜色的二维码image * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * @param fillColor 二维码填充颜色 * @param backColor 二维码背景颜色 * * @return 二维码 */ + (UIImage *)scQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor backColor:(UIColor *)backColor { if (qrString.length == 0) { return nil; } if (imageSize < 10.0) { return nil; } if (!fillColor) { fillColor = [UIColor blackColor]; } if (!backColor) { backColor = [UIColor whiteColor]; } NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding]; //生成 CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [qrFilter setValue:stringData forKey:@"inputMessage"]; [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; //上色 CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor" keysAndValues: @"inputImage",qrFilter.outputImage, @"inputColor0",[CIColor colorWithCGColor:fillColor.CGColor], @"inputColor1",[CIColor colorWithCGColor:backColor.CGColor], nil]; CIImage *qrImage = colorFilter.outputImage; //绘制 CGSize imgSize = CGSizeMake(imageSize, imageSize); CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent]; UIGraphicsBeginImageContext(imgSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetInterpolationQuality(context, kCGInterpolationNone); CGContextScaleCTM(context, 1.0, -1.0); CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage); UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRelease(cgImage); return codeImage; }

6.第六种方法:生成中间有logo的二维码

/** * 生成中间有logo的二维码 * * @param qrString 二维码的内容 * @param imageSize 二维码生成后的尺寸大小 * @param fillColor 二维码填充颜色 * @param backColor 二维码背景颜色 * @param subImage 二维码的子图 * * @return 带有子图的二维码 */ + (UIImage *)scQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor backColor:(UIColor *)backColor subImage:(UIImage *)subImage { UIImage *qrImage = [QRUtil scQRCodeForString:qrString size:imageSize fillColor:fillColor backColor:backColor]; if (subImage) { return [self addSubImage:qrImage sub:subImage]; } return qrImage; } #pragma mark - private + (UIImage *)addSubImage:(UIImage *)img sub:(UIImage *)subImage { //get image width and height NSInteger w = img.size.width; NSInteger h = img.size.height; NSInteger subWidth = subImage.size.width; NSInteger subHeight = subImage.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //create a graphic context with CGBitmapContextCreate CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); CGContextDrawImage(context, CGRectMake( (w-subWidth)/2, (h - subHeight)/2, subWidth, subHeight), [subImage CGImage]); CGImageRef imageMasked = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return [UIImage imageWithCGImage:imageMasked]; }

转载请注明原文地址: https://www.6miu.com/read-1100149.html

最新回复(0)