二維碼的生成
百科上說二維碼最多容納 1108個字節(jié) 近似1kb
二維碼的生成原理:
是用濾鏡生成的 給濾鏡設(shè)置值即可
CIFilter *qrcFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];//二維碼生成濾鏡名稱
[qrcFilter setValue:data forKey:@"inputMessage"];//設(shè)置data數(shù)據(jù)
[qrcFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
UIImage *image = [UIImage imageWithCIImage: qrcFilter.outputImage];
注釋:如何知道有哪些濾鏡,以及參數(shù)如何設(shè)置呢
[CIFilter filterNamesInCategory:nil];//使用此方法查看所有濾鏡名稱 返回值是一個數(shù)組
//使用此方法CIFilter *qrcFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];創(chuàng)建濾鏡之后,根據(jù)拿到的濾鏡查看相關(guān)屬性
qrcFilter.attributes;//數(shù)組 里面包含濾鏡的相關(guān)介紹和參數(shù)說明
qrcFilter.inputKeys;//數(shù)組 包含需要輸入的參數(shù)
最頂上的寫法已經(jīng)可以生成了一個二維碼,注意inputMessage的value需要二進(jìn)制數(shù)據(jù),但是生成二維碼還是比較小的.我們需要把它縮放到合適的大小.我們可以采用圖片重繪的方法.如下,注意interpolationQualityType插值類型需要寫無插值kCGInterpolationNone 要不然生成的二維碼是模糊的
-(UIImage *)ScaleToSize:(CGSize )size Withtype:(CGInterpolationQuality)interpolationQualityType{
//開啟畫布
UIGraphicsBeginImageContextWithOptions(size, NO, Scale);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), interpolationQualityType);
//重新繪制
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
//獲取圖片
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束繪制
UIGraphicsEndImageContext();
return result;
}
生成帶顏色的二維碼
這個仍然需要濾鏡,這個濾鏡的名字是偽顏色濾鏡.也就是說顏色是假的,不會影響二維碼存儲的真實信息.
多個濾鏡使用時,通常上一個濾鏡的結(jié)果是這個濾鏡的輸入.
CIFilter *falseColor = [CIFilter filterWithName:@"CIFalseColor"];
[falseColor setValue:qrcFilter.outputImage forKey:kCIInputImageKey];//上個濾鏡的輸出作為這個濾鏡的輸入
[falseColor setValue:[CIColor colorWithCGColor:foreColor.CGColor] forKey:@"inputColor0"];//二維碼顏色
[falseColor setValue:[CIColor colorWithCGColor:backColor.CGColor] forKey:@"inputColor1"];//底部背景色
image = [UIImage imageWithCIImage: falseColor.outputImage];
這樣得到的圖片就是帶顏色的圖片,其中的前景色和背景色類型需要CIColor
注意UIIimage的CIImage屬性可能為空
如果需要和上一步結(jié)合使用的話,拿到濾鏡的最終輸出結(jié)果在對圖片進(jìn)行縮放.
生成帶圖片的二維碼
生成帶圖片的二維碼,只需要在生成之后的二維碼上添加一張圖片就可以了.你可以選擇繪圖,也可以選擇直接在ImageView上添加一個ImageView.但是繪圖要更高效,這里采用繪圖.
UIImage *circleImage = [insertImage CircleImage];
//創(chuàng)建上下文
UIGraphicsBeginImageContext(image.size);
//繪制二維碼
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[circleImage drawInRect:CGRectMake((image.size.width - imageSize.width)/2, (image.size.height - imageSize.height)/2, imageSize.width, imageSize.height)];
//獲取圖像
image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束上下文
UIGraphicsEndImageContext();
注意:圖片的大小當(dāng)然是會影響二維碼信息的,因此不要太大哦
代碼整理
整理出了方法,你可以直接復(fù)制到你的項目中使用,
/**生成二維碼
百科上說二維碼最多容納 1108個字節(jié) 近似1kb
1.二維碼的內(nèi)容需要轉(zhuǎn)成為二進(jìn)制數(shù)據(jù)
2.插入圖片所占據(jù)的尺寸會影響二維碼的掃描識別
3.前景色和背景色要么都設(shè)置要么都不設(shè)置 并且前景色和背景色不能一樣或太接近
@param data 二維碼數(shù)據(jù)信息 字符串 網(wǎng)址 等需要轉(zhuǎn)成二進(jìn)制數(shù)據(jù)
@param size 二維碼的大小
@param insertImage 要插入的圖片 默認(rèn)會切圓角
@param imageSize 在二維碼中圖片顯示的尺寸,圖片過大會影響識別效果
@param foreColor 二維碼顏色
@param backColor 二維碼背景顏色
*/
+(UIImage *)createQRImageWithData:(nonnull NSData *)data QRCSize:(CGSize)size InsertImage:(UIImage *)insertImage imageSize:(CGSize)imageSize ForeColor:(UIColor *)foreColor andBackColor:(UIColor *)backColor{
//1.二維碼生成濾鏡
CIFilter *qrcFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrcFilter setValue:data forKey:@"inputMessage"];
[qrcFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
UIImage *image = [UIImage imageWithCIImage: qrcFilter.outputImage];
//2.顏色濾鏡
if (foreColor && backColor) {
CIFilter *falseColor = [CIFilter filterWithName:@"CIFalseColor"];
[falseColor setValue:qrcFilter.outputImage forKey:kCIInputImageKey];
[falseColor setValue:[CIColor colorWithCGColor:foreColor.CGColor] forKey:@"inputColor0"];//二維碼顏色
[falseColor setValue:[CIColor colorWithCGColor:backColor.CGColor] forKey:@"inputColor1"];//底部背景色
image = [UIImage imageWithCIImage: falseColor.outputImage];
}
//3.生成二維碼
image = [image ScaleToSize:size Withtype:kCGInterpolationNone];
//4.繪制一個頭像到二維碼上
if (insertImage) {
UIImage *circleImage = [insertImage CircleImage];
//創(chuàng)建上下文
UIGraphicsBeginImageContext(image.size);
//繪制二維碼
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//繪制頭像 假設(shè)頭像是100 100
[circleImage drawInRect:CGRectMake((image.size.width - imageSize.width)/2, (image.size.height - imageSize.height)/2, imageSize.width, imageSize.height)];
//獲取圖像
image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束上下文
UIGraphicsEndImageContext();
}
return image;
}
//這個方法,在上面有用到
-(UIImage *)ScaleToSize:(CGSize )size Withtype:(CGInterpolationQuality)interpolationQualityType{
//開啟畫布
UIGraphicsBeginImageContextWithOptions(size, NO, Scale);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), interpolationQualityType);
//重新繪制
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
//獲取圖片
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束繪制
UIGraphicsEndImageContext();
return result;
}
二維碼的識別
二維碼的識別需要用到二維碼檢測類
檢測圖片中的二維碼特征,檢測到返回即可,其中特征中會包含二維碼信息,這個屬性就是messageString.我封裝了一個方法,可以直接拿來用.注意這個是分類方法,self本身就是一個image
/**
檢測二維碼返回結(jié)果
沒有結(jié)果返回nil
@param detectImage 待檢測的圖片
@return 返回的二維碼信息
*/
-(NSString *)hasQRCmessage{
NSData *data = UIImageJPEGRepresentation(self, 1);
CIImage *resultImage = [CIImage imageWithData:data];
//檢測二維碼
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
NSArray *resultArr = [detector featuresInImage:resultImage];
if (resultArr.count > 0) {
CIQRCodeFeature *qrcFeature = resultArr.firstObject;
return qrcFeature.messageString;
}
return nil;
}