C#使用ZXing解码二维码出现中文乱码的问题

        public string ZXing_Decode(Bitmap barcodeBitmap)
        {
            try
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options = new ZXing.Common.DecodingOptions
                {
                    CharacterSet = "ISO-8859-1"
                };
                var result = reader.Decode(barcodeBitmap);
                if (result != null && result.Text != null)
                {
                    byte[] isoBytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(result.Text);
                    string value = Encoding.UTF8.GetString(isoBytes);
                    if (value.Contains(""))
                    {
                        //是"�"这个特殊字符的乱码情况,使用gb2312
                        value = Encoding.GetEncoding("gb2312").GetString(isoBytes);
                    }
                    return value;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

posted @ 2023-11-22 11:43  因雨  阅读(589)  评论(0)    收藏  举报