ZBarSDK无法读取非白色背景的QR码

时间:2013-07-05 13:56:39

标签: ios xcode qr-code zxing zbar-sdk

ZBarSDK适用于普通的QRCode图像,白色背景上的黑色代码,但在深色背景下没有检测到白色代码的QRCode图像?

2 个答案:

答案 0 :(得分:0)

这可以通过使用Zxing Lib以及GreyscaleLuminanceSource文件的getMatrix()方法中的以下更改来实现。

替换以下方法。

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

现在它只能在非白色背景上读取反转的QR码图像白色代码。

更多说明: Invert pixels - zxing

答案 1 :(得分:0)

您可以使用ZBarSDK读取常规和反向QR码,只需对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];
            }
        }

就是这样。到目前为止工作得很好。