CodeInfo.cs 2.6 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IDHIKCamera
{
    /// <summary>
    /// 条码信息
    /// </summary>
    [Serializable]
    public class CodeInfo
    {
        public string CodeStr = "";
        public int X = 0;
        public int Y = 0;
        public string CodeType;
        public double Orientation = 0;
        public CodeInfo()
        {

        }
        public CodeInfo(string codeStr, int x, int y)
        {
            this.CodeStr = codeStr;
            this.X = x;
            this.Y = y;
        }
        public CodeInfo(string codeStr, int x, int y, string type)
        {
            this.CodeType = type;
            this.CodeStr = codeStr;
            this.X = x;
            this.Y = y;
        }
        public string GetCodeStr()
        {
            return Gb2312Correct(CodeStr);
        }
        /// <summary>
        /// 判断字符串中是否包含中文
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>判断结果</returns>
        public static bool HasChinese(string str)
        {
            return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }
        /// <summary>
        /// utf8文字用gb2312格式显示时候乱码,需要转换为gb2312
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Gb2312Correct(string text)
        {
            //string ascii= Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(text));
            //return ascii;
            if (!HasChinese(text))
            {
                return text;
            }
            //声明字符集   
            System.Text.Encoding utf8, gb2312;
            //utf8   
            utf8 = System.Text.Encoding.GetEncoding("utf-8");
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            byte[] gb;
            gb = utf8.GetBytes(text);
            //utf8.GetString(System.Text.Encoding.Convert(utf8, gb2312, gb));
            gb = System.Text.Encoding.Convert(utf8, gb2312, gb);
            //返回转换后的字符   

            string s = utf8.GetString(gb);
            int sp = 0;
            while (true)
            {
                sp = s.IndexOf("?", sp + 1);
                if (sp < 0)
                    break;
                if (s.Substring(sp, 2) != "?;")
                {
                    s = s.Insert(sp + 1, ";");
                }
            }
            return s;
        }
    }
}