Aspose功能演示:使用C#以UTF-8編碼生成條形碼

在某些情況下,必須處理非英語字符。例如,當您使用阿拉伯語,拉丁語,希臘語或類似語言時。在這種情況下,可能需要將字符編碼爲Unicode標準,即UTF-8。因此,本文介紹瞭如何使用C#中的UTF-8編碼生成和讀取條形碼。

  • 在C#中使用UTF-8編碼生成條形碼
  • 使用C#以UTF-8編碼讀取條形碼

目前,Aspose.Barcode是強大的C#API,用於條形碼的生成和識別。使用API,可以使用多種條形碼符號。此外,API支持使用UTF-8編碼生成條形碼。,還沒使用過的朋友可以獲取最新版。


使用C#中的UTF-8編碼生成條形碼

以下是使用UTF-8編碼生成條形碼的步驟。

  • 首先,創建實例BarcodeGenerator類,並使用指定的條碼類型EncodeTypes。
  • 使用BarcodeGenerator.CodeText屬性設置條形碼的文本。
  • 使用BarcodeGenerator.Parameters.Barcode.QR.CodeTextEncoding屬性設置UTF-8文本編碼(根據您在BarcodeGenerator構造函數中指定的條形碼類型替換QR )。
  • 使用BarcodeGenerator.GenerateBarCodeImage()方法生成條形碼,並將返回的圖像保存到Bitmap對象中。
  • 最後,使用Bitmap.Save(String)方法將條形碼圖像保存爲文件。

以下代碼示例顯示瞭如何使用C#中的UTF-8編碼生成條形碼。

// Create a barcode generator
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Pdf417);

// Set barcode text
generator.CodeText = "منحة";

// Set resolution
generator.Parameters.Resolution = 400;

// Set encoding type
generator.Parameters.Barcode.Pdf417.CodeTextEncoding = Encoding.UTF8;

// Generate barcode
Bitmap imgBarcode = generator.GenerateBarCodeImage();

// Save barcode image
imgBarcode.Save("generate-barcode.png");

使用C#讀取UTF-8編碼條形碼

以下是使用C#識別UTF-8編碼條形碼的步驟。

  • 使用BarCodeReader類加載條形碼圖像。
  • 循環遍歷BarCodeReader.ReadBarCodes()方法返回的每個BarCodeResult。
  • 創建Encoding類的對象,並將編碼設置爲UTF-8。
  • 從BarCodeResult對象返回的字節中獲取char數組。
  • 從字符構建Unicode字符串以獲取條形碼文本。
  • 最後,打印檢索到的條形碼文本。

下面的代碼示例演示如何使用C#中的UTF-8編碼識別條形碼。

// Recognize the above barcode
using (BarCodeReader reader = new BarCodeReader("generate-barcode.png"))
{
    // Read barcodes
    foreach (BarCodeResult result in reader.ReadBarCodes())
    {
        // Set encoding
        Encoding unicode = Encoding.UTF8;

        // Get the characters array from the bytes
        char[] unicodeChars = new char[unicode.GetCharCount(result.CodeBytes, 0, result.CodeBytes.Length)];
        unicode.GetChars(result.CodeBytes, 0, result.CodeBytes.Length, unicodeChars, 0);

        // Build unicode string
        string strCodeText = new string(unicodeChars);
        Console.WriteLine(strCodeText);
    }
}

如果您有任何疑問或需求,請隨時加入Aspose技術交流羣(761297826),我們很高興爲您提供查詢和諮詢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章