reading an image pixel by pixel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    reading an image pixel by pixel

    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:


    (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:

    Online barcode decoder from the ZXing project


    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 imagecopyresize d() 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:



    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:

    Code:
    $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?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I think I understand. I have done similar image mapping in C.
    I found when reading picture files the beginning of the files would contain so many bytes of non-picture data -
    size, dimensions date etc.
    This info data can quite easily be ignored if you know where it will be and how many bytes long it is.

    I am guessing in your case it will vary.
    but what I am getting at is does this positioning data follow any regular pattern (no pun intended).
    Is it always in the same place?
    Is it always the same size?
    Does it have a start of positioning data flag?
    The picture data will be all integers.
    Are there other ASCII characters in the positioning data?

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Misunderstandin g. These sort of images are meant to be read optically, by a barcode scanner, not as an actual file. Attached is another image where I've highlighted the pixels that are not supposed to be read (at least by my scanner, they DO need to be read, but I don't need them yet.)

      So basically I need to ignore any of the pixels that are highlighted red, and read the rest of them. I don't need their actual color value, just if they're on or off (white or black).
      Attached Files

      Comment

      Working...