二维码放大

很多库生成的原始二维码都是很小的,我们需要按照像素点位放大二维码

ZoomQrcode.h

#pragma once
#include "windows.h"
//GDI+
#include   
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
using namespace std;

#include "Strtool.h"


class ZoomQrcode {
private:
    ULONG_PTR m_gdiplusToken;
    Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;
    bool init_succ_;
public:
    ZoomQrcode();
    ~ZoomQrcode();
    bool GetCodecClsid(LPCTSTR lpstrFormat, CLSID &clsid);
    bool Zoom(wstring src_path, wstring out_path, int size);
    

};

ZoomQrcode.cpp

#include "ZoomQrcode.h"
#include "stdio.h"

//初始化
ZoomQrcode::ZoomQrcode() :
    init_succ_(true)
{

    if (Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL) != Gdiplus::Ok)
    {
        printf("Gdiplus初始化失败");
        init_succ_ = false;
        return;
    }

    init_succ_ = true;

}

ZoomQrcode::~ZoomQrcode() {
    Gdiplus::GdiplusShutdown(m_gdiplusToken);
}


//data字符串数据,size要生成的二维码大小,pic_path生成的图片路径
bool ZoomQrcode::Zoom(wstring src_path, wstring out_path, int size) {
    Gdiplus::Bitmap bitmap_src(src_path.c_str());
    int srcImgWidth = bitmap_src.GetWidth();
    int srcImgHeight = bitmap_src.GetHeight();
    if (srcImgWidth <= 0)
        return false;

    //计算出需要放大的比例
    int scale_rate = size / srcImgWidth;

    Gdiplus::Bitmap bitmap_out(srcImgWidth*scale_rate, srcImgHeight*scale_rate);
    //填上白底
    Gdiplus::Graphics graphics(&bitmap_out);
    Gdiplus::SolidBrush brush(Gdiplus::Color(255, 255, 255, 255));
    graphics.FillRectangle(&brush, 0, 0, bitmap_out.GetWidth(), bitmap_out.GetHeight());

    //根据二维码像素点数据 开始绘制
    Color color;
    for (int i = 0; i < srcImgWidth; ++i)
    {
        for (int j = 0; j < srcImgHeight; ++j)
        {
            //获得当前像素点的颜色,如果有数据,则画上黑色
            bitmap_src.GetPixel(i, j, &color);
            if (color.GetRed() <=150
                && color.GetGreen() <= 150
                && color.GetBlue() <= 150)
            {
                //原始像素
                //bitmap_qrcode.SetPixel(i, j, RGB(0, 0, 0));
                //放大的倍数,每个像素点放大
                for (int x = 0; x < scale_rate; x++)
                    for (int y = 0; y < scale_rate; y++)
                        bitmap_out.SetPixel(i*scale_rate + x, j * scale_rate + y, RGB(1, 1, 1));


            }

        }
    }

    //保存图片
    CLSID pngclsid = { 0 };
    GetCodecClsid(TEXT("image/jpeg"), pngclsid);

    DeleteFile(out_path.c_str());
    bitmap_out.Save(out_path.c_str(), &pngclsid);

    if (GetFileAttributes(out_path.c_str()) == INVALID_FILE_ATTRIBUTES)
        return false;

    return true;
}



bool ZoomQrcode::GetCodecClsid(LPCTSTR lpstrFormat, CLSID &clsid)
{

    UINT nNum = 0, nSize = 0;
    Gdiplus::GetImageEncodersSize(&nNum, &nSize) == Gdiplus::Ok;

    Gdiplus::ImageCodecInfo *pInfo = (Gdiplus::ImageCodecInfo *)malloc(nSize);


    if (Gdiplus::GetImageEncoders(nNum, nSize, pInfo) != Gdiplus::Ok)
    {
        free(pInfo);
    }

    LPCWSTR lpstr = lpstrFormat;

    for (int nIndex = 0; nIndex < nNum; ++nIndex)
    {
        if (wcscmp(pInfo[nIndex].MimeType, lpstr) == 0)
        {
            clsid = pInfo[nIndex].Clsid;
            return true;
        }
    }

    return false;
};

你可能感兴趣的:(二维码放大)