反转像素 - zxing [英] Invert pixels - zxing

查看:166
本文介绍了反转像素 - zxing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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




  • 从源头拍摄图像,

  • 将所有像素转换为255灰度

  • 解码数据



这个特定库不支持的一件事是读/解码(我很确定在创建反转QRCode时也缺少这个。



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



方法下面是插入一些逻辑来反转像素的好地方( unsigned char * ),但由于我没有使用C ++,结果就是写这篇文章。



grayData _ unsigned char * 数据类型。在这个变量中,有来自源的grayScaled像素。



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



如果我是正确的,这是由 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_,宽度_);
}
}

//返回结果;
//从这一点开始是我的实现

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);
结果[i] = cz;
}
printf(****** \ n);
printf(%c,result [200]); //将字符打印到console / terminal
printf(%c,result [600]); //将字符打印到console / terminal
printf(%c,result [6000]); //在控制台/终端打印字符
printf(%c,result [7000]); //在控制台/终端打印一个字符

返回结果;
}

这是反转像素的正确方法吗?我对改变结果变量中的数据并不是很满意。

解决方案

您不希望将值打印为字符,因为它们不是ASCII。将它们打印为未签名:

  printf(%u,...); 

您可以将循环简化为

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

其他转换是正常的整体促销。



<请注意,ZXing在识别代码时会使用一些非对称启发式算法。如果您没有围绕代码的保护区域,在这种情况下是黑色(因为保护区域应该是白色的),它可能无法识别代码。


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.

解决方案

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.

这篇关于反转像素 - zxing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