最近项目需求需要用到扫描遂简单的写了一下到时候好直接调用。废话不多说直接贴代码了。
二维码生成
/**
创建简易二维码
@param content 数据
@return image
*/
+ (UIImage*)createQRCodeImageWithContent:(NSString*)content
{
// 1. 创建一个二维码滤镜实例(CIFilter)
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filtersetDefaults];
// 2. 给滤镜添加数据
NSString*stringCurrent = string?string:@"are you ok?";
NSData *data = [stringCurrent dataUsingEncoding:NSUTF8StringEncoding];
[filtersetValue:dataforKeyPath:@"inputMessage"];
// 3. 生成二维码
CIImage*image = [filteroutputImage];
CGFloatsizeCurrent = size==0?100:size;
CGRectextent =CGRectIntegral(image.extent);
CGFloatscale =MIN(sizeCurrent/CGRectGetWidth(extent), sizeCurrent/CGRectGetHeight(extent));
// 1.创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRefbitmapRef =CGBitmapContextCreate(nil, width, height,8,0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext*context = [CIContextcontextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
CGImageRefscaledImage =CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return[UIImageimageWithCGImage:scaledImage];
}
/**
创建有颜色二维码
@param content 数据
@param shadowColor 颜色值
@return image
*/
+ (UIImage*)createQRCodeImageWithContent:(NSString*)content ShadowColor:(UIColor*)shadowColor
{
constCGFloat*components =CGColorGetComponents(shadowColor.CGColor);
CGFloatred = components[0];
CGFloatgreen = components[1];
CGFloatblue = components[2];
UIImage *image = [self createQRCodeImageWithString:content];
intimageWidth = image.size.width;
intimageHeight = image.size.height;
size_tbytesPerRow = imageWidth *4;
uint32_t*rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRefcontext =CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight,8, bytesPerRow, colorSpaceRef,kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context,CGRectMake(0,0, imageWidth, imageHeight), image.CGImage);
//遍历像素, 改变像素点颜色
intpixelNum = imageWidth * imageHeight;
uint32_t*pCurPtr = rgbImageBuf;
for(inti =0; i
if((*pCurPtr &0xFFFFFF00) <0x99999900) {
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[3] = red*255;
ptr[2] = green*255;
ptr[1] = blue*255;
}else{
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[0] =0;
}
}
//取出图片
CGDataProviderRefdataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight,ProviderReleaseData);
CGImageRefimageRef =CGImageCreate(imageWidth, imageHeight,8,32, bytesPerRow, colorSpaceRef,
kCGImageAlphaLast|kCGBitmapByteOrder32Little, dataProvider,
NULL,true,kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
UIImage*resultImage = [UIImageimageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpaceRef);
}
voidProviderReleaseData (void*info,constvoid*data,size_tsize){
free((void*)data);
}
/**
创建带图标二维码
@param content 数据
@param iconImage 小图标
@param size 二维码大小
@return image
*/
+ (UIImage*)createQRCodeImageWithContent:(NSString*)content iconImage:(NSString*)iconImage size:(CGFloat)size
{
UIImage *image = [self createQRCodeImageWithString:content];
// 5、开启绘图, 获取图形上下文 (上下文的大小, 就是二维码的大小)
UIGraphicsBeginImageContext(image.size);
// 把二维码图片画上去 (这里是以图形上下文, 左上角为(0,0)点
[imagedrawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
// 再把小图片画上去
NSString*icon_imageName = iconImage;
UIImage*icon_image = [UIImageimageNamed:icon_imageName];
CGFloaticon_imageW = size/4;
CGFloaticon_imageH = icon_imageW;
CGFloaticon_imageX = (image.size.width- icon_imageW) *0.5;
CGFloaticon_imageY = (image.size.height- icon_imageH) *0.5;
[icon_imagedrawInRect:CGRectMake(icon_imageX, icon_imageY, icon_imageW, icon_imageH)];
// 6、获取当前画得的这张图片
UIImage *final_image = UIGraphicsGetImageFromCurrentImageContext();
// 7、关闭图形上下文
UIGraphicsEndImageContext();
returnfinal_image;
}
二维码扫描
我也是看别人写的然后整理整理,代码太多贴出来会很乱,有需要可以看看demo。
链接: https://pan.baidu.com/s/12cxTORXYze373knHVqeDOA 密码: gzkq