ZXingCodeHelper.cs 7.8 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.Multi.QrCode;

namespace CodeLibrary
{
    public class ZXingCodeHelper
    {
        public static List<string> DeCodes(Bitmap map, string codeType)
        {
            if (codeType.ToUpper().Equals("QR CODE"))
            {
                return DecodeQRCodes(map);
            }
            else if (codeType.ToUpper().Equals("DATA MATRIX ECC 200"))
            {
                return DecodeCodes(map);
            }
            else if (codeType.ToUpper().Equals("BARCODE"))
            {
                return DecodeCodes(map);
            }
            else
            {
                return DecodeCodes(map);
            }

        }
        public static List<string> DecodeCodes(Bitmap bmp)
        {
            MultiFormatReader mreader = new MultiFormatReader();
            ZXing.Multi.GenericMultipleBarcodeReader genericMultiple = new ZXing.Multi.GenericMultipleBarcodeReader(mreader);
            LuminanceSource source = new BitmapLuminanceSource(bmp);
            BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
            Result[] rr = genericMultiple.decodeMultiple(binarybitmap);

            List<string> result = new List<string>();
            if (rr != null)
            {
                foreach (Result res in rr)
                {
                    if (res != null)
                    {
                        string text = res.ToString();

                        if (!IsGBCode(text))
                        {
                            text = ConvertISO88591ToEncoding(text, Encoding.Default);
                        }
                        result.Add(text);
                    }
                }
            }
            return result;
        }
        public static List<string> DecodeBarCodes(Bitmap bmp)
        {

            List<string> result = new List<string>();
            BarcodeReader br = new BarcodeReader();
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.CODE_39, BarcodeFormat.CODE_128 };
            br.Options = decodeOption;
            Result res = br.Decode(bmp);
            if (res != null)
            {
                result.Add(res.ToString());
            }
            return result;
        }
        public static List<string> DecodeDMCodes(Bitmap bmp)
        {
            ZXing.Datamatrix.DataMatrixReader qc = new ZXing.Datamatrix.DataMatrixReader();
            List<string> result = new List<string>();
            LuminanceSource source = new BitmapLuminanceSource(bmp);
            BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
            IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
            hints.Add(DecodeHintType.CHARACTER_SET, "UTF-8");
            hints.Add(DecodeHintType.TRY_HARDER, "3");
            Result res = qc.decode(binarybitmap, hints);
            if (res != null)
            {
                result.Add(res.ToString());
            }
            return result;
        }
        public static List<string> DecodeQRCodes(Bitmap bmp)
        {
            QRCodeMultiReader qc = new QRCodeMultiReader();
            List<string> result = new List<string>();
            LuminanceSource source = new BitmapLuminanceSource(bmp);
            BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
            IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
            hints.Add(DecodeHintType.CHARACTER_SET, "GB2312");
            hints.Add(DecodeHintType.TRY_HARDER, "3");
            Result[] r = qc.decodeMultiple(binarybitmap, hints);
            if (r != null)
            {
                foreach (Result res in r)
                {
                    if (res != null)
                    {
                        string text = res.ToString();
                        //if (!IsGBCode(text))
                        //{
                        //    string chStr = "生产日期(或厂家批次):";
                        //    int index = text.IndexOf(chStr);
                        //    if (index > 0)
                        //    {
                        //        string sub1 = text.Substring(0, index);
                        //        int sub3startIndex =index+ chStr.Length;
                        //        string sub3 = text.Substring(sub3startIndex,text.Length- sub3startIndex);

                        //        string sub1r = ConvertISO88591ToEncoding(sub1, Encoding.Default);
                        //        string sub3r = ConvertISO88591ToEncoding(sub3, Encoding.Default);
                        //        text = sub1r + chStr + sub3r;
                        //    }
                        //    else
                        //    {
                        //        text = ConvertISO88591ToEncoding(text, Encoding.Default);
                        //    }

                        //}
                        result.Add(text);
                    }
                }
            }
            return result;
        }


        //public static string DecodeQRCode(Bitmap bmp)
        //{
        //    string text = "";
        //    DecodingOptions option = new DecodingOptions();

        //    //option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
        //    option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
        //    BarcodeReader br = new BarcodeReader();

        //    br.Options = option;
        //    Result rs = br.Decode(bmp);

        //    if (rs == null)
        //    {
        //        text = "";
        //    }
        //    else
        //    {
        //        text = rs.ToString();
        //        if (!IsGBCode(text))
        //        {
        //            text = ConvertISO88591ToEncoding(text, Encoding.Default);
        //        }
        //    }
        //    return text;
        //}
        //转换
        private static string ConvertISO88591ToEncoding(string srcString, Encoding dstEncode)
        {
            String sResult;
            Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
            Encoding GB2312Encoding = Encoding.GetEncoding("GB2312"); //这个地方很特殊,必须利用GB2312编码
            byte[] srcBytes = ISO88591Encoding.GetBytes(srcString);
            //将原本存储ISO-8859-1的字节数组当成GB2312转换成目标编码(关键步骤)
            byte[] dstBytes = Encoding.Convert(GB2312Encoding, dstEncode, srcBytes);
            char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
            dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);//利用char数组存储字符
            sResult = new string(dstChars);
            return sResult;
        }
        /// <summary>
        /// 判断一个word是否为GB2312编码的汉字
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private static bool IsGBCode(string word)
        {
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
            if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
            {
                return false;
            }
            else
            {
                byte byte1 = bytes[0];
                byte byte2 = bytes[1];
                if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254)  //判断是否是GB2312
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


       
    }
}