跳至页脚内容
条形码工具

QR Code Library (Developer Guide)

QR代码是一种二维条形码,比传统条形码更小、更便捷。 它通常用于帮助人们查找信息或在外出时与他们喜欢的品牌保持联系。 QR代码由两部分组成 - 左半部分和右半部分。 左半部分通常是黑色像素,而右半部分是白色。使用手机或计算机上的专门QR码扫描器扫描QR码,该扫描器通过使用类似信用卡终端的激光技术以高速捕获编码在代码白色部分的模式中的数据。 QR代码可以用于多个目的,例如显示关于商店、广告、促销、优惠券和新闻文章的信息。 您可能会在书籍封面的前面看到QR代码,更多情况下会在书籍封面后面,或者QR代码可以嵌入在公告栏上。 例如,可以将QR码技术放在学校图书馆中以访问书籍。 QR代码不仅可以帮助学生了解图书馆内容,而且QR代码还可以提供通向无尽额外信息的链接,例如,在线测验供学生使用。

QR码技术有多种使用案例,包括营销和广告活动、电子商务网站、销售点系统、非接触式支付等。 尽管它们已经存在了一段时间,但直到最近,消费者才开始在他们的日常生活中使用它们。 QR代码在广告商和消费者中越来越受欢迎。 它们提供了一种无缝的方式来快速获取有关商品或服务的信息,而无需键入地址或电话号码。 如果您是制作QR码图像的程序员,通过阅读本指南了解这一过程有多容易。

在本文中,您将看到如何使用不同语言如JavaScript、Python和C#编程扫描和创建QR码。 让我们开始吧。

QuaggaJS:JavaScript库

class="content-img-align-center">
class="center-image-wrapper"> Qr Code Library 1 related to QuaggaJS:JavaScript库

QuaggaJS是一个用于生成条形码的JavaScript库。 它利用现代浏览器功能使其可以在任何平台和浏览器上运行。 由于其快速性能、可移植性和兼容性,开发人员广泛使用QuaggaJS。 这个JavaScript库支持各种编码类型,如UPC-A、UPC-E、Code 128、Code 39、Interleaved 2 of 5 (ITF)和EAN 8/13。使用它不需要下载或安装任何额外的依赖项。 QuaggaJS使开发人员只需几行代码就可以在网页上生成复杂的类条形码输出。 它提供了一个高水平的API,该API抽象了底层HTML5画布和JavaScript API的所有复杂性,同时提供了合理的默认设置。

QuaggaJS被广泛用于开发人员和设计人员希望在其设计中整合QR码的数字营销解决方案中。 该库还与许多框架兼容,如React Native、Angular、Vue.js、Ionic 4等。

QuaggaJS库可通过NPM命令安装。 这是一个在项目中使用QuaggaJS库的代码示例:

// Initialize the QuaggaJS library for processing barcodes
Quagga.init({
    inputStream: {
        name: "Live",                // Stream type
        type: "LiveStream",          // Live stream from a webcam
        target: document.querySelector('#yourElement') // Target HTML element for rendering
    },
    decoder: {
        readers: ["code_128_reader"] // Barcode types to decode
    }
}, function(err) {
    if (err) {
        console.log(err);            // Log any initialization errors
        return;
    }
    console.log("Initialization finished. Ready to start");
    Quagga.start();                  // Start processing after initialization
});
// Initialize the QuaggaJS library for processing barcodes
Quagga.init({
    inputStream: {
        name: "Live",                // Stream type
        type: "LiveStream",          // Live stream from a webcam
        target: document.querySelector('#yourElement') // Target HTML element for rendering
    },
    decoder: {
        readers: ["code_128_reader"] // Barcode types to decode
    }
}, function(err) {
    if (err) {
        console.log(err);            // Log any initialization errors
        return;
    }
    console.log("Initialization finished. Ready to start");
    Quagga.start();                  // Start processing after initialization
});
JAVASCRIPT

您可以使用静态图像、实时网络摄像头或文件API扫描条形码。

Segno:Python库

class="content-img-align-center">
class="center-image-wrapper"> Qr Code Library 2 related to Segno:Python库

Segno是一个用于读取、写入和生成Python条形码的库。 使用此库,您可以轻松从头创建条形码图像。 Segno使用NumPy数组高效编码和解码条形码。 Segno的语法类似于Python的海龟模块之一,因此很容易快速掌握。 Segno的设计目标是简单,所以开发人员可以在不具备条形码生成先验知识的情况下使用它。 然而,它也有一个强大的API,使开发人员可以轻松扩展Segno的功能,如果他们想从条形码中获得更多。

Segno的独特功能包括根据条形码数据字段以多种方式生成相同的条形码图像; 支持固定大小、可变大小或两者兼而有之; 支持高分辨率栅格输出; 以及带有工具提示的侧边栏,可以帮助用户生成自己的图像。 以下是要在Python项目中使用Segno库的代码示例:

# Import the Segno library
import segno

# Create a QR code with specified data
qrcode = segno.make('Yellow Submarine')

# Save the generated QR code as a PNG file
qrcode.save('yellow-submarine.png')
# Import the Segno library
import segno

# Create a QR code with specified data
qrcode = segno.make('Yellow Submarine')

# Save the generated QR code as a PNG file
qrcode.save('yellow-submarine.png')
PYTHON

