【问题标题】:Can not change color of barcode generated from zxingObjc library for ios?无法更改 ios 的 zxingObjc 库生成的条码颜色?
【发布时间】:2014-08-20 11:30:41
【问题描述】:

我正在使用 zxingObjc 库从字符串生成条形码。它工作得很好,但我面临的问题是我无法更改生成的条形码的颜色。默认颜色为黑色。以下是我生成条形码的代码,

NSString *data = @"12345678901234567890";
if (data == 0) return;

ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];

ZXBitMatrix *result = [writer encode:data
                              format:kBarcodeFormatCode128
                               width:421
                              height:673
                               error:nil];


if (result) {
    ZXImage *image = [ZXImage imageWithMatrix:result];
    UIImage *barcodeImage = [UIImage imageWithCGImage:image.cgimage];
   // UIImage *colouredImage = [self changeColorForImage:barcodeImage toColor:[UIColor redColor]];
    self.barcodeImageView.image =barcodeImage;
} else {
    self.barcodeImageView.image = nil;
}

我需要将默认黑色更改为自定义颜色。任何帮助将不胜感激。

【问题讨论】:

    标签: ios barcode zxing color-space


    【解决方案1】:

    不确定您是否仍在寻找答案,但希望像我这样的其他人会看到这一点,而无需自己实施。它实际上看起来像新版本的 ox ZXingObjC 实际上会为你处理这个问题,这个方法在 ZXImage:

    + (ZXImage *)imageWithMatrix:(ZXBitMatrix *)matrix onColor:(CGColorRef)onColor offColor:(CGColorRef)offColor;

    这对我来说似乎适用于自定义前景色和背景色。

    【讨论】:

      【解决方案2】:

      这是红色的

      + (ZXImage *)imageWithMatrix:(ZXBitMatrix *)matrix {
            int width = matrix.width;
            int height = matrix.height;
            int8_t *bytes = (int8_t *)malloc(width * height * 4);
            for(int y = 0; y < height; y++) {
              for(int x = 0; x < width; x++) {
                BOOL bit = [matrix getX:x y:y];
                  int8_t intensity = bit ? 0 : 255;
                for(int i = 0; i < 3; i++) {
                  if(!i && !intensity)
                      bytes[y * width * 4 + x * 4 + i] = 255;
                  else
                    bytes[y * width * 4 + x * 4 + i] = intensity;
                }
                bytes[y * width * 4 + x * 4 + 3] = 255;
              }
            }
      
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGContextRef c = CGBitmapContextCreate(bytes, width, height, 8, 4 * width, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
            CFRelease(colorSpace);
            CGImageRef image = CGBitmapContextCreateImage(c);
            CFRelease(c);
            free(bytes);
      
            ZXImage *zxImage = [[ZXImage alloc] initWithCGImageRef:image];
      
            CFRelease(image);
            return zxImage;
          }
      

      这只是改变像素的颜色,如果它是黑色的。我认为有一种方法可以使用一些与图像相关的方法或掩码从外部更改像素。

      - (UIImage *)convertToGrayscale:(UIImage*)inputImage color:(UIColor*)color{
          CGSize size = [inputImage size];
          int width = size.width;
          int height = size.height;
      
          // the pixels will be painted to this array
          uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
      
          // clear the pixels so any transparency is preserved
          memset(pixels, 0, width * height * sizeof(uint32_t));
      
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      
          // create a context with RGBA pixels
          CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
                                                       kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
      
          // paint the bitmap to our context which will fill in the pixels array
          CGContextDrawImage(context, CGRectMake(0, 0, width, height), [inputImage CGImage]);
      
          for(int y = 0; y < height; y++) {
              for(int x = 0; x < width; x++) {
                  uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
      
                  // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
                 // uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
      
                  // set the pixels to gray
                  if( !rgbaPixel[RED] && !rgbaPixel[GREEN] && !rgbaPixel[BLUE])
                  {
                      const CGFloat *components = CGColorGetComponents(color.CGColor);
                      CGFloat red = components[0];
                      CGFloat green = components[1];
                      CGFloat blue = components[2];
                      CGFloat alpha = components[3];
                      rgbaPixel[RED] =  255 * red;
                      rgbaPixel[GREEN] =255 * green;
                      rgbaPixel[BLUE] = 255 * blue;
                  }
              }
          }
      
          // create a new CGImageRef from our context with the modified pixels
          CGImageRef image = CGBitmapContextCreateImage(context);
      
          // we're done with the context, color space, and pixels
          CGContextRelease(context);
          CGColorSpaceRelease(colorSpace);
          free(pixels);
      
          // make a new UIImage to return
          UIImage *resultUIImage = [UIImage imageWithCGImage:image];
      
          // we're done with image now too
          CGImageRelease(image);
      
          return resultUIImage;
      }
      

      这可能会有所帮助: Convert image to grayscale

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-06
        • 1970-01-01
        相关资源
        最近更新 更多