QR Codeを作るならImager::QRCodeを使おう!
G::B::QRcode と比べて良いところは以下。
- 出来上がったものはそのままImagerのオブジェクトになっているため、出来た画像に対していろいろできます。
- 白黒の部分の色を Imager::Color で指定できるようになっていますので、白地に緑とか青とか赤とかの QR code ができちゃったりします。
- GDなどのインストールはいりません。libqrencodeという小さなライブラリを入れるだけでさくっと動きます。
以下ベンチマークになります(ベンチマークプログラムは最後に載せています)。
Benchmark: timing 100 iterations of G::B::QRcode, I::QRCode::plot, I::QRCode::plot_qrcode...
G::B::QRcode: 1 wallclock secs ( 0.01 usr + 1.64 sys = 1.65 CPU) @ 60.61/s (n=100)
I::QRCode::plot: 2 wallclock secs ( 0.67 usr 1.57 sys + 0.28 cusr 0.14 csys = 2.66 CPU) @ 44.64/s (n=100)
I::QRCode::plot_qrcode: 3 wallclock secs ( 0.79 usr 1.35 sys + 0.33 cusr 0.15 csys = 2.62 CPU) @ 46.73/s (n=100)
Rate I::QRCode::plot I::QRCode::plot_qrcode G::B::QRcode
I::QRCode::plot 44.6/s -- -4% -26%
I::QRCode::plot_qrcode 46.7/s 5% -- -23%
G::B::QRcode 60.6/s 36% 30% --
G::B::QRcodeの 60% ぐらいしか速度でませんが、QR Code の生成はそんな頻繁ではないと思うので速度は御勘弁を…。といいつつ、もうちょっと早くならないか考え中です。
use strict;
use GD::Barcode::QRcode;
use Imager::QRCode;
use Benchmark qw(timethese cmpthese);
use Encode;
my $count = 100;
my $text = encode('cp932', decode('utf8', 'これはテストです'));
cmpthese(timethese($count, {
'G::B::QRcode' => sub {
my $oGdBar = GD::Barcode::QRcode->new(
$text, {
Ecc => 'L',
Version => 2,
ModuleSize => 2,
}
);
open(my $fh, '>', 'gd-barcode-qrcode.gif');
print $fh $oGdBar->plot->gif;
close($fh);
},
'I::QRCode::plot' => sub {
my $qrcode = Imager::QRCode->new(
level => 'L',
version => 2,
size => 2,
);
my $img = $qrcode->plot($text);
$img->write(file => 'imager-qrcode-plot.gif');
},
'I::QRCode::plot_qrcode' => sub {
my $img = Imager::QRCode::plot_qrcode($text, {
level => 'L',
version => 2,
size => 2,
});
$img->write(file => 'imager-qrcode-plot_qrcode.gif');
},
}));