基于qt和libdmtx库生成DataMatrix的工程,
DataMatrix开源的编码及识别库libdmtx的github:https://github.com/dmtx/libdmtx;
我用qt写的完整工程在:https://github.com/abcvincent/dmtxMaker;
github->doc中有DataMatrix国际标准文件和知乎的详解QR码的文档;
效果如下:
下面是简单的测试代码,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[] = "30Q324343430794image, 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