2

I'm using zxingObjc library to generate barcode from string. Its working good but the problem I'm facing is I can't change the color of the generated barcode. The default color is black. Following is my code to generate barcode,

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;
}

I need to change the default black color to custom color. Any help would be highly appreciated.

Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20
Saleh Masum
  • 2,137
  • 1
  • 16
  • 16

2 Answers2

2

Not sure if you're still looking for an answer, but hopefully others like me will see this and not need to implement it themselves. It actually looks like newer versions ox ZXingObjC actually will handle this for you, with this method in ZXImage:

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

That seems to work for custom foreground and background colors for me.

bplattenburg
  • 623
  • 1
  • 8
  • 33
1

This is for 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;
    }

This is just changing the colour of pixel if its black. I think there a methods to change the pixel from outside using some image related methods or masks.

- (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;
}

This may help: Convert image to grayscale

Community
  • 1
  • 1
Suhail kalathil
  • 2,673
  • 1
  • 13
  • 12