如何自訂並向 QR 碼添加標誌

Hairil related to 如何自訂並向 QR 碼添加標誌
海里海西米·賓·奧馬
2023年3月29日
已更新 2024年12月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

QR 碼因其更高的數據容量和掃描便利性而取代傳統條碼,變得越來越流行。 他們在市場行銷中特別受到重視,因為他們的可定制性,包括添加標誌、更改顏色和整合其他品牌元素的選項。

為了滿足這個需求,IronBarcode提供了一系列自訂QR碼的功能。 用戶可以創建帶有標誌的 QR 碼,更改配色方案並添加註釋。 這些功能由IronDrawing提供支持,這是一個免費且開源的庫。

開始使用 IronBarcode

立即在您的專案中使用IronBarcode,並享受免費試用。

第一步:
green arrow pointer




使用徽標創建 QR 碼示例

在生成 QR 碼時,需要一個 QRCodeLogo 物件來嵌入商標圖像。 CreateQrCodeWithLogo 方法也用於生成帶有標誌的 QR 碼。

:path=/static-assets/barcode/content-code-examples/how-to/customize-qr-code-style-logo.cs
using IronBarCode;
using IronSoftware.Drawing;

AnyBitmap qrlogo = AnyBitmap.FromFile("ironbarcode_top.webp");

QRCodeLogo logo = new QRCodeLogo(qrlogo, 0, 0, 20f);

GeneratedBarcode QrCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250);

QrCodeWithLogo.SaveAsPng("QrCodeWLogo2.png");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrlogo As AnyBitmap = AnyBitmap.FromFile("ironbarcode_top.webp")

Private logo As New QRCodeLogo(qrlogo, 0, 0, 20F)

Private QrCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250)

QrCodeWithLogo.SaveAsPng("QrCodeWLogo2.png")
$vbLabelText   $csharpLabel
帶有標誌的 QR Code

讓我們檢查上述代碼生成的輸出 QR 碼。 我們可以看到QR碼中央帶有一個標誌,邊緣是圓角的。

若要自訂標誌,您需要在創建新的QRCodeLogo對象時填寫某些欄位。 以下是所需欄位的說明:

  • 導入圖像:您可以通過多種方式導入圖像,例如從AnyBitmapStreamByte Array、相對文件路徑URI
  • 影像尺寸:請指定標誌影像的所需寬度和高度,單位為像素。 如果圖像過大,導致 QR 碼無法讀取,將會拋出一個異常。 使用值 0 來自動確定最大可行尺寸。
  • 圖片角落:設定標誌圖片圓角的半徑。 使用預設值 0 來實現方形角落。

    最後,要匯出所生成的QR碼,只需調用保存方法。 您有多種匯出選項,包括圖像文件HTMLPDF

更改 QR 碼顏色的示例

除了能夠在您的QR碼上添加標誌之外,IronBarcode還允許用戶通過更改其顏色來進一步自定義他們的QR碼。 使用我們的IronDrawing庫,使用者可以輕鬆地使用 RGB 值或十六進位色碼定義他們自己的顏色,並將其應用於 QR 碼。 讓我們看看演示此功能的代碼片段以及從運行此代碼獲得的結果 QR 碼。

:path=/static-assets/barcode/content-code-examples/how-to/customize-qr-code-style-logo-color.cs
using IronBarCode;
using IronSoftware.Drawing;

AnyBitmap qrlogo = AnyBitmap.FromFile("ironbarcode_top.webp");

QRCodeLogo logo = new QRCodeLogo(qrlogo, 0, 0, 20f);

IronSoftware.Drawing.Color ColorFromRgb = new IronSoftware.Drawing.Color(51, 51, 153);

GeneratedBarcode QrCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250);
GeneratedBarcode QrCodeWithLogoAndColor = QrCodeWithLogo.ChangeBarCodeColor(ColorFromRgb);
QrCodeWithLogoAndColor.SaveAsPng("ColorQrCodeWithLogo.png");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrlogo As AnyBitmap = AnyBitmap.FromFile("ironbarcode_top.webp")

Private logo As New QRCodeLogo(qrlogo, 0, 0, 20F)

Private ColorFromRgb As New IronSoftware.Drawing.Color(51, 51, 153)

Private QrCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250)
Private QrCodeWithLogoAndColor As GeneratedBarcode = QrCodeWithLogo.ChangeBarCodeColor(ColorFromRgb)
QrCodeWithLogoAndColor.SaveAsPng("ColorQrCodeWithLogo.png")
$vbLabelText   $csharpLabel
具有自定義標誌和顏色的 QR 碼

上面的代碼片段擴展了之前創建帶有標誌的QR碼的示例。 它演示了如何使用ChangeBarCodeColor方法更改 QR 碼顏色,此方法接受一個IronSoftware.Drawing.Color物件作為輸入。 您可以使用 RGB 值、十六進位代碼或預定義的枚舉來創建此對象。 訪問我們的「Create Color」程式碼範例以了解更多資訊。

