如何为 QR 代码定制和添加徽标

This article was translated from English: Does it need improvement?
Translated
View the article in English

海瑞尔 哈西米 本 奥马尔

与传统条形码相比,二维码因其数据容量大、扫描方便而越来越受欢迎。在市场营销中,二维码因其可定制性(包括添加徽标、更改颜色和融入其他品牌元素)而尤其受到重视。

为满足这一需求,IronBarcode 提供了一套定制 QR 码的功能。用户可以创建带有徽标的 QR 码、更改配色方案并添加注释。这些功能由 IronDrawing是一个免费的开源库。



适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronBarcodeNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。

适用于的C# NuGet库 nuget.org/packages/BarCode/
Install-Package BarCode

考虑安装 IronBarcode DLL 直接。下载并手动安装到您的项目或GAC表单中: IronBarCode.zip

手动安装到你的项目中

下载DLL

创建带有徽标的 QR 代码示例

生成二维码时,需要一个 QRCodeLogo 对象来嵌入徽标图像。也可使用 "CreateQrCodeWithLogo "方法生成带有徽标的二维码。

: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")
VB   C#
带徽标的 QR 码

让我们来看看上述代码生成的 QR 码输出结果。我们可以看到,QR 码的中心是一个徽标,边缘呈圆形。

要自定义徽标,需要在创建新的 QRCodeLogo 对象时填写某些字段。下面是对必填字段的解释:

  • 导入图像:您可以通过多种方式导入图像,例如从任意位图字节数组、相对文件路径URI导入。
  • 图像尺寸:以像素为单位指定徽标图像的宽度和高度。如果图像太大,二维码无法读取,则会出现异常。使用值 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")
VB   C#
带有自定义徽标和颜色的 QR 码

上面的代码片段扩展了之前关于创建带有徽标的 QR 代码的示例。它演示了如何使用 "ChangeBarCodeColor "方法更改二维码颜色,该方法将一个IronSoftware.Drawing.Color对象作为输入。您可以使用 RGB 值、十六进制代码或预定义枚举创建该对象。访问我们的 "创建颜色" 代码示例,了解更多信息。

添加 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")
VB   C#
带注释的 QR 码

IronBarcode 提供设置注释位置的方法 (二维码上方或下方) 以及字体家族和颜色。以下是该功能的可用方法:

  • AddAnnotationTextAboveBarcode`:在***二维码上方添加注释文本。
  • AddAnnotationTextBelowBarcode`:在二维码下方添加注释文本。
  • AddBarcodeValueTextAboveBarcode:在 QR 码的**上方添加条码值文本。
  • AddBarcodeValueTextBelowBarcode:在 QR 码的**下方添加条码值文本。

自定义注释和条形码值

上述四种方法都接受自定义字体(IronSoftware.Drawing.Font 对象)、颜色(IronSoftware.Drawing.Color 对象)和一个整数(以像素为单位指定文本的上下间距)。请注意,这些参数都是可选参数;如果未指定,将使用默认字体、颜色和间距。

简而言之,IronBarcode 是创建和定制 QR 码的理想工具。除了用于定制的直接方法外,IronBarcode 还使用了我们自己的 IronDrawing 作为辅助库,用于处理与图像有关的任何内容,这比依赖其他外部库要更加稳定。

海瑞尔 哈西米 本 奥马尔

软件工程师

像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。