添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
3
0

More than 5 years have passed since last update.

perl で QR コード

Posted at

Imager::QRCode

install
$ cpanm Imager::QRCode

環境によっては、sudo 必要

サンプルコードを動かそうとして。
$ perldoc Imager::QRCode | grep -A 13 SYNOPSIS | grep -v SYNOPSIS > test.pl
$ cat test.pl
        use Imager::QRCode;
        my $qrcode = Imager::QRCode->new(
            size          => 2,
            margin        => 2,
            version       => 1,
            level         => 'M',
            casesensitive => 1,
            lightcolor    => Imager::Color->new(255, 255, 255),
            darkcolor     => Imager::Color->new(0, 0, 0),
        my $img = $qrcode->plot("blah blah");
        $img->write(file => "qrcode.gif");
$ perl test.pl

あれ? qrcode.gif が生成されない、、、と思ったら ↓ のエラー。 @yosemite1

$ grep gif .cpanm/build.log
Warning (mostly harmless): No library found for -lgif
GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h', 'errno.h', 'string.h', 'gif'
t/200-file/220-nogif.t .......... ok

ちょっとググったら gif のライブラリが、homebrew にあるらしいので、

giflib のインストール
$ brew install giflib
$ find /PATH/TO/homebrew -name gif_lib.h 
/PATH/TO/homebrew/Cellar/giflib/4.2.3/include/gif_lib.h    
/PATH/TO/homebrew/include/gif_lib.h 
$ cpanm -reinstall Imager::QRCode

したら、build.log のエラーは消えた。

しかし、$ perl test.pl しても、qrcode.gif は生成されない。エラーも吐かない。

ウィンドウズでのこのチケットと関係があるのかないのか、、、ほっとこか2

$ perl -i -pe 's/gif/png/' test.pl 
$ perl test.pl                     

して、 qrcode.png が出来るの確認。

先日の BARCODE.pm を use して文字列付きの連番 QR コードを出力

$ cat test.pl
use strict;
use warnings;
use autodie ;
use Getopt::Long ;
use Imager::QRCode;
use BARCODE ;
my $string = q{test} ;
my $index = 0 ;
my $range = 20 ;
GetOptions (
    "string=s" => \$string,
    "start=i" => \$index,
    "range=i" => \$range,
$index -- if $index != 0 ;
my $qrcode = Imager::QRCode->new(
    margin        => 1,
    casesensitive => 1,
    mode => '8-bit',
    lightcolor    => Imager::Color->new(255, 255, 255),
    darkcolor     => Imager::Color->new(0, 0, 0),
my $obj  = BARCODE->new( $index ) ;
open my $out, q{>}, q{./index.html} ;
print $out <<EOF ;
<!doctype html>
<table>
for ( 0 .. ( $range - 1) ){
    $obj->add ;
    print $out "<tr>\n" ;
    print $out "<td>\n" ;
    my $str = $string . $obj->{barcode} ;
    my $img = $qrcode->plot($str);
    $img->write(file => "./${str}.png");
    print $out qq{<td>${str}</td>} ;
    print $out qq{<td><img src="./${str}.png" alt="${str}" width="60" height="60"></td>} ;
    print $out "\n</td>\n" ;
    print $out qq{<tr height="30"></tr>\n} ;
print $out <<EOF ;
</table>
</body>
close $out ;
$ perl test.pl -str TEST -sta 1 -r 4
3
0
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
3
0