跳過到頁腳內容
條碼工具

QR碼庫(開發人員指南)

二維碼是一種二維條碼,比傳統條碼更小巧、更容易使用。 它常用於幫助人們隨時隨地尋找資訊或與他們喜愛的品牌聯繫。 二維碼由兩部分組成-左半部和右半部。 二維碼的左半部通常為黑色像素,右半部為白色。掃描二維碼時,需要使用手機或電腦上的專用二維碼掃描器。此掃描器利用雷射技術高速掃描二維碼白色部分中的圖案,取得編碼數據,類似信用卡終端使用的掃描器。 二維碼可用於多種用途,例如展示商店資訊、廣告、促銷、優惠券和新聞文章。 你可能會在書的封面正面看到印有二維碼,但更常見的是在書的封底,或者二維碼可能被印在公告板上。 例如,可以在學校圖書館放置二維碼技術,用於存取書籍。 二維碼不僅可以幫助學生了解圖書館的內容,還可以提供指向各種附加資訊的鏈接,例如學生的線上測驗。

QR Code 技術有多種使用案例,包括行銷和廣告活動,電子商務網站,銷售點系統,非接觸式支付等。 雖然這些技術已經存在很久了,但直到最近,消費者才開始在日常生活中使用這些技術。 二維碼在廣告商和消費者中越來越受歡迎。 它們提供了一種便捷的方式,無需輸入地址或電話號碼即可快速獲取有關商品或服務的資訊。 如果你是製作二維碼圖像的程式設計師,請閱讀本指南,了解製作二維碼圖像有多麼簡單。

本文將介紹如何使用 JavaScript、Python 和 C# 等不同語言以程式方式掃描和建立二維碼。 我們開始吧。

QuaggaJS:JavaScript 函式庫

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 讓開發人員只需幾行程式碼即可在 Web 上輕鬆產生複雜的條碼狀輸出。 它提供了一個高級 API,抽象化了底層 HTML5 canvas 和 JavaScript API 的所有複雜性,同時提供了合理的預設值。

QuaggaJS 已被眾多開發者和設計師廣泛用於創建將二維碼融入其設計中的數位行銷解決方案。 該函式庫也相容於 React Native、Angular、Vue.js、Ionic 4 等多種框架。

可以使用 NPM 指令安裝 QuaggaJS 函式庫。 以下是專案中使用 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 掃描條碼。

標誌:Python 庫

Qr Code Library 2 related to 標誌:Python 庫

Segno 是一個用於在 Python 中讀取、寫入和產生條碼的函式庫。 有了這個庫,你可以輕鬆地從零開始創建條碼圖像。 Segno 使用 NumPy 陣列有效率地對條碼進行編碼和解碼。 Segno 的語法與 Python 的 turtle 模組之一類似,因此應該很容易快速掌握。 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# 條碼庫

Qr Code Library 3 related to IronBarcode:C# 條碼庫

IronBarcode 是一組支援條碼標準函式庫的程式碼物件。 這包括條碼編碼器、條碼產生器和編輯器等。 它提供了一個易於使用的 API,適用於行動和 Web 應用程式。 IronBarcode 透過提供一個標準化的程式碼物件庫來減少手動編碼工作量,這些程式碼物件可以在整個應用程式中重複使用。 這些物件透過自動化特定流程來縮短開發時間,並維護您的應用程序,使其更易於訪問,因為它們是跨平台標準化的。

IronBarcode 是目前成長最快的程式庫之一,因為它易於使用且效能卓越。 IronBarcode 無需任何更改或修改即可在 iOS、Android 和 Web 瀏覽器上運行,是您下一個專案的絕佳選擇。 該函式庫為條碼產生提供了一個抽象層,同時提供了一組標準實用程式和程式碼片段,以簡化開發。 導入此庫並傳遞生成的程式碼有助於建立條碼。 讓我們來看一些程式碼範例來了解庫的運行過程。

程式碼範例

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 庫建立和讀取二維碼。 它使用起來很簡單。 您只需導入庫,編寫幾行程式碼,您的條碼即可使用和讀取。

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 支援將二維碼匯出為 HTML 檔案或標籤。 它可以導出為表格標籤。 以上程式碼示範如何將二維碼轉換為 HTML。 您可以點擊此連結造訪 IronBarcode 庫網站,以了解更多詳情。

授權

IronBarcode 在開發階段是免費的。 您可以獲得商業用途或生產等級的免費試用版。 IronBarcode 依據開發者的需求提供三種定價方案。 您可以選擇最符合您需求的方案。 您也可以用購買 2 款 Iron Software 產品的價格購買 5 款 Iron Software 產品。 點擊此連結了解更多。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。