- 自定义二维码,就是指给系统二维码添加一些图片(前景或者背景图片), 或者改变下颜色
- 自定义二维码实质是通过Quartz2D框架来绘制图片
生成自定义二维码的基础
1. 可以添加前景图片的前提是因为二维码具备一定的"纠错率",如果二维码被部分遮挡,可以根据其他部分,计算出遮挡部分内容;
2. 但是要保证三个角不能被遮挡;三个角用作扫描定位使用(可能用户倒着拍, 斜着拍等等)
生成自定义二维码 <- OC
-
导入CoreImage框架,一些图片处理操作的功能, 都是用这个框架实现, 比如: 滤镜效果, 毛玻璃, 美颜相机....
#import <CoreImage/CoreImage.h>
-
实现代码(有兴趣可研究)
// 根据CIImage, 和指定大小, 生成UIImage图片 + (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 创建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); // 保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; }
-
生成一个彩色图片(有兴趣可研究)
void ProviderReleaseData (void *info, const void *data, size_t size){ free((void*)data); } // 生成一个彩色图片 + (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{ const int imageWidth = image.size.width; const int imageHeight = image.size.height; size_t bytesPerRow = imageWidth * 4; uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage); // 遍历像素 int pixelNum = imageWidth * imageHeight; uint32_t* pCurPtr = rgbImageBuf; for (int i = 0; i < pixelNum; i++, pCurPtr++){ if ((*pCurPtr & 0xFFFFFF00) < 0x99999900) // 将白色变成透明 { // 改成下面的代码,会将图片转成想要的颜色 uint8_t* ptr = (uint8_t*)pCurPtr; ptr[3] = red; //0~255 ptr[2] = green; ptr[1] = blue; } else { uint8_t* ptr = (uint8_t*)pCurPtr; ptr[0] = 0; } } // 输出图片 CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData); CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider, NULL, true, kCGRenderingIntentDefault); CGDataProviderRelease(dataProvider); UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef]; // 清理空间 CGImageRelease(imageRef); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return resultUIImage; }
生成自定义二维码 <- swift
-
导入框架(可选)
import CoreImage
-
实现代码
// 在生成系统二维码的基础上,获取到滤镜outputImage后,对图片进行加工,最终返回加工后的图片 // 自定义二维码 let center = UIImage(named: "erha.png") let hechengImage = createImage(outputImage, centerImage: center) // 加工系统二维码,生成自定义二维码 func createImage(sourceImage: UIImage?, centerImage: UIImage?) -> UIImage? { // 0. 容错处理 if (sourceImage == nil && centerImage == nil) { print("sourceImage或者centerImage为空") return nil } // 1. 获取图片大小 let size = sourceImage!.size // 2. 开启上下文 UIGraphicsBeginImageContext(size) // 3. 绘制大图片 sourceImage!.drawInRect(CGRectMake(0, 0, size.width, size.height)) // 4. 绘制小图片 let w: CGFloat = 90 let h: CGFloat = 90 let x: CGFloat = (size.width - w) * 0.5 let y: CGFloat = (size.height - h) * 0.5 centerImage!.drawInRect(CGRectMake(x, y, w, h)) // 5. 获取合成的图片 let resultImage = UIGraphicsGetImageFromCurrentImageContext() // 6. 关闭上下文 UIGraphicsEndImageContext() // 7. 返回结果 return resultImage }
-
根据CIImage生成指定大小的高清UIImage(有兴趣可研究)
/** 根据CIImage生成指定大小的高清UIImage :param: image 指定CIImage :param: size 指定大小 :returns: 生成好的图片 */ private func createBigImage(image: CIImage, size: CGFloat) -> UIImage { let extent: CGRect = CGRectIntegral(image.extent) let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)) // 1.创建bitmap; let width = CGRectGetWidth(extent) * scale let height = CGRectGetHeight(extent) * scale let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()! let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)! let context = CIContext(options: nil) let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent) CGContextSetInterpolationQuality(bitmapRef, CGInterpolationQuality.None) CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 2.保存bitmap到图片 let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)! return UIImage(CGImage: scaledImage) }