CodeBeamOrg / CodeBeam.MudBlazor.Extensions

Useful third party extension components for MudBlazor, from the contributors.
https://mudextensions.codebeam.org/
MIT License
367 stars 62 forks source link

MudBarcode component Code128 invalid width and height settings #253

Closed Qiulin-Git closed 1 month ago

Qiulin-Git commented 11 months ago
  1. Code128 invalid width and height settings
  2. I hope the QR code component can add logo images
mckaragoz commented 11 months ago

Do you have an example about logo images?

Qiulin-Git commented 11 months ago

Do you have an example about logo images?

Here is an example that can only run on Windows

public class BarcodeHelper
{

    /// <summary>
    /// 生成带Logo的二维码
    /// </summary>
    /// <param name="text">内容</param>
    /// <param name="logoPath">Logo 图片</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    public static Bitmap CreateQRCodeWithLogo(string text, string logoPath, int width, int height)
    {

        Bitmap logo = new Bitmap(logoPath);
        //构造二维码写码器
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
        hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        //生成二维码
        BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width , height , hint);
        bm = deleteWhite(bm);
        BarcodeWriter barcodeWriter = new BarcodeWriter();
        Bitmap map = barcodeWriter.Write(bm);

        //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
        int[] rectangle = bm.getEnclosingRectangle();

        //计算插入图片的大小和位置
        int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
        int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
        int middleL = (map.Width - middleW) / 2;
        int middleT = (map.Height - middleH) / 2;

        //将img转换成bmp格式,否则后面无法创建Graphics对象
        Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(bmpimg))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.DrawImage(map, 0, 0);
            //白底将二维码插入图片
            g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
            g.DrawImage(logo, middleL, middleT, middleW, middleH);
        }
        return bmpimg;
    }

    /// <summary>
    /// 删除默认对应的空白
    /// </summary>
    /// <param name="matrix"></param>
    /// <returns></returns>
    private static BitMatrix deleteWhite(BitMatrix matrix)
    {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++)
        {
            for (int j = 0; j < resHeight; j++)
            {
                if (matrix[i + rec[0], j + rec[1]])
                    resMatrix[i, j] = true;
            }
        }
        return resMatrix;
    }

}