IOS 创建彩色二维码实例详解

时间:2022-06-25 09:57:43

IOS 创建彩色二维码

因为系统创建的二维码默认都是黑色的,所以突然想改变一下二维码颜色,具体操作有点复杂,而且其中用到了好多C语言的语法,Swift不好写,所以默认用了OC。只贴了.m文件的代码,.h文件就是几个类函数的声明。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#import "UIImage+CreateQRCode.h"
 
@implementation UIImage (CreateQRCode)
 
+ (UIImage *)createQRCode:(NSString *)string andSize:(CGSize)size andColor:(UIColor *)color {
  UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:string] withSize:size];
  const CGFloat *_components = CGColorGetComponents(color.CGColor);
  CGFloat red = _components[0] * 255.f;
  CGFloat green = _components[1] * 255.f;
  CGFloat blue = _components[2] * 255.f;
  return [self imageBlackToTransparent:qrcode withRed:red andGreen:green andBlue:blue];
}
 
+ (void)setImageViewShadow:(UIImageView *)view {
  view.layer.shadowOffset = CGSizeMake(0, 2);
  view.layer.shadowRadius = 2;
  view.layer.shadowColor = [UIColor blackColor].CGColor;
  view.layer.shadowOpacity = 0.5;
  view.backgroundColor = [UIColor clearColor];
}
 
#pragma mark - 创建灰度图,只有灰度图才能改变颜色
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGSize)size {
  CGRect extent = CGRectIntegral(image.extent);
  CGFloat scale = MIN(size.width/CGRectGetWidth(extent), size.height/CGRectGetHeight(extent));
   
  size_t width = CGRectGetWidth(extent) * scale;
  size_t height = CGRectGetHeight(extent) * scale;
   
//  iOS不支持设备依赖颜色空间或通用颜色空间。iOS应用程序必须使用设备颜色空间
//  设备颜色空间主要用于IOS应用程序,因为其它颜色空间无法在IOS上使用。大多数情况下,Mac OS X应用程序应使用通用颜色空间,而不使用设备颜色空间。
//  CGColorSpaceCreateDeviceGray:创建设备依赖灰度颜色空间
//  CGColorSpaceCreateDeviceRGB:创建设备依赖RGB颜色空间
//  CGColorSpaceCreateDeviceCMYK:创建设备依赖CMYK颜色空间
  CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();//这个是改变二维码颜色的主要属性,必须是灰度空间,作用是将UIImage转变成了灰度图
   
  CGContextRef bitmapRef = CGBitmapContextCreate(NULL, width, height, 8, 0, cs, kCGImageAlphaNone);
  CIContext * context = [CIContext contextWithOptions:NULL];
  CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
  CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationHigh);
  CGContextScaleCTM(bitmapRef, scale, scale);
  CGContextDrawImage(bitmapRef, extent, bitmapImage);
  CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
   
  CGContextRelease(bitmapRef);
  CGImageRelease(bitmapImage);
   
  return [UIImage imageWithCGImage:scaledImage];
}
 
#pragma mark - 创建二维码的主要代码
+ (CIImage *)createQRForString:(NSString *)qrString {
  NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
  CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  [qrFilter setValue:stringData forKey:@"inputMessage"];
  [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
  return qrFilter.outputImage;
}
 
#pragma mark - 改变二维码的颜色
void ProviderReleaseData (voidvoid *info, const voidvoid *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;
}
 
@end

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/youshaoduo/article/details/53286153