代码之家  ›  专栏  ›  技术社区  ›  xtkwfn

stbi使用opencv Mat buffer从内存加载

  •  0
  • xtkwfn  · 技术社区  · 5 年前

    目前,我使用zxing(c++端口)扫描条形码,该端口使用stbi泳load从路径读取图像:

    filePath = "..\\data\\qr_t2.bmp";
    int width, height, channels;
    unsigned char *bytes;
    bytes = stbi_load(filePath.c_str(), &width, &height, &channels, 4);
    std::unique_ptr<stbi_uc, void(*)(void*)> buffer(bytes/*stbi_load(filePath.c_str(), &width, &height, &channels, 4)*/, stbi_image_free);
    

    但是我想用stbi_load_from_memory从内存读取图像,我的图像数据是使用opencv的Mat,我的代码如下:

    typedef unsigned char BYTE;
    
    Mat img_barcode = imread("..\\data\\qr_t2.bmp", 1);
    int width = img_barcode.cols;
    int height = img_barcode.rows;
    BYTE *pImg;
    int nFlag = img_barcode .channels() * 8;//bits of one pixel
    
    int nBytes = width * height * nFlag / 8;//totoal bits of image
    //if (pImg)
    //  delete[] pImg;
    pImg = new BYTE[nBytes];//unit of new is BYTE
    memcpy(pImg, srcImg.data, nBytes);//
    
    std::unique_ptr<stbi_uc, void(*)(void*)> buffer1(stbi_load_from_memory(pImg, 100*100*3, &width, &height, &channels, 3), stbi_image_free);
    

    buffer1总是空的!有人能帮忙吗?

    my test img Qr code

    0 回复  |  直到 5 年前