1

ZBarSDK is working fine for normal QRCode images, code with black color on white background, but it is not detecting QRCode images with code in white on dark color background?

user
  • 221
  • 1
  • 2
  • 11
  • Which version of ZBar are you using? – Danilo Jul 05 '13 at 13:57
  • https://github.com/markdarling/ZBar-SDK-iOS/tree/master/ZBarSDK this one – user Jul 05 '13 at 14:04
  • ZBar doesnt scan white on black at the moment. If the qrcodes you want to scan are always white on black, you could try to reverse the live image before ZBar processes it. – Danilo Jul 05 '13 at 14:09
  • can you please explain it, how it can be done? or can suggest any other library which can help me out.. – user Jul 05 '13 at 14:12
  • Maybe this will help on finding another library: http://qrarts.com/2010/06/readers-that-like-inverted-qr-codes/ – Danilo Jul 05 '13 at 14:18
  • This is not helping me out, can you suggest any other lib as i cant change the color of the image. – user Jul 05 '13 at 15:21
  • No, because technically that wouldn't be a QRcode anymore. ISO/IEC 18004:2006 says that a dark module is a binary one and a light module is a binary zero. – Danilo Jul 05 '13 at 15:40

2 Answers2

0

This can be achieved by using Zxing Lib with below changes in getMatrix() method of GreyscaleLuminanceSource file.

Replace the below method.

ArrayRef<char> GreyscaleLuminanceSource::getMatrix() const {
  int size = getWidth() * getHeight();
  ArrayRef<char> result (size);
  if (left_ == 0 && top_ == 0 && dataWidth_ == getWidth() && dataHeight_ == getHeight()) {
    memcpy(&result[0], &greyData_[0], size);
  } 
else {
    for (int row = 0; row < getHeight(); row++) {
      memcpy(&result[row * getWidth()], &greyData_[(top_ + row) * dataWidth_ + left_], getWidth());
    }
  }
    for (int i = 0; i < size; i++)
    {
        int val = static_cast<int>(result[i]);
        unsigned char cz = (255 -  val);
        result[i] = cz;
    }
  return result;
}

Now it can only read inverted QR Code images white code on non white background.

For more clarification: Invert pixels - zxing

Community
  • 1
  • 1
user
  • 221
  • 1
  • 2
  • 11
0

You can read both regular and inverted QR codes with ZBarSDK with just a few changes to ZBarCaptureReader.m:

unsigned int frameCounter = 0; // ADD THIS (only invert every few frames)

@implementation ZBarCaptureReader

// ... in this function ...
- (void)  captureOutput: (AVCaptureOutput*) output
  didOutputSampleBuffer: (CMSampleBufferRef) samp
         fromConnection: (AVCaptureConnection*) conn {

    // ...around line 300...
    //void *data = CVPixelBufferGetBaseAddressOfPlane(buf, 0);
    unsigned char *data = CVPixelBufferGetBaseAddressOfPlane(buf, 0);

    if (data) {

        // AND ADD THIS
        if (frameCounter++ % 3 == 0) {
            unsigned long size = w * h;
            for (unsigned long i = 0; i < size; i++) {
                data[i] = ~data[i];
            }
        }

That's it. Works great so far.

Steve Spencer
  • 121
  • 1
  • 3