Question

I'm working on a program that reads a bar code from png file. I am starting 5 pixels in (on the x-axis) which where I've determined the bar code starts in the image. I'm assigning a 1 to a black line and a 0 to white line and putting the values for each line into an array. As with all bar codes it starts with a black line, white line and another black line. This means the first 3 numbers should be 101. this is not the case however. Here is a link to the image Here is the code(I am reading across the middle horizontally of the bar code:

int q = 0;
for (i = 5; i < barcodeImage.getWidth()-5; i++) {

    colorState = barcodeImage.getRed(i,middle);
    //System.out.println(colorState);
    if (colorState == 255) {
        num[q] = 0;
    }
    if (colorState == 0) {
        num[q] = 1;
    }
    q++;
}

Any help is really appreciated! Thanks!

Était-ce utile?

La solution

The image you linked has vertical lines which are two pixels wide.

If you scan the whole .png:

public static void main(String[] args) throws IOException {
    BufferedImage barcodeImage = ImageIO.read(new File("src/barcode1.png"));
    int middle = barcodeImage.getHeight()/2;
    for (int i = 0; i < barcodeImage.getWidth(); i++) {
        System.out.println(barcodeImage.getRGB(i, middle));
    }
}

you can see the pixels are w,w,w,w,w,b,b,w,w,b,b,w,w,w,w,b,b,b,b... etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top