Rust QRコードライブラリ

RustでQRコードを生成

Rustのqrcode crateは高速でメモリ安全なQRコード生成を提供します。高パフォーマンスアプリケーション、WebAssembly、システムレベルのツールに最適です。

インストール

qrcode crateをCargo.tomlに追加します。

Cargo.toml
[dependencies]
qrcode = "0.14"
image = "0.25"  # For PNG output

RustでQRコードを生成

qrcode crateを使用したコード例。

QR Code as SVG
use qrcode::QrCode;
use qrcode::render::svg;

fn main() {
    let code = QrCode::new("https://qrcode.fun").unwrap();
    let svg = code.render::<svg::Color>()
        .min_dimensions(200, 200)
        .build();

    std::fs::write("qrcode.svg", &svg).unwrap();
    println!("SVG QR code saved!");
}
QR Code as PNG
use qrcode::QrCode;
use image::Luma;

fn main() {
    let code = QrCode::new("https://qrcode.fun").unwrap();
    let image = code.render::<Luma<u8>>()
        .dark_color(Luma([26u8]))
        .light_color(Luma([255u8]))
        .quiet_zone(true)
        .min_dimensions(300, 300)
        .build();

    image.save("qrcode.png").unwrap();
    println!("PNG QR code saved!");
}
QRCode.fun API

RustでAPIを使ってQRコードを生成

reqwestを使用してRustからQRCode.fun APIを呼び出し、スタイル付きQRコードを生成します。

Rust API連携
use reqwest;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::Client::new();
    let response = client
        .post("https://qrcode.fun/api/generate-qr-styled")
        .json(&json!({
            "data": "https://qrcode.fun",
            "width": 300,
            "height": 300,
            "type": "png",
            "margin": 10,
            "dotsOptions": { "color": "#1A2B3C", "type": "rounded" },
            "cornersSquareOptions": { "color": "#8564C3", "type": "extra-rounded" },
            "backgroundOptions": { "color": "#FFFFFF" }
        }))
        .send()
        .await?;

    let result: serde_json::Value = response.json().await?;
    println!("{}", &result["data"].as_str().unwrap()[..50]);
    Ok(())
}

QRコードライブプレビュー

今すぐRustでQRコードを生成してみましょう。

QRプレビュー

ネイティブライブラリ vs API

qrcode crateとQRCode.fun APIの比較。

機能qrcode CrateQRCode.fun API
セットアップの複雑さcargo add + PNG用image cratereqwest経由の単一HTTPリクエスト
カスタマイズimage crate経由でカラーフルスタイリング:カラー、シェイプ、ロゴ
オフラインサポートはいインターネット接続が必要
メンテナンスcargo update常に最新
出力形式SVG, PNG(image crate使用), ターミナルPNG, SVG

Rust QRコードの使用例

RustアプリケーションでのQRコードの一般的なシナリオ。

WebAssembly

QRコード生成をWASMにコンパイルし、JavaScriptライブラリなしでブラウザサイドの超高速QRコード作成を実現します。

CLIツール

ターミナル表示、ファイル出力、クリップボード統合用のQRコードを生成するコマンドラインユーティリティを構築します。

高パフォーマンスサーバー

ActixやAxum Webサーバーで最小限のメモリ割り当てと最大スループットでQRコードを生成します。

組み込みシステム

リソースが制限されたデバイスでQRコード生成を実行し、Rustのゼロコスト抽象化の優位性を活かします。

よくある質問

RustでのQRコード生成に関する一般的な質問。

qrcode crateが最も人気のある選択肢です。QRコードをSVG文字列またはマトリックスとして生成し、image crateでレンダリングできます。

RustでQRコード生成を始めましょう

無料のジェネレーターを使用するか、APIをRustアプリケーションに統合しましょう。