・2019/10/14
Visual Studio 2013の .NET C#で UTF-8対応 QRコード生成・読取アプリを作成する方法、ZXing.Net QR Codeライブラリを使用
(.NETで UTF-8対応 QRコード作成・解読アプリを作成、ILMergeで dllを exeに合体する方法)
Tags: [Windows], [無人インストール]
● Microsoft Visual Studio 2013の C# .NETで UTF-8対応 QRコード生成・読取アプリを作成する方法
Visual Studio 2013を使用して UTF-8対応 QRコード生成アプリを作成します。
QRコード = デンソーウェーブが開発した 2次元バーコード
QRコード開発ストーリー
1994年、それまでのコード読み取りの概念を大きく変えた出来事があった。QRコードの登場だ。
● UTF-8対応 QRコード生成アプリを作成する手段
ここでは QRコードとして無料で使用でき、かつオフライン環境で動作可能な ZXing製の ZXing.Netライブラリを使用します。
● ZXing.Netライブラリのインストール方法
micjahn/ZXing.Net
ZXing.Net 0.16.5
ZXing.Net is a port of ZXing, an open-source, multi-format 1D/2D barcode image processing library originally implemented in Java.
It is compatible with .Net 2.0, .Net 3.5, .Net 4.0, .Net 4.5, .Net 4.6, .Net 4.7, .Net 4.8,,,
APACHE LICENSE, VERSION 2.0
ZXing.Net Version 0.16.5は Visual Studio 2013ではエラーが出ました。
ZXing.Net Version 0.15.0を使用します。
PM> Install-Package ZXing.Net -Version 0.16.5
Install-Package : 'ZXing.Net' にはすでに 'NETStandard.Library' に対して定義された依存関係があります。
発生場所 行:1 文字:1
+ Install-Package ZXing.Net -Version 0.16.5
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
PM> Install-Package ZXing.Net -Version 0.15.0
'ZXing.Net 0.15.0' をインストールしています。
'ZXing.Net 0.15.0' が正常にインストールされました。
'ZXing.Net 0.15.0' を TestQR に追加しています。
'ZXing.Net 0.15.0' が TestQR に正常に追加されました。
● PictureBoxを追加します
とりあえず C#で ZXingの QrCodeライブラリを使用して QrCodeを生成する方法。
using System;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode;
namespace TestQR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BarcodeWriter writer = new BarcodeWriter();
BarcodeFormat format = BarcodeFormat.QR_CODE;
int width = pictureBox1.Width;
int height = pictureBox1.Height;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
CharacterSet = "UTF-8",
ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M,
Height = height,
Width = width,
};
writer.Options = options;
writer.Format = format;
string contents = "あいう漢字汉语\n123ABC";
pictureBox1.Image = writer.Write(contents);
}
}
}
・QRコード生成の例1 "あいう漢字汉语(改行)123ABC"

● PictureBox、TextBoxを追加して、TextBoxに TextChangedイベントを追加します
・Visual Studio C# Formで TextBoxに TextChangedイベントを追加の方法

TextBoxを追加して QRコードを自由に作成できる様にします。
using System;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode;
namespace TestQR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
BarcodeWriter writer = new BarcodeWriter();
BarcodeFormat format = BarcodeFormat.QR_CODE;
private void updateQrCode(string contents)
{
int width = pictureBox1.Width;
int height = pictureBox1.Height;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
CharacterSet = "UTF-8",
ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M,
Height = height,
Width = width,
Margin = 2,
};
writer.Options = options;
writer.Format = format;
// string contents = "あいう漢字汉语\n123ABC";
if (contents.Length > 0)
{
pictureBox1.Image = writer.Write(contents);
}
else
{
pictureBox1.Image = null;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string contents = ((TextBox)sender).Text;
updateQrCode(contents);
}
}
}
・QRコード生成の例2 "ほげほげ(改行)日本語漢字(改行)汉语你好"

