使用条形码阅读器C#.NET解码非英语文本

时间:2014-09-21 11:47:34

标签: .net barcode spire

你好我有这个简单的例子来生成条形码并使用Spire.Barcode库再次解码它

        BarcodeSettings bcsettings = new BarcodeSettings();
        bcsettings.Data = "مرحباً";
        bcsettings.Type = BarCodeType.DataMatrix;
        BarCodeGenerator bcgen = new BarCodeGenerator(bcsettings);
        System.Drawing.Image bcimg = bcgen.GenerateImage();
        System.Drawing.Bitmap bcbitmap = new System.Drawing.Bitmap(bcimg);
        String bcdata = BarcodeScanner.ScanOne(bcbitmap);
        output.Text += bcdata;

该示例适用于英语,但当我使用阿拉伯语时输出显示如下。

Barcode output for Arabic

有没有解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以在代码中使用以下代码:

private void button1_Click(object sender, EventArgs e)
        {
            BarcodeSettings bcsettings = new BarcodeSettings();

            bcsettings.Data = GetEncodingText("مرحباً");
            bcsettings.Data2D =GetEncodingText( "مرحباً");

            bcsettings.Type = BarCodeType.DataMatrix; 
            BarCodeGenerator bcgen = new BarCodeGenerator(bcsettings);
            System.Drawing.Image bcimg = bcgen.GenerateImage();
            System.Drawing.Bitmap bcbitmap = new System.Drawing.Bitmap(bcimg);
            String bcdata = BarcodeScanner.ScanOne(bcbitmap);
            output.Text += GetDecodingText(bcdata);

        }
        public string GetEncodingText(string unicodeStr)
        {
            string encodingResult = "";
            byte[] byteArray = Encoding.Unicode.GetBytes(unicodeStr);
            foreach (byte b in byteArray)
                encodingResult += (char)b;
            return encodingResult;
        }
        public string GetDecodingText(string unicodeStr)
        {
            string decodingResult = "";
            List<byte> bytes = new List<byte>();
            foreach (char c in unicodeStr)
                bytes.Add((byte)c);
            decodingResult = Encoding.Unicode.GetString(bytes.ToArray());
            return decodingResult;
        }