生成指定名称的二维码图片

int CAT_OLC_DUTDlg::QRGenerator(CString image,CString str/*char *str[]*/)
{
    string str0 = (CStringA)str; 
    string ImageName = (CStringA)image;
    char *szSourceSring = new char[str0.length() + 1];//QRCODE_TEXT
    strcpy(szSourceSring, str0.c_str());

    unsigned int    unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
    unsigned char*    pRGBData, *pSourceData, *pDestData;
    QRcode*            pQRC;
    FILE*            f;

    /*
    * Create a symbol from the string. The library automatically parses the input
    * string and encodes in a QR Code symbol.
    * @warning This function is THREAD UNSAFE when pthread is disabled.
    * @param string input string. It must be NUL terminated.
    * @param version version of the symbol. If 0, the library chooses the minimum
    *                version for the given input data.
    * @param level error correction level.
    * @param hint tell the library how non-alphanumerical characters should be
    *             encoded. If QR_MODE_KANJI is given, kanji characters will be
    *             encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
    *             non-alphanumerical characters will be encoded as is. If you want
    *             to embed UTF-8 string, choose this.
    * @param casesensitive case-sensitive(1) or not(0).
    * @return an instance of QRcode class. The version of the result QRcode may
    *         be larger than the designated version. On error, NULL is returned,
    *         and errno is set to indicate the error. See Exceptions for the
    *         details.
    * @throw EINVAL invalid input object.
    * @throw ENOMEM unable to allocate memory for input objects.
    * @throw ERANGE input data is too large.
    */

    // Compute QRCode
    if (pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1))
    {
        unWidth = pQRC->width;
        unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 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)))
        {
            AfxMessageBox(L"Out of memory");
            //printf("Out of memory");
            exit(-1);
        }

        // Preset to white-预设为白色
        memset(pRGBData, 0xff, unDataBytes);

        // Prepare bmp headers
        BITMAPFILEHEADER1 kFileHeader;
        kFileHeader.bfType = 0x4d42;  // "BM"
        kFileHeader.bfSize =    sizeof(BITMAPFILEHEADER1) +
            sizeof(BITMAPINFOHEADER1) +
            unDataBytes;
        kFileHeader.bfReserved1 = 0;
        kFileHeader.bfReserved2 = 0;
        kFileHeader.bfOffBits =    sizeof(BITMAPFILEHEADER1) +
            sizeof(BITMAPINFOHEADER1);

        BITMAPINFOHEADER1 kInfoHeader;
        kInfoHeader.biSize = sizeof(BITMAPINFOHEADER1);
        kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;
        kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER);
        kInfoHeader.biPlanes = 1;
        kInfoHeader.biBitCount = 24;
        kInfoHeader.biCompression = BI_RGB;
        kInfoHeader.biSizeImage = 0;
        kInfoHeader.biXPelsPerMeter = 0;
        kInfoHeader.biYPelsPerMeter = 0;
        kInfoHeader.biClrUsed = 0;
        kInfoHeader.biClrImportant = 0;


        // Convert QrCode bits to bmp pixels
        pSourceData = pQRC->data;
        for(y = 0; y < unWidth; y++)
        {
            pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER;
            for(x = 0; x < unWidth; x++)
            {
                if (*pSourceData & 1)
                {
                    for(l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++)
                    {
                        for(n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++)
                        {
                            *(pDestData +        n * 3 + unWidthAdjusted * l) =    PIXEL_COLOR_B;
                            *(pDestData + 1 +    n * 3 + unWidthAdjusted * l) =    PIXEL_COLOR_G;
                            *(pDestData + 2 +    n * 3 + unWidthAdjusted * l) =    PIXEL_COLOR_R;
                        }
                    }
                }
                pDestData += 3 * OUT_FILE_PIXEL_PRESCALER;
                pSourceData++;
            }
        }

        // Output the bmp file
        if (!(fopen_s(&f, ImageName.c_str(), "wb")))
        {
            fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER1), 1, f);
            fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER1), 1, f);
            fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
            fclose(f);
        }


        //if(QRFlag == 0)
        //{
        //    //AfxMessageBox(L"QRFlag == 0");
        //    QRFlag = 1;
        //    if (!(fopen_s(&f, OUT_FILE1, "wb")))
        //    {
        //        fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER1), 1, f);
        //        fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER1), 1, f);
        //        fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
        //        fclose(f);
        //    }
        //    else
        //    {
        //        AfxMessageBox(L"Unable to open file");
        //        //printf("Unable to open file");
        //        exit(-1);
        //    }
        //}
        //else{
        //    if (!(fopen_s(&f, OUT_FILE, "wb")))
        //    {
        //        fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER1), 1, f);
        //        fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER1), 1, f);
        //        fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);

        //        fclose(f);
        //    }
        //    else
        //    {
        //        AfxMessageBox(L"Unable to open file");
        //        //printf("Unable to open file");
        //        exit(-1);
        //    }
        //}
        // Free data
        free(pRGBData);
        QRcode_free(pQRC);
    }
    else
    {
        AfxMessageBox(L"NULL returned");
        //printf("NULL returned");
        exit(-1);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值