● Microsoft Visual Studio 2013で UTF-8対応 QRコード読取アプリを作成する方法
Visual Studio 2013を使用して UTF-8対応 QRコード読取アプリを作成します。
● QRコードのデコード(QRコードの読み込み)
using System.Drawing;
using ZXing;
private void decodeImage(String file)
{
// Bitmap image = new Bitmap(@"C:\tmp\hoge.png");
Bitmap image = new Bitmap(file);
ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
ZXing.Result result = reader.Decode(image);
if (result != null)
{
textBox1.Text = result.Text;
textBox2.Text = result.BarcodeFormat.ToString();
}
else
{
textBox1.Text = "";
textBox2.Text = "Error";
}
// 画像を自動で拡縮する
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = image;
}
● Drag and Drop
private void Form1_Load(object sender, EventArgs e)
{
// Drag & Drop
pictureBox1.AllowDrop = true;
}
private String getDragEventArgs(DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return null;
string[] drags = (string[])e.Data.GetData(DataFormats.FileDrop);
if (drags.Length != 1) return null;
string drag = drags[0];
if (!System.IO.File.Exists(drag)) return null;
try
{
Bitmap image = new Bitmap(drag);
if (image == null) return null;
}
catch (Exception ex)
{
return null;
}
return drag;
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
string drag = getDragEventArgs(e);
if (drag == null) return;
e.Effect = DragDropEffects.Copy;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string drag = getDragEventArgs(e);
if (drag == null) return;
decodeImage(drag);
}
● Microsoft Visual Studio 2013で画面の指定範囲をキャプチャして QRデコードする
Windows + Shift + Sキーの押下をエミュレートして、Windows OSの画面キャプチャ機能を利用します。
using System.Runtime.InteropServices;
{
// Win + Shift + S
win32api.keybd_event((byte)0x5B, 0, 0, (UIntPtr)0);
win32api.keybd_event((byte)0x10, 0, 0, (UIntPtr)0);
win32api.keybd_event((byte)'S', 0, 0, (UIntPtr)0);
// KEYEVENTF_KEYUP
win32api.keybd_event((byte)'S', 0, 2, (UIntPtr)0);
win32api.keybd_event((byte)0x10, 0, 2, (UIntPtr)0);
win32api.keybd_event((byte)0x5B, 0, 2, (UIntPtr)0);
// クリップボードに画像が反映されるまで少し待つ
System.Threading.Thread.Sleep(3500);
// クリップボードの内容を取得する
if (Clipboard.ContainsImage())
{
Image image = Clipboard.GetImage();
if (image != null)
{
decodeImage(image as Bitmap);
// 明示的に GCを行なう事でメモリを解放する
GC.Collect();
}
}
}
public class win32api
{
[DllImport("user32.dll")]
public static extern uint keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
}
●その他の QRコードライブラリ .NET QRCode Library
Open Source QRCode Library
.NET QRCode Library - From ideas to action
.NET QRCode Library
● C#の実行ファイルに DLLファイルを結合して1つのファイルにする方法
ILMerge
ILMerge
ILMerge is a utility that merges multiple .NET assemblies into a single assembly. It is available as open source and also as a NuGet package.
dotnet/ILMerge
ilmerge 3.0.29 2019/04
Install-Package ilmerge -Version 3.0.29
● ILMergeの使い方
Visual Studioで Releaseをビルドした時に自動的に ILMergeする様にする方法。
Package-Managerで
Install-Package ilmerge -Version 3.0.29
を実行する。
.csprojファイルを開き、下記を追加する。
Install-Package ilmergeで自動的に追加される
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
</PropertyGroup>
<Error Condition="!Exists('..\packages\ilmerge.3.0.29\build\ILMerge.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\ilmerge.3.0.29\build\ILMerge.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
下記を追加する(Exec Commandの ilmerge.3.0.29の所はバージョン毎に書き換えます)
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
<CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
<Output ItemName="AssembliesToMerge" TaskParameter="Include" />
</CreateItem>
<PropertyGroup>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<Exec Command=""$(SolutionDir)packages\ilmerge.3.0.29\tools\net452\ILMerge.exe" /out:@(MainAssembly) "@(IntermediateAssembly)" @(AssembliesToMerge->'"%(FullPath)"', ' ')" />
<Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>
</Project>
"Release"ビルド時にこんな感じで自動的に ILMergeを実行します。
"HogeProject\packages\ilmerge.3.0.29\tools\net452\ILMerge.exe" /out:bin\Release\TestQR.exe "obj\Release\TestQR.exe" "HogeProject\packages\ZXing.Net.0.15.0\lib\net20\zxing.dll"
リンク切れ
https://www.microsoft.com/en-us/download/details.aspx?id=17630
Date Published: 8/6/2012
Version: 2.12.0803
File Name: ILMerge.msi
File Size: 726 KB
リンク切れ
http://download.microsoft.com/download/1/3/4/1347c99e-9dfb-4252-8f6d-a3129a069f79/ILMerge.msi
● ILMergeすると .pdbファイルが自動的に生成される
ILMergeの実行パラメータに /ndebugを追加します。
Visual Studioのプロジェクトで「ビルド」-「出力」-「ビルドの詳細設定」-「デバッグ情報」-「none」を指定していても ILMergeを実行すると ILMergeが自動的に .pdbファイルを生成して .exe内に参照を含めます。
.pdbファイルのパス情報が .exeに埋め込まれるので、配布するアプリの場合は .pdbを含めると都合が悪いのです。(たとえ「パス情報」と言えども自身のセキュリティ的に「外部」に出したくない)
2.8 DebugInfo
2.8 DebugInfo
public bool DebugInfo { get; set; }
When this is set to true, ILMerge creates a .pdb file for the output assembly and merges into it any .pdb files found for input assemblies. If you do not want a .pdb file created for the output assembly, either set this property to false or else specify the /ndebug option at the command line.
Default: true
Command line option: /ndebug
● ILMergeに類似の ILRepack
gluck/il-repack
ILRepack 2.0.18
Install-Package ILRepack -Version 2.0.18
Tags: [Windows], [無人インストール]
●関連するコンテンツ(この記事を読んだ人は、次の記事も読んでいます)
OpenSSLコマンドを使って RSA暗号処理する方法
OpenSSLコマンドで公開鍵暗号方式の RSA暗号処理する方法を解説
Java言語で RSA暗号処理する方法、OpenSSLとの連携方法、Androidにも対応
Java言語で公開鍵暗号方式の RSA暗号処理と OpenSSLと連携する方法を解説
Visual Studio 2013の .NET C#で RSA暗号処理する方法、OpenSSLとの連携方法
C# .NETで公開鍵暗号方式の RSA暗号処理と OpenSSLと連携する方法を解説
PHP言語で RSA暗号処理する方法、OpenSSLとの連携方法
PHP言語で 公開鍵暗号方式の RSA暗号処理と OpenSSLと連携する方法を解説
GO言語で RSA暗号処理する方法、OpenSSLとの連携方法
GO言語で 公開鍵暗号方式の RSA暗号処理と OpenSSLと連携する方法を解説
Visual Studio 2013の C# .NETで Hash計算処理をする方法のサンプルプログラム
HashAlgorithm.TransformBlockを使用すると巨大ファイル等でハッシュ計算の進行状況の進捗を取得できます
C# .NETで ZIPファイル解凍ツール UnZipをソースリスト 1行で自作する方法、Windows .NET専用
Visual Studio 2013の C# .NET 4.5で ZipFile.ExtractToDirectoryを使い、UnZip解凍ツールを作成
Visual Studio 2013に Windows 10 SDK + UwpDesktopで UWPの機能を素の Windowsアプリから使用
VS2013に Win 10 SDKをインストールして Uwp Desktopで UWPの機能を従来の Windowsアプリで動かす
Visual Studio 2013の C# .NETで 日本語対応の OCR文字認識アプリを自作する方法
オフライン環境で動作可能な 世界各国語対応の OCR文字認識アプリを C# .NETで作成、MS製 OCRライブラリを使用
Visual Studio 2013の C#で日本語対応の手書き文字認識アプリを自作する方法
オフライン環境で動作する世界各国語対応の手書き文字認識アプリを作成、MS製 手書き認識ライブラリを使用
Open XML SDKを使用して Officeのファイルを C#で自在に操る方法
Microsoft Officeのファイルをプログラムで生成したり直接中身の読み取りができます
Visual Studioの各バージョンと開発できる .NET Frameworkの各バージョンのまとめ
.Microsoft.NET Framework Developer Packの各バージョンのダウンロードまとめ。言語パック等
Windows 10対応 Microsoft Speech使用の音声認識アプリ
SpeechRecognizeApp 音声認識エンジンを使用してマイク入力の音声を認識します
Windows 10の音声合成エンジンを使用して入力した文字列を喋る & Waveファイル書き出し
SpeechApp Windows 10用 Speech 音声合成 Text-to-Speech TTSのアプリ
Visual Studioの C#や MFC C++用のサンプルアプリのリンク集
Visual Studioの C#や MFC C++でアプリを作る時の参考資料として
Windowsの Input Method Manager IMEや TSF Text Services Frameworkの公式情報源のまとめ
Windowsで IME Input Method Manager関連のアプリを作成する時の情報 ImeTrayもこの辺を必死に読んだ
Visual Studioの C#で開発した .Netアプリの難読化をする方法
C#で開発した .Netアプリの難読化は必須事項です、素の状態では簡単に内部を解析されます
Visual Studioの C#で開発した .Netアプリを逆コンパイルして、中身の実装を覗き見る方法
C#で開発した .Netアプリは比較的簡単に元のソースコードに戻せます
Visual Studio 2019 Professional v16.4を無人インストールする方法、完全自動でインストール
VS2019 v16.4を完全オフラインインストール&コンポーネント選択の事前設定で自動インストールする
Visual Studio 2013 Professionalを無人インストールする方法、完全自動でインストール
VS2013を Update 5適用済みとコンポーネント選択の事前設定でインストール時の手間を省く
[HOME]
|
[BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c)
2019 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator
http://www.neko.ne.jp/~freewing/software/visual_c_sharp_qr_code_encode_zxing_net/