反转像素 - zxing

发布于 2025-01-04 23:41:35 字数 1841 浏览 5 评论 0原文

我在我的 iOS 项目中使用 zxing 库。它是一个用于读取和创建 QR 码的库。

根据我在网上的研究和浏览,解码图像的过程由以下步骤组成:

  • 从源获取图像,
  • 将所有像素转换为 255 灰度
  • 对数据进行解码

该特定库不支持的一件事是读取/解码(而且我很确定在创建时也缺少这一点)倒置的 QR 码。

倒置QR码与普通码基本相同->但颜色相反(白色是黑色,黑色是白色)。但是因为QRCodes标准没有描述倒置QRCodes的实现,并且zxing项目网站上有一些请求和问题,所以我必须自己实现。

下面的方法是插入一些逻辑来反转像素的好地方(unsigned char*),但由于我没有 C++ 经验,所以结果是这样的。

grayData_ 是一种 unsigned char* 数据类型。在这个变量内部有来自源的灰度像素。

我想做的是反转这些像素。

如果我是正确的,这是由 unsigned char cz = (255 - val); 完成的?

unsigned char* GreyscaleLuminanceSource::getMatrix() {
    int size = width_ * height_;
    unsigned char* result = new unsigned char[size];
    if (left_ == 0 && top_ == 0 && dataWidth_ == width_ && dataHeight_ == height_) {
        memcpy(result, greyData_, size);
    } else {
        for (int row = 0; row < height_; row++) {
            memcpy(result + row * width_, greyData_ + (top_ + row) * dataWidth_ + left_, width_);
        }
    }

    //return result;
    //from this point down is my implementation

    printf(" %c", result[200]);
    printf(" %c", result[600]);
    printf(" %c", result[6000]);
    printf(" %c", result[7000]);

    for (int i = 0; i < size; i++)
    {
        int val = static_cast<int>(result[i]); 
        unsigned char cz = (255 -  val);
        result[i] = cz;
    }
    printf("******\n");
    printf(" %c", result[200]); //prints a " " char to console/terminal
    printf(" %c", result[600]); //prints a " " char to console/terminal
    printf(" %c", result[6000]); //prints a " " char to console/terminal
    printf(" %c", result[7000]); //prints a " " char to console/terminal

    return result;
}

这是反转像素的正确方法吗?我对更改 result 变量中的数据不太满意。

I'm using a zxing library in my iOS project. It's a library for reading and creating QR Codes.

From my researching and browsing around the web the process of decoding the image is made of this steps:

  • takes an image from source,
  • converts all the pixels to 255 grayscale
  • decodes the data

One thing which is not supported by this specific library is reading/decoding (and I'm pretty sure that this is missing in creating also) of inverted QRCodes.

Inverted QR Codes are basicly the same as normal codes -> but with inverted colors (white is black and black is white). But because the QRCodes standard doesn't describe the inverted QRCodes implementation and on zxing project website there is a few requests and issues, I must implement this by myself.

The method below is a good place to insert some logic to invert the pixels (unsigned char*), but because of my non-experience with C++ the result is this writing.

The grayData_ is a unsigned char* data type. And inside this variable there are grayScaled pixels from source.

What I want to do is invert these pixels.

If I'm correct this is done by unsigned char cz = (255 - val);?

unsigned char* GreyscaleLuminanceSource::getMatrix() {
    int size = width_ * height_;
    unsigned char* result = new unsigned char[size];
    if (left_ == 0 && top_ == 0 && dataWidth_ == width_ && dataHeight_ == height_) {
        memcpy(result, greyData_, size);
    } else {
        for (int row = 0; row < height_; row++) {
            memcpy(result + row * width_, greyData_ + (top_ + row) * dataWidth_ + left_, width_);
        }
    }

    //return result;
    //from this point down is my implementation

    printf(" %c", result[200]);
    printf(" %c", result[600]);
    printf(" %c", result[6000]);
    printf(" %c", result[7000]);

    for (int i = 0; i < size; i++)
    {
        int val = static_cast<int>(result[i]); 
        unsigned char cz = (255 -  val);
        result[i] = cz;
    }
    printf("******\n");
    printf(" %c", result[200]); //prints a " " char to console/terminal
    printf(" %c", result[600]); //prints a " " char to console/terminal
    printf(" %c", result[6000]); //prints a " " char to console/terminal
    printf(" %c", result[7000]); //prints a " " char to console/terminal

    return result;
}

Is this the right way to invert the pixels? And I'm not so happy with changing the data in the result variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2025-01-11 23:41:35

您不想将这些值打印为字符,因为它们不是 ASCII。将它们打印为无符号:

printf(" %u", ...);

您可以将循环简化为简单的

result[i] = static_cast<unsigned char>(255 - result[i]);

其他转换是正常的整数提升。

您应该注意到,ZXing 在识别代码时使用了一些非对称启发式方法。如果代码周围没有保护区域,在本例中为黑色(因为保护区域应该是白色),则可能无法识别代码。

You don't want to print the values as characters because they aren't ASCII. Print them as unsigneds:

printf(" %u", ...);

You can simplify the loop to simply

result[i] = static_cast<unsigned char>(255 - result[i]);

The other conversions are normal integral promotion.

And you should note that ZXing uses some asymmetric heuristics when identifying codes. If you don't have a guard region surrounding the code that is, in this case, black (since the guard region is supposed to be white), it may fail to identify the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文