トップへ(mam-mam.net/)

ZXing.DelphiとDSPackでWEBカメラからバーコードやQRコードを読み取るQRコードリーダーを作る(VCL) ~Delphiソースコード集

ZXing.DelphiとDSPackでWEBカメラからバーコードやQRコードを読み取るQRコードリーダーを作る(VCL) ~Delphiソースコード集

ZXing.Delphi(https://github.com/Spelt/ZXing.Delphi)を使うと画像から
QRコードやCode39バーコードなどの画像をスキャンして文字に変換することができます。

DSPackを使うとWEBカメラから画像を取得することができます。
DSPackのDelphi IDEへのインストール方法は https://mam-mam.net/delphi/dspack_install.html を参考にしてインストールしていることとします。
この2つを使うとカメラからQRコードを読み取る Windows用QRコードスキャナ アプリケーション ソフトウェアを作ることができます。

ZXing.Delphiダウンロード

https://github.com/Spelt/ZXing.Delphi
から「Code」⇒「Download Zip」をクリックして「ZXing.Delphi-v_3.0.zip」ファイルをダウンロードします。
ダウンロードしたファイルを解凍しておきます。


    

プロジェクト作成

Delphi IDEを起動したら[ファイル]⇒[新規作成]⇒[Windows VCL アプリケーション -Delphi]をクリックして新規プロジェクトを作成します。 すべて保存ボタンを押して、
..\ドキュメント\Enbarcadero\Studio\Projects
に[新しいフォルダー]ボタンを押して「Vcl_ZXing_DSPack」等のプロジェクト保存用のフォルダを作成します。
プロジェクト保存用フォルダ内にユニット「Unit1.pas」で保存し、プロジェクト「Project1.dproj」で保存します。

「ZXing.Delphi-v_3.0.zip」ファイルを解凍した中にある「Lib」フォルダを プロジェクトフォルダ「..\ドキュメント\Enbarcadero\Studio\Projects\Vcl_ZXing_DSPack」にフォルダごとコピーします。

プロジェクトの設定

[プロジェクト]⇒[オプション]をクリックします。

  1. 左ペインで[ビルド]⇒[Delphi コンパイラ]を選択します
  2. ターゲットで[全ての構成]を選択します
  3. [条件定義]に以下を入力します
    FRAMEWORK_VCL
    (入れ忘れて一度でもコンパイルしてエラーが出たときは[条件定義]に FRAMEWORK_VCL を入れてから[プロジェクト]⇒[全てのプロジェクトをビルド]をクリックします)
  4. [検索パス」に以下を入れます
    Lib\Classes;Lib\Classes\Common;Lib\Classes\Common\Detector;Lib\Classes\Common\ReedSolomon;Lib\Classes\Filtering;Lib\Classes\1D Barcodes;Lib\Classes\2D Barcodes;Lib\Classes\2D Barcodes\Decoder;Lib\Classes\2D Barcodes\Detector
  5. 追加したら[保存]ボタンをクリックします。
Delphi IDE コンパイラ設定

画面設計

フォームに以下のコンポーネントをドラッグ&ドロップで配置します

Delphi IDE 画面設計(コンポーネントの配置)

ソースコード

以下ソースコードをコピーして貼り付けます。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
  DSPack, DXSUtil, DirectShow9,
  ZXing.ReadResult, ZXing.BarCodeFormat, ZXing.ScanManager;

type
  TForm1 = class(TForm)
    VideoWindow1: TVideoWindow;
    FilterGraph1: TFilterGraph;
    Filter1: TFilter;
    SampleGrabber1: TSampleGrabber;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure FilterGraph1GraphVideoSizeChanged(sender: TObject; Width,
      height: Word);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FilterGraph1GraphVideoSizeChanged(sender: TObject; Width,
  height: Word);
begin
  VideoWindow1.Width:=320;
  VideoWindow1.Height:=320*Height div Width;
end;

procedure TForm1.FormCreate(Sender: TObject);
var ICapB:ICaptureGraphBuilder2;
    VideoDevices:TSysDevEnum;   //取り込みビデオデバイスの列挙
begin
  Timer1.Enabled:=False;
  VideoDevices:=TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
  try
    Filter1.FilterGraph:=FilterGraph1;
    Filter1.BaseFilter.Moniker:=VideoDevices.GetMoniker(0);
    VideoWindow1.FilterGraph:=FilterGraph1;
    SampleGrabber1.FilterGraph:=FilterGraph1;
    FilterGraph1.ClearGraph;
    FilterGraph1.Mode:=gmCapture;
    FilterGraph1.Active:=True;
   ICapB:=FilterGraph1 as ICaptureGraphBuilder2;
    ICapB.RenderStream(@PIN_CATEGORY_PREVIEW,nil,Filter1 as IBaseFilter,
      SampleGrabber1 as IBaseFilter,VideoWindow1 as IBaseFilter);
    FilterGraph1.Play;
    Timer1.Enabled:=True;
  finally
    VideoDevices.Free;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FilterGraph1.Stop;
  FilterGraph1.Active:=False;
  FilterGraph1.ClearGraph;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var bmp:TBitmap;
    ScanManager:TScanManager;
    ReadResult: TReadResult;
begin
  bmp:=TBitmap.Create;
  try
    SampleGrabber1.GetBitmap(bmp);
    ScanManager:=TScanManager.Create(TBarcodeFormat.Auto,nil);
    try
      ReadResult:=ScanManager.Scan(bmp);
      if ReadResult<>nil then
        Memo1.LInes.Add(ReadResult.text);
    finally
      ScanManager.Free;
    end;
  finally
    bmp.Free
  end;
end;

end.

イベントプロパティにプロシージャを割り当てます。

イベントプロパティプロシージャ
Form1.OnCreateFormCreate
Form1.OnDestroyFormDestroy
FilterGraph1.OnGraphVideoSizeChangedFilterGraph1GraphVideoSizeChanged
Timer1.OnTimerTimer1Timer
Delphi IDE 画面設計(コンポーネントの配置)

実行する

アプリケーションをコンパイルして実行します。
カメラにQRコードやCode39バーコードなどを映すとスキャンしてMemo1に出力します。

Delphiで完成したQRコードリーダー アプリケーションソフトウェア