use strict;
use warnings;
use Imager;
use Imager::QRCode;
use Plack::Request;
our $VERSION = 0.01;
sub render_qr {
my ($src, $size) = @_;
my $qrcode = Imager::QRCode->new(
size => $size,
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($src);
$img->write( data => \my $data, type => 'png' ) or die "Cannot output image: " . $img->errstr;
$data;
}
my $app = sub {
my $req = Plack::Request->new($_[0]);
if (my $text = $req->param("q")) {
my $size = $req->param("s") || 7;
my $png = render_qr($text, $size);
return [200, ['Content-Type' => 'image/png', 'Content-Length' => length($png)], [$png]];
} else {
my $html = <<"...";
<!doctype html>
<html>
<head>
<title>qrcode server - $VERSION</title>
</head>
<body>
<h1>qrcode server - $VERSION</h1>
<div>
This is qrcode rendering server.
<form action="/" method="get">
<select name="s">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<textarea name="q" rows="5" cols="50"></textarea>
<input type="submit" value="render" />
</form>
<a href="javascript:location.href=location.href+'?q='+encodeURIComponent(location.href)">bookmarklet</a>
</div>
</body>
</html>
...
return [200, ['Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => length($html)], [$html]];
}
};