本望断在图像生成Data-Matrix格式的二维码
根据此编写出的生成QR-Code和Data-Matrix的二维码(库)
https://download.csdn.net/download/wangduanqiugao/10383903
本秋高使用到的软件工具:VS2010 dmtx.h libdmtx-0.7.4.lib(使用以上现有库直接实现功能)
VS2010就不用说了;使用静态库函数的步骤也可以的,此部分作基础就不进行阐述
libdmtx-0.7.4版本参考网址:
https://blog.csdn.net/wangduanqiugao/article/details/78696337(资源包附赠(二维码生成图片小软件))
#include "dmtx.h"
#include <iostream>
using namespace std;
#define PIXEL_COLOR_R 0// Color of bmp pixels
#define PIXEL_COLOR_G 0
#define PIXEL_COLOR_B 0
#define OUT_FILE_PIXEL_PRESCALER 8
//BMP defines
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef signed long LONG;
#define BI_RGB 0L
#pragma pack(push, 2)
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
}
BITMAPFILEHEADER1;
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}
BITMAPINFOHEADER1;
#pragma pack(pop)
bool daixuechuping_Data_Matrix(char *DM_text, char *bmpSavedPath)
{
size_t width, height, bytesPerPixel;
string DXstring = DM_text;
const char* CPsring = bmpSavedPath;
unsigned int unWidth, i, j,unWidthAdjusted, unDataBytes;
unsigned char* pRGBData;
unsigned char *pxl;
DmtxEncode *enc;
FILE* DXCP;
/* 1) ENCODE a new Data Matrix barcode image (in memory only) */
enc = dmtxEncodeCreate();
assert(enc != NULL);
dmtxEncodeDataMatrix(enc, strlen(DXstring.c_str()), (unsigned char*)DXstring.c_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);
assert(pxl != NULL);
memcpy(pxl, enc->image->pxl, width * height * bytesPerPixel);
std::string hex(width * height * bytesPerPixel * 3, '\0');
int pos = 0;
long vi = 0;
long vv = 0;
vv = (long) width * height * bytesPerPixel;
for(vi = 0; vi < vv; vi ++)
{
pos += sprintf(&hex.front() + pos, "%02X " , pxl[vi]); //注意02X后有空格
}
int num = 0;
int number = 0;
unsigned char* Dep = (unsigned char*)malloc(width * height / 25);
for (vi = 0; vi < pos - 1; vi += 45)
{if (hex[vi] == 'F')
{num ++;
Dep[number] = '0'; //白色
number ++;
}
if (hex[vi] == '0')
{num ++;
Dep[number] = '1'; //黑色
number ++;
}
if (num == width / 5)
{num = 0;
vi = vi + 45 * width / 5 * (5 - 1);
}
}
width = width / 5;
height = height / 5;
unWidth = width; //矩阵的维数
unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3; //每一个维度占的像素的个数(8),每个像素3个字节
if (unWidthAdjusted % 4)
{
unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
}
unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER;
// Allocate pixels buffer
if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
{
return false;
}
// Preset to white
memset(pRGBData, 0xff, unDataBytes);
// Prepare bmp headers
BITMAPFILEHEADER1 bmpFileHeader;
bmpFileHeader.bfType = 0x4d42; // "BM"
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + unDataBytes;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER1 bmpInfoHeader;
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;
bmpInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER); //负数可以控制图像上下颠倒
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biSizeImage = 0;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
//Convert QrCode bits to bmp pixels
int iSub = 0, jSub = 0, iCur = 0, jCur = 0;
for( i = 0; i < width; i++)
{
iCur = unWidthAdjusted * i * OUT_FILE_PIXEL_PRESCALER;
for(j = 0; j < height; j++)
{
if(Dep[i * width + j] == '1')
{
for(iSub = 0; iSub < OUT_FILE_PIXEL_PRESCALER; iSub++)
{
for(jSub = 0; jSub < OUT_FILE_PIXEL_PRESCALER; jSub++)
{
jCur = iCur + j * 3 * OUT_FILE_PIXEL_PRESCALER + unWidthAdjusted * iSub + jSub * 3;
pRGBData[jCur + 0] = PIXEL_COLOR_B;
pRGBData[jCur + 1] = PIXEL_COLOR_G;
pRGBData[jCur + 2] = PIXEL_COLOR_R;
}
}
}
}
}
//Output the bmp file
if (!(fopen_s(&DXCP, CPsring, "wb")))
{
fwrite(&bmpFileHeader, sizeof(BITMAPFILEHEADER1), 1, DXCP);
fwrite(&bmpInfoHeader, sizeof(BITMAPINFOHEADER1), 1, DXCP);
fwrite(pRGBData, sizeof(unsigned char), unDataBytes, DXCP);
fclose(DXCP);
}
else
{
return false;
}
//Free data
free(pRGBData);
return true;
}