撰寫 1-BPP BarCode 影像

1bpp 影像是一種單色影像,只能顯示兩種顏色,通常是黑色和白色。影像中的每個像素都由一個位元表示,其中"0"可以代表黑色,"1"可以代表白色,反之亦然。 這種格式最適合對速度和準確性要求很高的場景,並且是為機器讀取而設計的。 在這個程式碼範例中,我們將示範如何將我們建立的條碼轉換為 1bpp 影像,以便在掃描時獲得最大的對比度和可靠性。

將條碼轉換為 1BPP 條碼影像的 5 步驟指南

  • using IronBarCode;
  • var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
  • myBarcode.SaveAs1BppBitmap("1bppImage.bmp");
  • var byteData = myBarcode.To1BppBinaryData();
  • var anyBitmap = myBarcode.To1BppImage();

程式碼解釋

We first import the IronBarcode library, then we generate a barcode using the BarcodeWriter.CreateBarcode method. 我們向該方法傳遞兩個變數:字串值和條碼類型。 在上面的例子中,它們分別是"12345"和"EAN8"。

After creating the barcode, we can call the save method SaveAs1BppBitmap and pass it a file name. 這樣做會將條碼儲存為 1bpp 位圖,名稱即為已儲存的檔案名稱。

除了直接保存為點陣圖之外,還有其他保存方法。 IronBarcode supports saving the barcode as 1bpp binary data using the To1BppBinaryData method. 轉換為二進位資料後,您可以將變數作為二進位資料傳遞給應用程式的其他部分,或與現有程式碼庫整合。 Furthermore, IronBarcode also supports saving the barcode as a 1-bit-per-pixel (1bpp) image using the To1BppImage method.

準備好開始了嗎?
Nuget 下載 2,002,059 | 版本: 2025.12 剛發表