Jump to content

Recommended Posts

I'm trying to create a QRCode decoder in PHP.  I'm not sure if something like this has been created already, but from my searching, I don't think it has.  If you don't know what QRCodes are, they're basically 2D barcodes that let you store a lot of information.  Here's an example:

 

qr_img.php?d=0123456789&e=M&s=6&v=1

(mouse over for the whole thing)

 

This code is simple, just contains the text "0123456789", but it's just for testing purposes.

 

I'm basically working my way up a QRCode generator script, doing everything in reverse.  it's obviously possible to decode them, there are many barcode readers for mobile phones capable of doing it, and there's also this site:

 

http://zxing.org/w/decode.jspx

 

But the source for that web-based decoder isn't available, and I'd like to try and figure it out on my own.

 

Ok, so now that you know the context, here's what I'm doing.  First, I read the image and get it's width and height, which should always be equal.  I start at the top-left corner and work my way down 1 pixel at a time in a diagonal line until I find a pixel of a different color (the border is always white, but not always the same width.)  Once I've found a different colored pixel, I know how wide my border is.  Starting at the border offset, I again recurse down until I hit another white pixel, this gives me my block width (the width and height of each square in the image.)  I then find the "smallsize" of the image, the size I have to make it so that each square is a single pixel, without the border.  I then do an imagecopyresized() to shrink the image down to it's smallest size, making each square a single pixel.

 

And this is where I'm stuck.  I need to read the value of each of the pixels, BUT, I need to ignore some as well.  The larger squares in the top-left and right, and bottom-left are positioning data, used to tell mobile scanners how to rotate the image to read it.  I don't need their pixel data.  Each of the positioning squares are 8x8.  There are also lines of timing data.  I'm not sure what it's for, but it's not part of the data and I don't need it's pixel value either.  Here's another version of that same image above, but with the data pixels turned red:

 

qr_img.php?d=0123456789&e=M&s=6&c=65280

 

The 3 8x8 squares in the corners and the black pixels apart from those need to be ignored.  Basically what I'm doing now is I'm just iterating through every pixel, like this:

 

$x = 0;
while($x < $smallsize) { // while x is less than 21
  $y = 0;
  while($y < $smallsize) { // while y is less than 21
    $vals[$x][$y] = imagecolorat($shrink, $x, $y);
    $y++;
    }
  $x++;
  }

 

And that gives me an array or each pixel's on/off value.  So since Idon't need the positioning and timing pixels, how would I go about ignoring them?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.