无法改变zosObjc库为ios生成的条形码的颜色?

时间:2014-06-30 06:42:55

标签: ios barcode zxing color-space

我使用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;
}

我需要将默认黑色更改为自定义颜色。任何帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

不确定您是否还在寻找答案,但希望像我这样的其他人会看到这一点而不需要自己实施。它实际上看起来像较新版本的牛ZXingObjC实际上会为你处理这个问题,这个方法在+ (ZXImage *)imageWithMatrix:(ZXBitMatrix *)matrix onColor:(CGColorRef)onColor offColor:(CGColorRef)offColor;

<div class="ui-grid-b"> <div class="ui-block-a"> <div class="ui-field-contain"> <input type="text" id="pobox" name="pobox" autocomplete="false" placeholder="P.O. Box:" class="inputshort" required> </div> </div> <div class="ui-block-b"> <div class="ui-field-contain"> <input type="text" id="city" name="city" autocomplete="false" placeholder="City.." required> </div> </div>

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

答案 1 :(得分:1)

这适用于Redcolor

+ (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