您可以使用PIP安装进行安装。

IronBarcode:C#条形码库

class="content-img-align-center">
class="center-image-wrapper"> Qr Code Library 3 related to IronBarcode:C#条形码库

IronBarcode是支持条形码标准库的代码对象集合。 包括条形码编码器、条形码生成器和编辑器等。 它提供了一个易于使用的API,适用于移动和Web应用程序。 IronBarcode通过提供可以在应用程序中重用的标准化代码对象库来减少手动编码工作量。 这些对象通过自动化特定流程、维护您的应用程序并使其更易于访问,因为它们在平台之间是标准化的,从而减少了开发时间。

由于易用性和高性能,IronBarcode是目前增长最快的库之一。 凭借在iOS、Android和Web浏览器上无需任何更改或修改即可运行的能力,IronBarcode是您下一个项目的绝佳选择。 该库提供了条形码生成的抽象层,同时提供了一套标准实用程序和代码片段以简化开发。 导入此库并传递生成的代码以创建条形码是很有帮助的。 让我们看看代码示例以了解库的运行步骤。

代码示例

using IronBarCode;

//*******     WRITE A QR CODE      *******/

// Create a Barcode in 1 Line of Code
BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode).SaveAsJpeg("QuickStart.jpg");

//*******    READ A QR CODE    *******/

// Read a Barcode in 1 Line of Code. Gets text, numeric codes, binary data, and an image of the barcode
BarcodeResult result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg");

// Check the result of reading the QR code
if (result != null && result.Text == "https://ironsoftware.com/csharp/barcode")
{
    System.Console.WriteLine("Success!"); // Confirm the text matches
}
using IronBarCode;

//*******     WRITE A QR CODE      *******/

// Create a Barcode in 1 Line of Code
BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode).SaveAsJpeg("QuickStart.jpg");

//*******    READ A QR CODE    *******/

// Read a Barcode in 1 Line of Code. Gets text, numeric codes, binary data, and an image of the barcode
BarcodeResult result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg");

// Check the result of reading the QR code
if (result != null && result.Text == "https://ironsoftware.com/csharp/barcode")
{
    System.Console.WriteLine("Success!"); // Confirm the text matches
}
Imports IronBarCode

'*******     WRITE A QR CODE      *******/

' Create a Barcode in 1 Line of Code
BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode).SaveAsJpeg("QuickStart.jpg")

'*******    READ A QR CODE    *******/

' Read a Barcode in 1 Line of Code. Gets text, numeric codes, binary data, and an image of the barcode
Dim result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg")

' Check the result of reading the QR code
If result IsNot Nothing AndAlso result.Text = "https://ironsoftware.com/csharp/barcode" Then
	System.Console.WriteLine("Success!") ' Confirm the text matches
End If
$vbLabelText   $csharpLabel

上述代码用于使用IronBarcode库创建和读取QR码。 使用起来非常简单。 您只需导入库,编写一些代码行,您的条形码就已经准备好使用和读取。

using IronBarCode;

/*** EXPORTING BARCODES AS HTML FILES OR TAGS ***/

// Create a barcode object
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128);

// Save as a stand-alone HTML file with no image assets required
myBarCode.SaveAsHtmlFile("MyBarCode.html");

// Save as a stand-alone HTML image tag which can be served in HTML files, ASPX, or MVC Views. No image assets required, the tag embeds the entire image in its src contents
string imgTag = myBarCode.ToHtmlTag();

// Turn the image into an HTML/CSS Data URI
string dataURI = myBarCode.ToDataUrl();
using IronBarCode;

/*** EXPORTING BARCODES AS HTML FILES OR TAGS ***/

// Create a barcode object
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128);

// Save as a stand-alone HTML file with no image assets required
myBarCode.SaveAsHtmlFile("MyBarCode.html");

// Save as a stand-alone HTML image tag which can be served in HTML files, ASPX, or MVC Views. No image assets required, the tag embeds the entire image in its src contents
string imgTag = myBarCode.ToHtmlTag();

// Turn the image into an HTML/CSS Data URI
string dataURI = myBarCode.ToDataUrl();
Imports IronBarCode

'''* EXPORTING BARCODES AS HTML FILES OR TAGS **

' Create a barcode object
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128)

' Save as a stand-alone HTML file with no image assets required
myBarCode.SaveAsHtmlFile("MyBarCode.html")

' Save as a stand-alone HTML image tag which can be served in HTML files, ASPX, or MVC Views. No image assets required, the tag embeds the entire image in its src contents
Dim imgTag As String = myBarCode.ToHtmlTag()

' Turn the image into an HTML/CSS Data URI
Dim dataURI As String = myBarCode.ToDataUrl()
$vbLabelText   $csharpLabel

IronBarcode支持将QR码导出为HTML文件或标签。 可以在表格标签中导出。 以上代码演示了QR码到HTML的转换。 您可以通过此链接从IronBarcode库网站获取更多详细信息。

许可

IronBarcode在开发阶段是免费的。 您可以获得免费试用用于商业用途或生产级别。 IronBarcode有三个根据开发人员需求的价格计划。 您可以选择最适合您需求的选项。 您还可以以2个Iron软件产品的价格购买5个Iron Software产品套装。 从此链接获取更多信息。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。