添加 QR 碼註解示例

自定義或設計 QR 碼的另一個重要方面是在 QR 碼圖像內添加註釋。 這些註解可以是條碼值本身,或者是用於促銷或市場營銷的自定義文字。

現在,讓我們檢查以下代碼片段所實現的方法以及由此生成的 QR 碼圖像。

:path=/static-assets/barcode/content-code-examples/how-to/customize-qr-code-style-logo-color-annotation.cs
using IronBarCode;
using IronSoftware.Drawing;

AnyBitmap qrlogo = AnyBitmap.FromFile("ironbarcode_top.webp");

QRCodeLogo logo = new QRCodeLogo(qrlogo, 0, 0, 20f);

Color colorForBarcode = new Color(51, 51, 153); // color from RGB
Color annotationAboveBarcodeColor = new Color("#176feb");  // color from Hex
Font annotationAboveBarcodeFont = new Font("Candara", FontStyle.Bold, 15);
Color barcodeValueBelowBarcodeColor = new Color("#6e53bb");
Font barcodeValueBelowBarcodeFont = new Font("Cambria", FontStyle.Regular, 15);

GeneratedBarcode qrCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250);
GeneratedBarcode qrCodeWithLogoAndColor = qrCodeWithLogo.ChangeBarCodeColor(colorForBarcode);
GeneratedBarcode qrCodeWithAnnotation = qrCodeWithLogoAndColor.AddAnnotationTextAboveBarcode("IronBarcodeRocks!", annotationAboveBarcodeFont, annotationAboveBarcodeColor, 2).AddBarcodeValueTextBelowBarcode(barcodeValueBelowBarcodeFont, barcodeValueBelowBarcodeColor, 2);
qrCodeWithAnnotation.SaveAsPng("QRCodeWithAnnotation.png");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrlogo As AnyBitmap = AnyBitmap.FromFile("ironbarcode_top.webp")

Private logo As New QRCodeLogo(qrlogo, 0, 0, 20F)

Private colorForBarcode As New Color(51, 51, 153) ' color from RGB
Private annotationAboveBarcodeColor As New Color("#176feb") ' color from Hex
Private annotationAboveBarcodeFont As New Font("Candara", FontStyle.Bold, 15)
Private barcodeValueBelowBarcodeColor As New Color("#6e53bb")
Private barcodeValueBelowBarcodeFont As New Font("Cambria", FontStyle.Regular, 15)

Private qrCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", logo, 250)
Private qrCodeWithLogoAndColor As GeneratedBarcode = qrCodeWithLogo.ChangeBarCodeColor(colorForBarcode)
Private qrCodeWithAnnotation As GeneratedBarcode = qrCodeWithLogoAndColor.AddAnnotationTextAboveBarcode("IronBarcodeRocks!", annotationAboveBarcodeFont, annotationAboveBarcodeColor, 2).AddBarcodeValueTextBelowBarcode(barcodeValueBelowBarcodeFont, barcodeValueBelowBarcodeColor, 2)
qrCodeWithAnnotation.SaveAsPng("QRCodeWithAnnotation.png")
$vbLabelText   $csharpLabel
带注释的 QR 码

IronBarcode提供方法以設定註解的位置(在QR碼上方或下方)以及其字體家族和顏色。 以下是這個功能可用的方法:

  • AddAnnotationTextAboveBarcode:在QR碼上方添加註釋文字。
  • AddAnnotationTextBelowBarcode:在 QR 碼下方新增註釋文字。
  • AddBarcodeValueTextAboveBarcode:在 QR 碼上方添加條碼值文字。
  • AddBarcodeValueTextBelowBarcode:在QR碼下方添加條碼值文本。

自訂註解和條碼值

以上提到的四種方法都接受自訂字型作為IronSoftware.Drawing.Font物件、顏色作為IronSoftware.Drawing.Color物件,並使用整數來指定文字的上下間距,以像素為單位。 請注意,這些參數是可選的; 如果未指定,將使用預設的字型、顏色和間距。

簡而言之,IronBarcode 是創建和自定義 QR 碼的理想工具。 除了用於自定義的直接方法外,IronBarcode 也使用了我們自己的 IronDrawing 作為與圖像處理相關的輔助庫,這被認為比依賴其他外部庫更穩定。

Hairil related to 自訂註解和條碼值
海里海西米·賓·奧馬
軟體工程師
和所有優秀的工程師一樣,Hairil 是一位熱衷學習的人。他正在精進自己對 C#、Python 和 Java 的知識,利用這些知識為 Iron Software 團隊的成員創造價值。Hairil 從馬來西亞的馬來西亞工藝大學加入了 Iron Software 團隊,他在那裡獲得了化學和過程工程學士學位。