2

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?

Michał Górny
  • 18,713
  • 5
  • 53
  • 76
user1602235
  • 21
  • 1
  • 3
  • like this? http://www.moe.co.uk/2011/02/25/qr-barcodes-and-perl/ – user827992 Aug 16 '12 at 05:05
  • Is there something other than Perl? Or US Perl the fastest – user1602235 Aug 16 '12 at 20:19
  • 1
    what language or technology are you targeting? what are the software available on your server? – user827992 Aug 16 '12 at 20:26
  • 1
    +1 from me. @user1602235, you have to decide whether you want to generate QR codes on the server (and then you won't have worry [much] for the browser compatibility) - or on the client (with JavaScript, check [this discussion](http://stackoverflow.com/questions/4542632/qr-code-generation-library-in-javascript) for some ideas). As for Perl, it's certainly capable of doing that fast enough with help of CPAN XS modules. – raina77ow Aug 20 '12 at 12:42

2 Answers2

5

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);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Can you please tell me how can be formed as a php code - I mean the perl code above? – user1602235 Aug 22 '12 at 05:14
  • 8
    Since (a) I don't like PHP (b) I try to avoid knowing PHP very well and (c) you explicitly asked for Perl in the first place — no. – Quentin Aug 22 '12 at 09:39
0

Add binmode to display the image of the qr code, for example:

print $q->header('image/png');
binmode STDOUT;
$img->write(fh => \*STDOUT, type => 'png');