.netbarcodespire

Decode Non-English text with barcode reader C#.NET


hello I have this simple example of generating a bar code and decoding it again using Spire.Barcode library

        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;

the example works very well with English but the output shows like this when I use Arabic.

Barcode output for Arabic

Is there anyway to fix this?

Thanks in advance.


Solution

  • you can use below code to your project:

    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;
            }