首页 > 其他分享 >pdf417lib库封装和使用

pdf417lib库封装和使用

时间:2024-03-23 23:44:37浏览次数:26  
标签:FIXED 封装 NAME int pdf417 PDF417 pdf417lib 使用 property

pdf417lib库封装和使用

pdf417lib下载链接

https://master.dl.sourceforge.net/project/pdf417lib/pdf417lib/0.91/pdf417lib-c-0.91.zip?viasf=1

CMake文件

yh@ubuntu:/Test/pdf417lib$ tree
.
├── build
├── CMakeLists.txt
├── include
│   ├── pdf417.h
│   ├── pdf417lib.h
│   └── pdf417libimp.h
├── main.cpp
└── src
    ├── pdf417.cpp
    └── pdf417lib.c

3 directories, 7 files

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.0.0)
project(barcode)
include_directories(${PROJECT_SOURCE_DIR}/include)
file(GLOB LIBRARY_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/pdf417lib.c")

#设置静态库和动态库所需的中间文件列表
set(LIBRARY_OBJS_LIST ${PROJECT_NAME}-objs)
add_library(${LIBRARY_OBJS_LIST} OBJECT ${LIBRARY_SRC_LIST})
#开启静态库和动态库所需的中间文件的PIC编译选项
set_target_properties(${LIBRARY_OBJS_LIST} PROPERTIES POSITION_INDEPENDENT_CODE True)

#生成静态库
#设置静态库文件临时名称
set(TMP_STATIC_LIB_NAME ${PROJECT_NAME}-static)
#生成静态库文件
add_library(${TMP_STATIC_LIB_NAME} STATIC $<TARGET_OBJECTS:${LIBRARY_OBJS_LIST}>)
#设置静态库文件的最终名称
set_target_properties(${TMP_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})

#生成动态库
#设置动态库文件临时名称
set(TMP_SHARED_LIB_NAME ${PROJECT_NAME}-shared)
#生成动态库文件
add_library(${TMP_SHARED_LIB_NAME} SHARED $<TARGET_OBJECTS:${LIBRARY_OBJS_LIST}>)
#设置动态库文件的最终名称
set_target_properties(${TMP_SHARED_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})

add_executable(test${PROJECT_NAME} main.cpp)
target_link_libraries(test${PROJECT_NAME} ${TMP_STATIC_LIB_NAME})

pdf417lib.h

#ifndef __PDF417LIB_H__
#define __PDF417LIB_H__

#ifdef __cplusplus
extern "C" {
#endif
//四者取其一,优先判断PDF417_FIXED_RECTANGLE,若乘积大于(MAX_DATA_CODEWORDS + 2)
//则取最大值(MAX_DATA_CODEWORDS + 2),并且重设信息块数的数量和行高。
//接着判断PDF417_FIXED_COLUMNS和PDF417_FIXED_ROWS若未设置,则采用PDF417_USE_ASPECT_RATIO
//建议不要使用PDF417_USE_ASPECT_RATIO受aspectRatio、yHeight和strlen(text)影响很不稳定
#define PDF417_USE_ASPECT_RATIO     0      //此项为默认值,默认比例为0.5,通过aspectRatio来修改
#define PDF417_FIXED_RECTANGLE      1      //同时指定pdf417条码的宽度(数据区信息块的数量)和高度(最小方块的数量)
#define PDF417_FIXED_COLUMNS        2      //指定pdf417条码的宽度(数据区信息块的数量)
#define PDF417_FIXED_ROWS           4      //指定pdf417条码的高度(最小方块的数量)

//二者取其一
#define PDF417_AUTO_ERROR_LEVEL     0      //此项为默认值,根据内容的字节数自动设置纠错等级
#define PDF417_USE_ERROR_LEVEL      16     //指定pdf417条码的纠错等级(0到8)

#define PDF417_USE_RAW_CODEWORDS    64     //将text作为二进制流而非字符流,此时需要设置lenText的大小
#define PDF417_INVERT_BITMAP        128    //反转pdf417条码的黑白块,默认1表示黑块

//pdf417param中error的值, 表示paintCode()的执行结果
#define PDF417_ERROR_SUCCESS        0      //没有错误
#define PDF417_ERROR_TEXT_TOO_BIG   1      //所需编码的信息过大
#define PDF417_ERROR_INVALID_PARAMS 2      //错在错误的条码参数

typedef struct _pdf417param {
    char *outBits;
    int lenBits;
    int bitColumns;
    int codeRows;        //最大值90,若与codeColumns大于928,则两个值会被重设
    int codeColumns;     //最大值30,若与codeRows大于928,则两个值会被重设
    int codewords[928];
    int lenCodewords;
    int errorLevel;
    char *text;
    int lenText;
    int options;
    float aspectRatio;
    float yHeight;
    int error;
} pdf417param, *pPdf417param;

void paintCode(pPdf417param p);
void pdf417init(pPdf417param param);
void pdf417free(pPdf417param param);
#ifdef __cplusplus
}
#endif

#endif

pdf417.h

#ifndef __PDF417__
#define __PDF417__

#include <string>
#include <vector>

class pdf417
{
    public:
    enum class FitPolicy {UseAspectRatio = 0, FixedRectangle = 1, FixedColumns = 2, FixedRows = 4};
public:
    pdf417() = default;
    ~pdf417() = default;
public:
    void setBarCodeProperty(int columnfit = 6, int pixelBitDepth = 1, bool ifInvertPixel = false, int errorLevel = -1, FitPolicy shapefit = FitPolicy::FixedColumns, float ratio = 0.5, float yHeight = 3.0, int rowfit = 0);
    bool printBarCode(const std::string &data);
    bool output2postscript(const std::string &path);
public:
    int m_Width = 0;
    int m_Height = 0;
    int m_BytesPerRow = 0;
    int m_PixelBitsDepth = 1;
    int m_ColumnFit = 0;
    int m_RowFit = 0;
    int m_ShapeFit = 0;
    int m_ErrorLevel = 0;
    float m_YHeight = 3.0;
    float m_AspectRatio = 0.5;
    bool m_IfInvertPixel = false;
    std::vector<unsigned char> m_CacheMap;
    std::string m_ErrorMSG;
};

#endif

pdf417.cpp

#include <pdf417.h>
#include <pdf417lib.h>
#include <iostream>

using namespace std;

void pdf417::setBarCodeProperty(int columnfit, int pixelBitDepth, bool ifInvertPixel, int errorLevel, FitPolicy shapefit, float ratio, float yHeight, int rowfit)
{
    m_AspectRatio = 0.5;
    m_RowFit = 0;
    m_ColumnFit = 0;

    switch(shapefit)
    {
        case FitPolicy::FixedRectangle:
        {
            m_RowFit = rowfit;
            m_ColumnFit = columnfit;
            m_ShapeFit= PDF417_FIXED_RECTANGLE;
            break;
        }
        case FitPolicy::FixedColumns:
        {
            m_ShapeFit= PDF417_FIXED_COLUMNS;
            m_ColumnFit = columnfit;
            break;
        }
        case FitPolicy::FixedRows:
        {
            m_ShapeFit= PDF417_FIXED_ROWS;
            m_RowFit = rowfit;
            break;
        }
        default:
        {
            m_ShapeFit= PDF417_USE_ASPECT_RATIO;
            m_AspectRatio = ratio;
            break;
        }  
    }
    m_PixelBitsDepth = pixelBitDepth;
    m_IfInvertPixel = ifInvertPixel;
    m_ErrorLevel = errorLevel;
    m_YHeight = yHeight;
}

bool pdf417::printBarCode(const std::string &data)
{
    pdf417param property;
    pdf417init(&property);
    property.text = const_cast<char*>(data.c_str());
    property.errorLevel = m_ErrorLevel;
    if(m_IfInvertPixel)
    {
        property.options |= PDF417_INVERT_BITMAP;
    }
    property.options |= m_ShapeFit;
    property.codeRows = m_RowFit;
    property.codeColumns = m_ColumnFit;
    property.aspectRatio =  m_AspectRatio;
    property.yHeight = m_YHeight;
    paintCode(&property);
    switch(property.error)
    {
        case PDF417_ERROR_SUCCESS:
        {
            m_Width = property.bitColumns;
            m_Height = property.codeRows;
            m_BytesPerRow = (property.bitColumns + 7) / 8;
            m_CacheMap = vector<unsigned char>(m_BytesPerRow * m_Height);
            for (int k = 0; k < m_CacheMap.size(); ++k)
            {
                m_CacheMap[k] = property.outBits[k];
            }
            m_ErrorMSG = "";
            return true;
        }
        case PDF417_ERROR_TEXT_TOO_BIG:
        {
            m_ErrorMSG = "输入数据text过大";
            break;
        }
        case PDF417_ERROR_INVALID_PARAMS:
        {
            m_ErrorMSG = "条款参数设置异常";
            break;
        }
        default:
        {
            m_ErrorMSG = "未知错误";
            break;
        }
    }
    pdf417free(&property);
    return false;
}

bool pdf417::output2postscript(const std::string &path)
{
    FILE *fp = fopen(path.c_str(), "wb");
    if(fp)
    {
        int cols = m_BytesPerRow;
        fprintf(fp, "%%!PS1.3\n");
        fprintf(fp, "/Times findfont\n");
        fprintf(fp, "8 scalefont setfont\n");
        fprintf(fp, "100 92 moveto\n");
        switch(m_ShapeFit)
        {
        case PDF417_FIXED_RECTANGLE:
        {
            fprintf(fp, "(FIXED_RECTANGLE row: %d, column: %d)show\n", m_Height, m_Width);
            break;
        }
        case PDF417_FIXED_COLUMNS:
        {
            fprintf(fp, "(FIXED_COLUMNS row: %d, column: %d)show\n", m_Height, m_Width);
            break;
        }
        case PDF417_FIXED_ROWS:
        {
            fprintf(fp, "(FIXED_ROWS row: %d, column: %d)show\n", m_Height, m_Width);
            break;
        }
        case PDF417_USE_ASPECT_RATIO:
        {
            fprintf(fp, "(USE_ASPECT_RATIO ratio: %f, yheight: %f, row: %d, column: %d)show\n", m_AspectRatio, m_YHeight, m_Height, m_Width);
            break;
        } 
        }
        fprintf(fp, "stroke\n100 100 translate\n%g %g scale\n", (double)m_Width, (double)m_Height);
        fprintf(fp, "%d %d 1 [%d 0 0 %d 0 %d]{<", m_Width, m_Height, m_Width, -m_Height, m_Height);
        for (int k = 0; k < m_CacheMap.size(); ++k)
        {
            if (!(k % cols))
            {
                fprintf(fp, "\n");
            }  
            fprintf(fp, "%02X", m_CacheMap[k] & 0xff);
        }
        fprintf(fp, "\n>}image\nshowpage\n");
        fclose(fp);
        return true;
    }
    else
    {
        cerr << "文件打开失败请检查所在目录是否拥有访问和读写权限!" << endl;
    }
    return false;
}

main.cpp

#include <iostream>
#include <pdf417.h>

int main(int argc, const char *argv[])
{
    pdf417 barcode;
    int columnfit = 6;
    int pixelBitDepth = 1;
    bool ifInvertPixel = true;
    int errorLevel = -1;
    float ratio = 0.5;
    float yHeight = 3.0;
    int rowfit = 16;
    barcode.setBarCodeProperty(columnfit, pixelBitDepth, ifInvertPixel, errorLevel, pdf417::FitPolicy::FixedRows, ratio, yHeight, rowfit);
    std::string data = "1234567890abcdefg1234567890abcdefg";
    barcode.printBarCode(data);
    barcode.output2postscript("file.ps");
    return 0;
}

处理pdf417lib往标准输出里面输出调试信息的问题

pdf417lib.c第693行

printf("%c%.*s\n", v->type, v->end - v->start, p->param->text + v->start);
修改为
fprintf(stderr, "%c%.*s\n", v->type, v->end - v->start, p->param->text + v->start);

测试

cmake .. && make -j8 && ./testbarcode && ps2pdf12 file.ps file.pdf

标签:FIXED,封装,NAME,int,pdf417,PDF417,pdf417lib,使用,property
From: https://www.cnblogs.com/yuanhaoblog/p/18091947

相关文章

  • 使用hbuilderX开发小程序遇到的坑
    第一,微信小程序编译每次都出现[project.config.json文件内容错误]project.config.json:libVersion字段需为string解决办法第二,修改了pages文件夹的.vue页面发现页面空白没生效原因:修改要记得保存第三, 微信开发者工具中报错:routeDonewithawebviewId2thati......
  • pinctrl使用实例
    不同半导体厂商的pinctrl设计均不同,这里以高通的pinctrl使用举例: dts修改://mtp-pinctrl.dtsileds_redon:leds_redon{mux{pins="gpio161";function="gpio";};config{......
  • Java面试题:用Java并发工具类,实现一个线程安全的单例模式;使用Java并发工具包和并发框架
    面试题一:设计一个Java并发工具类,实现一个线程安全的单例模式,并说明其工作原理。题目描述:请设计一个Java并发工具类,实现一个线程安全的单例模式。要求使用Java内存模型、原子操作、以及Java并发工具包中的相关工具。考察重点:对Java内存模型的理解。对Java并发工具包的了......
  • 使用两级缓存框架 J2Cache
    J2Cache是OSChina目前正在使用的两级缓存框架(要求至少Java8)。第一级缓存使用内存,同时支持Ehcache2.x、Ehcache3.x和Caffeine(推荐)。第二级缓存使用Redis(推荐)/Memcached。由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2的读取次数......
  • vue2 在 main.js 中定义全局函数,在二次封装的 api\index.js 中引用全局函数 GPT4 Tur
    在Vue2中,你可以通过Vue的原型系统来定义全局函数,然后在整个应用的任何组件中使用这些函数。同样,你也可以在其他JavaScript文件中使用这些函数,比如你提到的二次封装的API文件。下面是如何实现这一过程的步骤:###第一步:在`main.js`中定义全局函数在Vue项目的入口文件`main.js`中,你......
  • cmake之find_library使用问题
    附上工程源码demo工程PS:这个工程用于导出库CMakeLists.txtcmake_minimum_required(VERSION3.5)project(demoLANGUAGESCXX)set(CMAKE_INCLUDE_CURRENT_DIRON)set(CMAKE_CXX_STANDARD11)set(CMAKE_CXX_STANDARD_REQUIREDON)add_library(demoSHAREDdemo.cpp......
  • python之迭代器和生成器的使用方式
    下面我将分别介绍迭代器和生成器的使用示例:迭代器示例:迭代器是一种对象,它可以在遍历时逐个访问元素而不需要将所有元素加载到内存中。下面是一个简单的迭代器示例,该迭代器生成斐波那契数列的前n个数字:classFibonacciIterator:def__init__(self,n):self.n=......
  • 实验:基于Red Hat Enterprise Linux系统在终端使用vim进行拷贝、删除、查找、替换、保
    目录一.实验目的二.实验内容三.实验设计描述及实验结果        一.vim文本编译器模式切换:    命令模式:        输入模式:        末行模式:        二.复制、删除:        三.查找字符串:        四.替换:......
  • 基于Python代码的相关性热力图,VIF共线性诊断图及残差四图的使用及解释
    注:热力图和共线性诊断图易看易解释,这里不再阐述残差四图(ResidualsvsFittedPlot,NormalQ-QPlot,Scale-LocationPlot,Cook'sDistancePlot)各种现象的相关解释如下:ResidualsvsFittedPlot(残差与拟合值散点图):这个图用于帮助检验回归模型的线性关系假设。在这个图中,我......
  • Arrays简单的使用方法
    数组packagemethod;importjava.util.Arrays;publicclassArrayDemo04{publicstaticvoidmain(String[]args){int[]a={1,2,15,47,56,22,222,11,4,4455};//Arrays.sort(a);//排序:升序Arrays.fill(a,2,4,0);//从2-4被0填充,fill填充......