how to make QR codes in perl cgi

2019-02-26 23:45发布

I'm trying to create a website with forms for people to fill out and when the user presses submit button the texts in each form field are concatenated into a single text string to be used to make a QR code. How could I do this and what language would be the best for most browsers to be compatible.

In addition, I would like to have the text fields have a new line (\n) associated with it to make the format a little more pretty when the user scans the QR code.

Please let me know.. Thanks in advance.. could you include a sample code of a website that has three text areas to concatenate?

标签: perl qr-code
1条回答
神经病院院长
2楼-- · 2019-02-27 00:07

The Imager::QRCode module makes this easy. I just knocked the following up in 5 minutes.

#!/Users/quentin/perl5/perlbrew/perls/perl-5.14.2/bin/perl

use v5.12;
use CGI; # This is a quick demo. I recommend Plack/PSGI for production.
use Imager::QRCode;

my $q = CGI->new;
my $text = $q->param('text');
if (defined $text) {
    my $qrcode = Imager::QRCode->new(
        size          => 5,
        margin        => 5,
        version       => 1,
        level         => 'M',
        casesensitive => 1,
        lightcolor    => Imager::Color->new(255, 255, 255),
        darkcolor     => Imager::Color->new(0, 0, 0),
    );
    my $img = $qrcode->plot($text);
    print $q->header('image/gif');
    $img->write(fh => \*STDOUT, type => 'gif')
        or die $img->errstr;
} else {
    print $q->header('text/html');
    print <<END_HTML;
<!DOCTYPE html>
<meta charset="utf-8">
<title>QR me</title>
<h1>QR me</h1>
<form>
    <div>
        <label>
            What text should be in the QR code?
            <textarea name="text"></textarea>
        </label>
        <input type="submit">
    </div>
</form>
END_HTML
}

How could I do this and what language would be the best for most browsers to be compatible.

If it runs on the server then you just need to make sure the output is compatible across browsers; so use GIF or PNG.

could you include a sample code of a website that has three text areas to concatenate?

Just use a . to concatenate string variables in Perl.

my $img = $qrcode->plot($foo . $bar . $baz);
查看更多
登录 后发表回答