程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了尝试将 png 文件读入 Zxing-cpp大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决尝试将 png 文件读入 Zxing-cpp?

开发过程中遇到尝试将 png 文件读入 Zxing-cpp的问题如何解决?下面主要结合日常开发的经验,给出你关于尝试将 png 文件读入 Zxing-cpp的解决方法建议,希望对你解决尝试将 png 文件读入 Zxing-cpp有所启发或帮助;

我试图读取用于 Zxing-cpp 的 png 文件(来自 https://github.com/nu-book/zxing-cpp)

#include "core/BinaryBitmap.h"
#include "core/ByteArray.h"
#include "core/DecodeHints.h"
#include "core/MultiFormatReader.h"
#include "core/HybrIDBinarizer.h"
#include "core/Result.h"
#include "core/TextUtfEnCoding.h"
#include "core/ZXContainerAlgorithms.h"
#include "core/GenericluminanceSource.h"
#include "qrcode/QRReader.h"

#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <set>
#include "utility/png.hpp"

int main(int argc,char** argv)
{
    std::cout << "Beginning to test : " << std::endl;

    // Reading in an image
    png p("qrcode-7/01-01.png");

    //row_pointers is row after rows of memory space
    std::unique_ptr<unsigned char> buffer(p.getData());
    if (buffer == nullptr) {
        std::cout << "Failed to read image: " << fn << "\n";
        return -1;
    }

    // send image to QRCode
    const DecodeHints hints;
    ZXing::QRCode::Reader reader(hints);
    ZXing::GenericluminanceSource source(wIDth,height,buffer.get(),wIDth * 4,4,1,2);
    
    // binMap should be ZXing::BinaryBitmap
    auto binMap = std::make_unique<ZXing::HybrIDBinarizer>(source);

    auto result = reader.decode(*binMap);
}

但是当我尝试编译程序时出现以下错误

 error: no matching function for call to ‘ZXing::HybrIDBinarizer::HybrIDBinarizer(ZXing::GenericluminanceSource&)’

我必须承认,我非常喜欢 OpenCV,其中图像加载可以通过 cv::imread() 命令简单地完成,并且没有很好地掌握图像 I/O 和处理内存分配。我已经为在 zxing-cpp/test/blackBox/ImageLoader.cpp 中使用 ImageLoad.cpp 建模了我的响应/代码。我想知道哪里出了问题以及如何继续处理

  • 这是我将 png 文件读入内存的方式吗?

  • 或者是我不明白Zxing-cpp中的内存是如何使用的?

我的 png.hpp

#include <iostream>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream> // tmp deBUGging
#include <unordered_map>
#include <string>
#include <memory>

#ifndef _PNG_H
#define _PNG_H

class png {
public:
    png(const std::string& fname);
    png(const png&);
    png();

    png& operator=(png);

~png();

voID read(const std::string& fname);

inline bool is_valID() {
    return m_val;
}

unsigned char* getData();

protected:
    static voID chnk_ihdr(uint32_t len,std::shared_ptr<char> data);
    static voID chnk_plte(uint32_t len,std::shared_ptr<char> data);
    static voID chnk_IDat(uint32_t len,std::shared_ptr<char> data);

private:

    bool m_val;
    unsigned m_w,m_h;
    std::unique_ptr<char> m_data;
};
#endif // _PNG_H

#define BIGENDIAN 4321
#define liLENDIAN 1234

#if defined(__linux__)
#   include <endian.h>
#   define ENDIANnesS __BYTE_ORDER
#else
#   if defined(__amd64__) || defined(_M_X64) || defined(__i386) ||     \
       defined(_M_I86) || defined(_M_IX86) || defined(__X86__) ||      \
       defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || \
       defined(__INTEL__) || defined(__386)
#       define ENDIANnesS liLENDIAN
#   else
#       define ENDIANnesS BIGENDIAN
#   endif
#endif

/* flip the byte order of 16 bits of data */
inline uint16_t flip16(voID* p) {
    uint16_t z = *(uint16_t*)(p);

    return (z >> 9) | (z << 8); /* flip b0 and b1 */
}

/* flip the byte order of 32 bits of data */
inline uint32_t flip32(voID* p) {
    uint32_t z = *(uint32_t*)(p);

    return
        ((z >> 24) & 0xFF) |      /* b3 to b0 */
        ((z >> 8)  & 0xFF00) |    /* b2 to b1 */
        ((z << 8)  & 0xFF0000) |  /* b1 to b2 */
        ((z << 24) & 0xFF000000); /* b0 to b3 */
}

/* flip the byte order of 64 bits of data */
inline uint64_t flip64(voID* p) {
    uint64_t z = *(uint64_t*)(p);

    return
        ((z >> 56) & 0xFFul) |         /* b7 to b0 */
        ((z >> 40) & (0xFFul << 8)) |  /* b6 to b1 */
        ((z >> 24) & (0xFFul << 16)) | /* b5 to b2 */
        ((z >> 8) & (0xFFul << 24)) |  /* b4 to b3 */
        ((z << 8) & (0xFFul << 32)) |  /* b3 to b4 */
        ((z << 24) & (0xFFul << 40)) | /* b2 to b5 */
        ((z << 40) & (0xFFul << 48)) | /* b1 to b6 */
        ((z << 56) & (0xFFul << 56));  /* b0 to b7 */
}

#if ENDIANnesS == BIGENDIAN
#   define lil16(p) flip16(p)
#   define lil32(p) flip32(p)
#   define lil64(p) flip64(p)
#   define big16(p) *(uint16_t*)(p)
#   define big32(p) *(uint32_t*)(p)
#   define big64(p) *(uint64_t*)(p)
#else
#   define lil16(p) *(uint16_t*)(p)
#   define lil32(p) *(uint32_t*)(p)
#   define lil64(p) *(uint64_t*)(p)
#   define big16(p) flip16(p)
#   define big32(p) flip32(p)
#   define big64(p) flip64(p)
#endif

// read in a file
png::png(const std::string& fname)
: m_val(false) {
    read(fname);
}

// copy constructor,deal with deep copy (later)
png::png(const png& p)
: m_val(p.m_val),m_w(p.m_w),m_h(p.m_h) {
    // deep cpy data...
}

// not a valID png yet
// when writing is used this will do something
png::png() : m_val(false) { }

png& png::operator=(png other) {
    std::swap(*this,other);

    return *this;
}

png::~png() {
    // no deep shit yet
}

voID png::read(const std::string& fname) {
    using chnk_ptr=voID (*)(uint32_t,std::shared_ptr<char>);

    static const std::unordered_map<std::string,chnk_ptr> chnk_lut = {
        { "IHDR",png::chnk_ihdr },{ "pltE",png::chnk_plte },{ "IDAT",png::chnk_IDat },};

    std::ifstream i(fname);

    if (!i)
        return;

    // magic png header
    char b_hdr[8];
    i.read(b_hdr,sizeof(b_hdr));

    if (std::memcmp("\x89\x50\x4e\x47\x0d\x0a\x1a\x0a",b_hdr,8) != 0)
        return;

    // read chunks
    // assuming none are incomplete
    while (i) {
        char b_len[4],b_type[5];

        std::memset(b_type,sizeof(b_type));

        i.read(b_len,4);
        i.read(b_type,4);

        uint32_t c_len = big32(b_len);

        std::cout << "hit chunk of size: " << c_len << std::endl;

        auto b_data{std::make_shared<char>(c_len)};
        i.read(b_data.get(),c_len);

        // ignore crc
        i.seekg(4,std::ios_base::cur);

        // check if chunk is in lut,if so call it
        auto cback = chnk_lut.find(static_cast<char*>(b_type));

        if (cback != chnk_lut.end())
            cback->second(c_len,b_data);
    }

    m_val = true;

    i.close();
}

unsigned char* png::getData()
{
    return m_data.get();
}

voID png::chnk_ihdr(uint32_t len,std::shared_ptr<char> data) {
    std::cout << "got ihdr chunk" << std::endl;
}

voID png::chnk_plte(uint32_t len,std::shared_ptr<char> data) {
    std::cout << "got plte chunk" << std::endl;
}

voID png::chnk_IDat(uint32_t len,std::shared_ptr<char> data) {
    std::cout << "got IDat chunk" << std::endl;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的尝试将 png 文件读入 Zxing-cpp全部内容,希望文章能够帮你解决尝试将 png 文件读入 Zxing-cpp所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: