1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

QR Code を 生成する方法

Last updated at Posted at 2020-05-15

QR コードを印刷したいときの対処方法

簡単な順番に・・

1)WEB サービスを使う
2)エクセルを使う
3)プログラミングする (.NET Core)

WEB サービスを使う方法

を開いて 作成した QR コードを入力して Generate ボタンをクリック

image.png

エクセルを使う方法

エクセルで ActiveX コンポーネント
「Microsoft BarCcde Control 16.0」
を追加して その プロパティーValue に 生成したい QR コードを設定する。

手順

「ファイル」メニューの「オプション」から「リボンのユーザ設定」で「開発」のタブのチェックを入れる。

image.png

次に 「開発」メニューの「挿入」から「ActiveX コントロール」で「コントロールの選択」の中にある「Microsoft BarCcde Control 16.0」を選択

image.png

そのバーコードコントロールを右クリックして
「Microsoft BarCard Control 16.0 オブジェクト」の「プロパティー」の「スタイル」から「11-QRコード」を選択。
「プロパティー」の中の「Value」に表示したい値を設定します。

image.png

プログラミングする方法

.NET Core が簡単。 Windows でも Linux でもできます。
インストール
.NET Core
Visual Studio Code

コマンドプロンプトから

mkdir barcord
cd barcord
dotnet new console
dotnet add package QRCoder
code .

Visual Studio Code の画面で 下記コードを入力して

using System;
using QRCoder;
using System.IO;

namespace barcord
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = "Hello World!";

            SaveToBitmap(msg , "hello.bmp");
            SaveToPng(msg, "hello.png");
#if NET6_0_WINDOWS
            SaveToSVG(msg, "hello.svg");
#endif
        }

        static void SaveToBitmap(string msg,string imgFileName)
        {
            var bytes = BitmapByteQRCodeHelper.GetQRCode(msg, QRCodeGenerator.ECCLevel.M, 10);
            using(FileStream fs = new FileStream(imgFileName, FileMode.Create)) {
                fs.Write(bytes);
                fs.Flush();
                fs.Close();
            }
        }

        static void SaveToPng(String msg,string imgFileName)
        {
            var bytes = PngByteQRCodeHelper.GetQRCode(msg, 10, new byte[]{ 0,0,0}, new byte[]{255,255,255}, QRCodeGenerator.ECCLevel.M);
            using(FileStream fs = new FileStream(imgFileName, FileMode.Create)) {
                fs.Write(bytes);
                fs.Flush();
                fs.Close();
            }
        }
#if NET6_0_WINDOWS
        static void SaveToSVG(String msg,string imgFileName)
        {
            String svgString = SvgQRCodeHelper.GetQRCode(msg,10,"#000000","#FFFFFF", QRCodeGenerator.ECCLevel.M);
            using(StreamWriter sw = new StreamWriter(imgFileName)) {
                sw.WriteLine(svgString);
                sw.Flush();
                sw.Close();
            }
        }
#endif
    }
}

プログラムを実行

dotnet run

Windows でも Linux(WSL Ubunto 18.04 )でも QRコードを作成する事ができました。

※ 2022.04.13 現在 .NET Core 6.0 では Windows でのみ SVG形式の画像が作成されます。
Linux で SVG形式の QR コードを作成できない 詳しい理由は 開発元のページ
https://github.com/codebude/QRCoder/pull/358
で確認できます。
近い将来この問題は解決するかもしれません。

https://github.com/codebude/QRCoder/issues/366
https://github.com/codebude/QRCoder/issues/315

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?