交叉编译
./configure --prefix=$(pwd)/_install --host=arm-rockchip-linux-gnueabihf CC=arm-rockchip-linux-gnueabihf-gcc --enable-static
C++生成二维码并保存图片
QImage QrcodeEn::encodeImage(const QString& qrTxt, int bulk,const QString& saveName)
{
qDebug() << "QrcodeEn encodeImage";
QImage ret;
QRcode* qr = QRcode_encodeString(qrTxt.toUtf8(), 12, QR_ECLEVEL_Q, QR_MODE_8, 1);
if ( qr != nullptr )
{
int allBulk = (qr->width) * bulk;
ret = QImage(allBulk, allBulk, QImage::Format_MonoLSB);
QPainter painter(&ret);
QColor fg("black");
QColor bg("white");
painter.setBrush(bg);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, allBulk, allBulk);
painter.setBrush(fg);
for( int y=0; y<qr->width; y++ )
{
for( int x=0; x<qr->width; x++ )
{
if ( qr->data[y*qr->width+x] & 1 )
{
QRectF r(x*bulk, y*bulk, bulk, bulk);
painter.drawRects(&r, 1);
}
}
}
QRcode_free(qr);
if(saveName!="")
ret.save(saveName);
}
return ret;
}
C语言生成二维码
int qrencodeString(char *QRTEXT)
{
QRcode *qrCode = NULL;
int version = 12;
QRecLevel level = QR_ECLEVEL_Q;
QRencodeMode hint = QR_MODE_8;
int casesensitive = 1;
qrCode = QRcode_encodeString(QRTEXT, version, level, hint, casesensitive);
if (NULL == qrCode)
{
printf("QRcode create fail\n");
return -1;
}
printf("QRcode create success\n");
saveQrcode("qrcode24.bmp", qrCode, 24);
QRcode_free(qrCode);
resizeBmp("qrcode24.bmp", "qrcode24bit.bmp", 4);
return 0;
}
保存二维码
int saveQrcode(const char *qrcodeName, QRcode *qrCode, const unsigned char bmpBitCount)
{
int width = qrCode->width;
int height = qrCode->width;
int widthByte = (width * bmpBitCount / 8 + 3) / 4 * 4;
int bodySize = widthByte * height;
unsigned char bmpByteCount = bmpBitCount / 8;
unsigned char *qrBmpData = malloc(bodySize);
memset(qrBmpData, 255, bodySize);
unsigned char *qrData = qrCode->data;
int i, j, k;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
if (*(qrData)&1)
{
for (k = 0; k < bmpByteCount; ++k)
{
*(qrBmpData + widthByte * i + bmpByteCount * j + k) = 0;
}
}
qrData++;
}
}
saveBitmap(qrcodeName, qrBmpData, width, -height, bodySize, bmpBitCount);
free(qrBmpData);
return 0;
}
保存BMP图片
int saveBitmap(const char *bmpName, void *bmpData, int width, int height, int bodySize, const unsigned char bmpBitCount)
{
FILE *pf = fopen(bmpName, "wb");
if (NULL == pf)
{
printf("%s file open fail.\n", bmpName);
goto fail;
}
BITMAPFILEHEADER bitMapFileHeader;
bitMapFileHeader.bfType = 0x4D42;
bitMapFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bodySize;
bitMapFileHeader.bfReserved1 = 0;
bitMapFileHeader.bfReserved2 = 0;
bitMapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER bitMapInfoHeader;
bitMapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bitMapInfoHeader.biWidth = width;
bitMapInfoHeader.biHeight = height;
bitMapInfoHeader.biPlanes = 1;
bitMapInfoHeader.biBitCount = bmpBitCount;
bitMapInfoHeader.biCompression = 0;
bitMapInfoHeader.biSizeImage = bodySize;
bitMapInfoHeader.biXPelsPerMeter = 0;
bitMapInfoHeader.biYPelsPerMeter = 0;
bitMapInfoHeader.biClrUsed = 0;
bitMapInfoHeader.biClrImportant = 0;
if (fwrite(&bitMapFileHeader, sizeof(BITMAPFILEHEADER), 1, pf) != 1)
{
printf("fwrite bitMapFileHeader fail.\n");
goto fail;
}
if (fwrite(&bitMapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pf) != 1)
{
printf("fwrite bitMapInfoHeader fail.\n");
goto fail;
}
if (fwrite(bmpData, bodySize, 1, pf) != 1)
{
printf("fwrite bmpData fail.\n");
goto fail;
}
fclose(pf);
return 0;
fail:
fclose(pf);
return -1;
}
读取BMP图片
void *readBitmap(const char *bmpName, BITMAPFILEHEADER *bmpHeader, BITMAPINFOHEADER *bmpInfoHeader, int *bodySize)
{
FILE *fd = fopen(bmpName, "rb");
if (NULL == fd)
{
printf("file open fail.\n");
fclose(fd);
return NULL;
}
if (fread(bmpHeader, 1, sizeof(BITMAPFILEHEADER), fd) != sizeof(BITMAPFILEHEADER))
{
printf("fread fail.\n");
return NULL;
}
if (fread(bmpInfoHeader, 1, sizeof(BITMAPINFOHEADER), fd) != sizeof(BITMAPINFOHEADER))
{
printf("fread fail.\n");
return NULL;
}
*bodySize = bmpHeader->bfSize - 54;
void *body = malloc(*bodySize);
fseek(fd, 54, SEEK_SET);
if (fread(body, 1, *bodySize, fd) != *bodySize)
{
printf("fread fail.\n");
return NULL;
}
fclose(fd);
return body;
}
缩放BMP图片
int resizeBmp(const char *srcBmpName, const char *dstBmpName, const unsigned char multiple)
{
BITMAPFILEHEADER srcBmpHeader;
BITMAPINFOHEADER srcBmpInfoHeader;
unsigned char *srcBmpData = NULL;
int srcBodySize = 0;
srcBmpData = readBitmap(srcBmpName, &srcBmpHeader, &srcBmpInfoHeader, &srcBodySize);
if (srcBmpData == NULL)
{
printf("readBitmap fail\n");
return -1;
}
unsigned char bmpByteCount = srcBmpInfoHeader.biBitCount / 8;
int srcWidthByte = (srcBmpInfoHeader.biWidth * bmpByteCount + 3) / 4 * 4;
int width = srcBmpInfoHeader.biWidth * multiple;
int height = srcBmpInfoHeader.biHeight * multiple;
int dstWidthSize = (width * srcBmpInfoHeader.biBitCount / 8 + 3) / 4 * 4;
int dstBodySize = dstWidthSize * abs(height);
unsigned char *dstBmpData = malloc(dstBodySize);
if (dstBmpData == NULL)
{
printf("resizeBmp malloc error:%d\n",dstBodySize);
free(srcBmpData);
return -1;
}
memset(dstBmpData, 0, dstBodySize);
printf("resizeBmp end\n");
double rateH = (double)1 / multiple;
double rateW = (double)1 / multiple;
int tSrcH, tSrcW;
int i, j;
for (i = 0; i < abs(height); i++)
{
tSrcH = (int)(rateH * i);
for (j = 0; j < width; j++)
{
tSrcW = (int)(rateW * j);
memcpy(&dstBmpData[i * dstWidthSize] + j * bmpByteCount, &srcBmpData[tSrcH * srcWidthByte] + tSrcW * bmpByteCount, bmpByteCount);
}
}
saveBitmap(dstBmpName, dstBmpData, width, height, dstBodySize, srcBmpInfoHeader.biBitCount);
free(dstBmpData);
free(srcBmpData);
return 0;
}
BMP RGB888toRGB565
unsigned short RGB888toRGB565(unsigned char red, unsigned char green, unsigned char blue)
{
unsigned short B = ((blue >> 3) & 0x1F) << 0;
unsigned short G = ((green >> 2) & 0x3F) << 5;
unsigned short R = ((red >> 3) & 0x1F) << 11;
return (unsigned short)(R | G | B);
}
int rgb888to565(const char *srcBmpName, const char *dstBmpName, const unsigned char dstBitCount)
{
BITMAPFILEHEADER srcBmpHeader;
BITMAPINFOHEADER srcBmpInfoHeader;
unsigned char *srcBmpData = NULL;
int srcBodySize = 0;
srcBmpData = readBitmap(srcBmpName, &srcBmpHeader, &srcBmpInfoHeader, &srcBodySize);
if (srcBmpData == NULL)
{
printf("readBitmap fail\n");
return -1;
}
int srcWidthByte = (srcBmpInfoHeader.biWidth * srcBmpInfoHeader.biBitCount / 8 + 3) / 4 * 4;
unsigned char srcBmpByteCount = srcBmpInfoHeader.biBitCount / 8;
int width = srcBmpInfoHeader.biWidth;
int height = srcBmpInfoHeader.biHeight;
int widthByte = (width * dstBitCount / 8 + 3) / 4 * 4;
int bodySize = widthByte * height;
unsigned char bmpByteCount = dstBitCount / 8;
unsigned char *dstBmpData = malloc(bodySize);
int i, j;
unsigned short color;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
color = RGB888toRGB565(srcBmpData[i * srcWidthByte + j * srcBmpByteCount + 2], srcBmpData[i * srcWidthByte + j * srcBmpByteCount + 1],
srcBmpData[i * srcWidthByte + j * srcBmpByteCount]);
dstBmpData[i * widthByte + bmpByteCount * j] = color;
dstBmpData[i * widthByte + bmpByteCount * j + 1] = color >> 8;
}
}
saveBitmap(dstBmpName, dstBmpData, width, height, bodySize, dstBitCount);
free(dstBmpData);
free(srcBmpData);
return 0;
}