bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing;
using ZXing.Common;
using ZXing.OneD;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
 
namespace Utility.Tools
{
    public class BarcodeHelper
    {
        static readonly string prefix = "data:image/png;base64,";
        public static Bitmap GetCodeBar(string txt, int width, int height, bool hideCode = false)
        {
            Code128EncodingOptions options = new Code128EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 0,
                PureBarcode = hideCode,
                ForceCodesetB = true,
                GS1Format = false
            };
 
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.CODE_128,
                Options = options,
            };
            try
            {
                Bitmap map = writer.Write(txt);
                return map;
            }
            catch (Exception e)
            {
                var msg = e.Message;
                return null;
            }
        }
 
        /// <summary>
        /// 获取一维码的Base64内容【png格式】
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="hideCode"></param>
        /// <returns></returns>
        public static string GetCodeBarBase64(string txt, int width, int height, bool hideCode = false)
        {
            Bitmap map = GetCodeBar(txt, width, height, hideCode);
            if (map == null)
            {
                return string.Empty;
            }
            MemoryStream stream = new MemoryStream();
            try
            {
                map.Save(stream, ImageFormat.Png);
                string re = $"{prefix}{Convert.ToBase64String(stream.ToArray())}";
                return re;
            }
            catch (Exception)
            {
                return string.Empty;
            }
            finally
            {
                map.Dispose();
                stream.Dispose();
            }
        }
        /// <summary>
        /// 获取二维码QR
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap GetQrCode(string txt, int width, int height)
        {
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                ErrorCorrection = ErrorCorrectionLevel.H,
                Margin = 0//设置二维码的边距,单位不是固定像素
            };
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = options
            };
            try
            {
                Bitmap map = writer.Write(txt);
                return map;
            }
            catch (Exception)
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取二维码QR的Base64内容【png格式】
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string GetQrCodeBase64(string txt, int width, int height)
        {
            Bitmap map = GetQrCode(txt, width, height);
            if (map == null)
            {
                return string.Empty;
            }
            MemoryStream stream = new MemoryStream();
            try
            {
                map.Save(stream, ImageFormat.Png);
                string re = $"{prefix}{Convert.ToBase64String(stream.ToArray())}";
                return re;
            }
            catch (Exception)
            {
                return string.Empty;
            }
            finally
            {
                map.Dispose();
                stream.Dispose();
            }
        }
 
        /// <summary>
        /// 获取识别一维码或二维码的结果
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static string GetDecode(string base64)
        {
            string re = string.Empty;
            var bts = Convert.FromBase64String(base64);
            MemoryStream stream = new MemoryStream(bts);
            var map = (Bitmap)Image.FromStream(stream);
            //
            var reader = new BarcodeReader();
            try
            {
                var result = reader.Decode(map);
                if (result != null)
                {
                    re = $"{result.BarcodeFormat}:{result.Text}";
                }
                return re;
            }
            catch (Exception)
            {
                return re;
            }
            finally
            {
                stream.Dispose();
                map.Dispose();
            }
        }
    }
}