iOS - 原生的CIFilter生成二维码和条形码

Posted 公羽寒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - 原生的CIFilter生成二维码和条形码相关的知识,希望对你有一定的参考价值。

使用CIFilter可以不引入任何第三方库,就可以生成想要的二维码和条形码,今天简单的介绍一下使用CIFilter生成二维码和条形码。系统CIFilter生成的二维码和条形码的大小有时并不能满足需求,直接使用起来,二维码图片看起来有些模糊,这就需要我们手动的根据目标大小重新生成清晰的二维码和条形码。

1.生成二维码(iOS7.0以上支持)

/**
 inputCorrectionLevel
 等级  容错率
 L     7%
 M     15%  默认值
 Q     25%
 H     30%
 */



+ (UIImage *)generateQRCodeWithInputMessage:(NSString *)inputMessage
                                      Width:(CGFloat)width
                                     Height:(CGFloat)height{
    NSData *inputData = [inputMessage dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setValue:inputData forKey:@"inputMessage"];
    //    [filter setValue:@"H" forKey:@"inputCorrectionLevel"]; // 设置二维码不同级别的容错率

    CIImage *ciImage = filter.outputImage;
    // 消除模糊
    CGFloat scaleX = MIN(width, height)/ciImage.extent.size.width;
    CGFloat scaleY = MIN(width, height)/ciImage.extent.size.height;
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
    UIImage *returnImage = [UIImage imageWithCIImage:ciImage];
    return returnImage;
}

 

二维码中心添加图片

 + (UIImage *)generateQRCodeWithInputMessage:(NSString *)inputMessage
                                      Width:(CGFloat)width
                                     Height:(CGFloat)height
                             AndCenterImage:(UIImage *)centerImage{
    UIImage *backImage = [self generateQRCodeWithInputMessage:inputMessage Width:width Height:height];
    UIGraphicsBeginImageContext(backImage.size);
    [backImage drawInRect:CGRectMake(0, 0, backImage.size.width, backImage.size.height)];
    CGFloat centerImageWH = MIN(backImage.size.width, backImage.size.height) * 0.15;
    [centerImage drawInRect:CGRectMake((backImage.size.width - centerImageWH)*0.5, (backImage.size.height - centerImageWH)*0.5, centerImageWH, centerImageWH)];
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return returnImage;
}

 


**2. 生成条形码 (ios8.0以上 支持)**

#pragma mark - 获取条形码
-(UIImage*)createBarImageWithOrderStr:(NSString*)str{
    // 创建条形码
    CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
    // 恢复滤镜的默认属性
    [filter setDefaults];
    // 将字符串转换成NSData
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    // 通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKey:@"inputMessage"];
    // 获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];
    // 将CIImage转换成UIImage,并放大显示
    UIImage* image =  [UIImage imageWithCIImage:outputImage scale:0 orientation:UIImageOrientationUp];
    return image;
}

 



+ (UIImage *)generateBarcodeWithInputMessage:(NSString *)inputMessage
                                       Width:(CGFloat)width
                                      Height:(CGFloat)height{
    NSData *inputData = [inputMessage dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
    CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
    [filter setValue:inputData forKey:@"inputMessage"]; // 设置条形码内容
    //    [filter setValue:@(50) forKey:@"inputQuietSpace"]; // 设置条形码上下左右margin值
    //    [filter setValue:@(height) forKey:@"inputBarcodeHeight"]; // 设置条形码高度
    CIImage *ciImage = filter.outputImage;
    CGFloat scaleX = width/ciImage.extent.size.width;
    CGFloat scaleY = height/ciImage.extent.size.height;
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
    UIImage *returnImage = [UIImage imageWithCIImage:ciImage];
    return returnImage;
}

 

/**

生成条形码
*/
(IBAction)generateBarCode:(id)sender {
  CIImage ciImage = [self generateBarCodeImage:@"1234567890"];
  UIImage image = [self resizeCodeImage:ciImage withSize:CGSizeMake((self.view.frame.size.width - 80), 80)];
  _barCodeImageView.image = image;
}
/
生成条形码
@param source
@return 生成条形码的CIImage对象
*/
(CIImage *) generateBarCodeImage:(NSString *)source
{
  // iOS 8.0以上的系统才支持条形码的生成,iOS8.0以下使用第三方控件生成
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    // 注意生成条形码的编码方式
    NSData *data = [source dataUsingEncoding: NSASCIIStringEncoding];
    CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
    [filter setValue:data forKey:@"inputMessage"];
    // 设置生成的条形码的上,下,左,右的margins的值
    [filter setValue:[NSNumber numberWithInteger:0] forKey:@"inputQuietSpace"];
    return filter.outputImage;
  }else{
    return nil;
  }
}

 

**3. 生成自定义的二维码和条形码的大小**
/**

调整生成的图片的大小
@param image CIImage对象
@param size 需要的UIImage的大小
@return size大小的UIImage对象
*/
(UIImage *) resizeCodeImage:(CIImage *)image withSize:(CGSize)size
{
  if (image) {
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
    CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
    size_t width = CGRectGetWidth(extent) * scaleWidth;
    size_t height = CGRectGetHeight(extent) * scaleHeight;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
    CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef imageRef = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
    CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
    CGContextDrawImage(contentRef, extent, imageRef);
    CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
    CGContextRelease(contentRef);
    CGImageRelease(imageRef);
    return [UIImage imageWithCGImage:imageRefResized];
  }else{
    return nil;
  }
}

 


以上就是使用iOS系统原生的CIFilter生成二维码和条形码,对于生成二维码和条形码时,CIFilter的参数的定义可以参考官方文档。

官网:CIFilter







以上是关于iOS - 原生的CIFilter生成二维码和条形码的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发-二维码

iOS开发 - 二维码的生成与读取

通过CoreImage生成二维码

二维码的生成

iOS二维码条形码生成(可指定大小颜色)

二维码