欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DataMatrix识别及定位项目笔记(1)——基于QT+libdmtx-0.7.5的DataMatrix生成器

程序员文章站 2022-05-18 18:35:45
...

基于qt和libdmtx库生成DataMatrix的工程,

DataMatrix开源的编码及识别库libdmtx的github:https://github.com/dmtx/libdmtx

我用qt写的完整工程在:https://github.com/abcvincent/dmtxMaker

github->doc中有DataMatrix国际标准文件和知乎的详解QR码的文档;

效果如下:

DataMatrix识别及定位项目笔记(1)——基于QT+libdmtx-0.7.5的DataMatrix生成器

下面是简单的测试代码,qt工程:

//测试代码
//    QString str = "DataMatrix";
QString str = "123456789";
DmtxEncode* encode = dmtxEncodeCreate();
assert(encode != NULL);
encode->moduleSize = 5;
encode->marginSize = 5;
encode->sizeIdxRequest=DmtxSymbolSquareAuto;//设置类型默认

//    dmtxEncodeSetProp( encode, DmtxPropModuleSize, 5); // encode->moduleSize = 5;
//    dmtxEncodeSetProp( encode, DmtxPropMarginSize,20); //  encode->marginSize = 10;

//    下面是代码中默认函数设置 dmtxencode.c->dmtxEncodeCreate(void);
//    encode->scheme = DmtxSchemeAscii;
//    encode->sizeIdxRequest = DmtxSymbolSquareAuto;
//    encode->marginSize = 10;
//    encode->moduleSize = 5;
//    encode->pixelPacking = DmtxPack24bppRGB;
//    encode->imageFlip = DmtxFlipNone;
//    encode->rowPadBytes = 0;
//    encode->fnc1 = DmtxUndefined;

//    int ret = dmtxEncodeDataMatrix(encode, strlen(str.toStdString().c_str()), (unsigned char*)str.toStdString().c_str());//案例函数c语言
int ret = dmtxEncodeDataMatrix(encode, str.size(), (uchar*)str.toStdString().data());
assert(ret == 1);

int width = dmtxImageGetProp(encode->image, DmtxPropWidth);
int height = dmtxImageGetProp(encode->image, DmtxPropHeight);
int bytesPerPixel = dmtxImageGetProp(encode->image, DmtxPropBytesPerPixel);
int bytesPerLine = dmtxImageGetProp(encode->image, DmtxPropRowSizeBytes);

uchar *pxlData = (uchar *)malloc(width*height*bytesPerPixel);
memcpy(pxlData,encode->image->pxl,width*height*bytesPerPixel);
dmtxEncodeDestroy(&encode);

QImage img = QImage(pxlData,width,height,bytesPerLine,QImage::Format_RGB888);//如果RGB888不行换别的format
QImage imgshow=img.scaled( this->ui->label->width(), this->ui->label->height(),Qt:: KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(imgshow));

下面是libdmtx库中test的例子,纯c代码,可移植嵌入式,输出的是0和1的数据值,每个代表一个像素;

    //libdmtx 自带 example
    size_t          width, height, bytesPerPixel;
    unsigned char   str[] = "30Q324343430794<OQQ";
    unsigned char  *pxl;
    DmtxEncode     *enc;
    DmtxImage      *img;
    DmtxDecode     *dec;
    DmtxRegion     *reg;
    DmtxMessage    *msg;

    //1) ENCODE a new Data Matrix barcode image (in memory only)
    enc = dmtxEncodeCreate();
    //dmtxEncodeSetProp( enc, DmtxPropPixelPacking, DmtxPack16bppRGB );
    //dmtxEncodeSetProp( enc, DmtxPropPixelPacking, DmtxPack32bppRGB );
    //dmtxEncodeSetProp( enc, DmtxPropWidth, 160 );
    //dmtxEncodeSetProp( enc, DmtxPropHeight, 160 );

    assert(enc != NULL);
    dmtxEncodeDataMatrix(enc, strlen((const char *)str), str);
    //2) COPY the new image data before releasing encoding memory
    width = dmtxImageGetProp(enc->image, DmtxPropWidth);
    height = dmtxImageGetProp(enc->image, DmtxPropHeight);
    bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel);
    pxl = (unsigned char *)malloc(width * height * bytesPerPixel);//malloc c语言的函数
    assert(pxl != NULL);
    memcpy(pxl, enc->image->pxl, width * height * bytesPerPixel);

    //    int width = dmtxImageGetProp(enc->image, DmtxPropWidth);
    //    int height = dmtxImageGetProp(enc->image, DmtxPropHeight);
    //    int bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel);
    int bytesPerLine = dmtxImageGetProp(enc->image, DmtxPropRowSizeBytes);
    dmtxEncodeDestroy(&enc);//清除内存
    fprintf(stdout, "width:  \"%d\"\n", width);
    fprintf(stdout, "height: \"%d\"\n", height);
    fprintf(stdout, "bpp:    \"%d\"\n", bytesPerPixel);

    for (int i=0; i<width*height; i++){
       fprintf(stdout, "%d", (pxl[i*3])==0);
       if (i%width==width-1){
          fprintf(stdout, "\n");

 

相关标签: 项目笔记