C++ libPNG&;QREnCode将qrcode保存到PNG

C++ libPNG&;QREnCode将qrcode保存到PNG,c++,image,qr-code,libpng,C++,Image,Qr Code,Libpng,嘿,伙计们,我在从qrcode数据输出png图像时遇到了麻烦 此时此刻,我有以下几点: 位图结构: struct Bitmap { std::vector<unsigned char> buf; // Pixel data if owned by Bitmap unsigned char* ptr; // Pixel data if owned by someone else unsigned width, height; double ar;

嘿,伙计们,我在从qrcode数据输出png图像时遇到了麻烦

此时此刻,我有以下几点:

位图结构:

struct Bitmap {
    std::vector<unsigned char> buf;  // Pixel data if owned by Bitmap
    unsigned char* ptr;  // Pixel data if owned by someone else
    unsigned width, height;
    double ar;  // Aspect ratio
    double timestamp;  // Used for video frames
    pix::Format fmt;
    bool linearPremul;  // Is the data linear RGB and premultiplied (as opposed to sRGB and non-premultiplied)
    bool bottomFirst;  // Upside-down (only used for taking screenshots)
    Bitmap(unsigned char* ptr = NULL): ptr(ptr), width(), height(), ar(), timestamp(), fmt(pix::CHAR_RGBA), linearPremul(), bottomFirst() {}
    void resize(unsigned w, unsigned h) {
        if (!ptr) buf.resize(w * h * 4); else buf.clear();
        width = w;
        height = h;
        ar = double(w) / h;
    }
    void swap(Bitmap& b) {
        if (ptr || b.ptr) throw std::logic_error("Cannot Bitmap::swap foreign pointers.");
        buf.swap(b.buf);
        std::swap(width, b.width);
        std::swap(height, b.height);
        std::swap(ar, b.ar);
        std::swap(timestamp, b.timestamp);
        std::swap(fmt, b.fmt);
    }
    unsigned char const* data() const { return ptr ? ptr : &buf[0]; }
    unsigned char* data() { return ptr ? ptr : &buf[0]; }

    void crop(const unsigned width, const unsigned height, const unsigned x, const unsigned y);
};
在我的QR码中,我将一些url转换成QR码。此QRCode调用了我的writePNG函数,但输出了一个有点奇怪的图像,这肯定不是QRCode:

这意味着PNG的标题正确,但数据不正确

我这样调用该方法:

QRCode::QRCode(const char* data, std::string path): m_qrCode(QRcode_encodeString(data, 4, QR_ECLEVEL_H, QR_MODE_8, 1)), m_path(path) 
{
    //QRCode::WriteBMP(path);

    Bitmap img;
    img.width = 256;
    img.height = 256;
    unsigned stride = (img.width * 3 + 3) & ~3;
    img.buf.resize(stride*img.height);
    img.fmt = pix::RGB;
    img.ptr = m_qrCode->data;

    writePNG(findFile("qrCode.png"), img);
}
我想我对qrdata做了一些奇怪的事情,它需要重新处理或其他什么

QRCode::QRCode(const char* data, std::string path): m_qrCode(QRcode_encodeString(data, 4, QR_ECLEVEL_H, QR_MODE_8, 1)), m_path(path) 
{
    //QRCode::WriteBMP(path);

    Bitmap img;
    img.width = 256;
    img.height = 256;
    unsigned stride = (img.width * 3 + 3) & ~3;
    img.buf.resize(stride*img.height);
    img.fmt = pix::RGB;
    img.ptr = m_qrCode->data;

    writePNG(findFile("qrCode.png"), img);
}