跳至頁尾內容
條碼工具

QR碼庫(開發人員指南)

QR碼是一種2D條碼,比傳統條碼更小且更易接觸。 它經常用來幫助人們找到資訊或隨時與他們喜愛的品牌連接。 QR碼由左半部和右半部兩部分組成。 左半部通常有黑色像素,而右半部是白色。QR碼使用專有QR碼掃描器在手機或電腦上進行掃描,該掃描器使用雷射技術以高速捕捉編碼進白色部分樣式中的資料,就像用於信用卡終端一樣。 QR碼可以用於很多目的,例如顯示有關商店、廣告、促銷活動、優惠券和新聞文章的資訊。 您可能會看到QR碼印在書籍封面前面,但更常見的是在書籍封面背面,或者QR碼可能會貼在公告欄上。 例如,QR碼技術可以放置在學校圖書館中以存取書籍。 QR碼不僅可以幫助學生了解圖書館內容,還可以提供無窮無盡的附加資訊的連結,例如,給學生的線上測驗。

QR碼技術的使用案例有很多,包括市場營銷和廣告活動、電子商務網站、銷售點系統、非接觸式付款等。 雖然它們已經存在相當一段時間,但最近消費者才開始在他們的日常生活中使用它們。 QR碼在廣告商和消費者中越來越受歡迎。 它們提供了一種無縫的方式來快速獲得有關商品或服務的資訊,而無需輸入地址或電話號碼。 如果您是程式設計師並製作QR碼圖像,閱讀本指南以了解它有多麼容易。

在本文中,您將看到如何使用JavaScript、Python和C#程式化掃描和建立QR碼。 讓我們開始吧。

QuaggaJS:JavaScript程式庫

Qr Code Library 1 related to QuaggaJS:JavaScript程式庫

QuaggaJS是一個用於生成條碼的JavaScript程式庫。 它利用現代瀏覽器功能來使其在任何平台和瀏覽器上運行。 開發人員廣泛使用QuaggaJS,因為其性能快、便攜性強且相容性好。 這個JavaScript程式庫支持各種編碼型別,如UPC-A、UPC-E、Code 128、Code 39、交錯2 of 5(ITF)和EAN 8/13。使用它無需下載或安裝任何額外依賴項。 QuaggaJS使開發人員能夠在網頁上生成類似於複雜條碼的輸出,只需幾行程式碼。 它提供了一個高級API,抽象了底層HTML5畫布和JavaScript API的所有複雜性,而提供合理的預設值。

QuaggaJS被廣泛用於開發者和設計師尋求建立數字市場解決方案,其中將QR碼融入設計中。 該程式庫還可與許多框架相容,如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來掃描條碼。

Segno:Python程式庫

Qr Code Library 2 related to Segno: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由於其易用性和高效能,是目前增長最快的程式庫之一。 由於它無需進行更改或修改即可在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 Software產品的價格購買5個Iron Software產品的套件。 從此連結獲取更多資訊。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話