1

I have a code to read barcode in java, and it is working perfectly fine if image contains only barcode, but if I try to read barcode in a image form it is not working. But if I corp the barcode image and paste and create new image it is working.

From the above scenario I have identified that if an image contains only barcode the code is working fine but if it contains some other data too then it fails.

Please find below the code I am using to read the barcode.

package com.life;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.google.zxing.Reader;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class BarcodeGeneration {

public static void main(String[] args) throws IOException {
    InputStream barCodeInputStream = new FileInputStream("C:\\Destination\\AE973220_P01.TIF");  
    BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);  

    LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);  
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
    Reader reader = new MultiFormatReader();  
    Result result;
    try {
        result = reader.decode(bitmap);
        Systemwhi.out.println("Barcode text is " + result.getText());
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ChecksumException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  


}

}

Is there any way to read exact image position say for eg: only barcode in image using x and y axis.

Below is the code I tried to read the particular image position but didn't worked.

public static void main(String[] args) throws IOException {
    try {
    /*InputStream barCodeInputStream = new FileInputStream("C:/RinDestination/2012/12/2012-12-05/700466296/AE973220_P01.TIF");  
    BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
    LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);*/
    File imageFile=new File("C:/RinDestination/2012/12/2012-12-05/700466296/AD449293_P01.TIF" +
            "");
        BufferedImage image;
         image = ImageIO.read(imageFile);
         int height=image.getHeight();
         System.out.println("height---"+height);
         int width=image.getWidth();
         System.out.println("width---"+width);
         int minx=image.getTileHeight();
         System.out.println("minx---"+minx);
         int miny=image.getTileWidth();
         System.out.println("miny---"+miny);
         BufferedImage cropedImage = image.getSubimage(1654,-800,width,height );
         LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
    Reader reader = new MultiFormatReader();  
    Result result;

        result = reader.decode(bitmap);
        System.out.println("Barcode text is " + result.getText());
    //  byte[] b = result.getRawBytes();
    //  System.out.println(ByteHelper.convertUnsignedBytesToHexString(result.getText().getBytes("UTF8")));
        //System.out.println(ByteHelper.convertUnsignedBytesToHexString(b));
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ChecksumException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
}

But the above code is not working. Please advise how to read barcode in a image form.

Regards, Pise

pise
  • 849
  • 6
  • 24
  • 51
  • 2
    Do you have any stack trace ? Because creating a buffer which contains the bar code area should give same result as cropping the image yourself. – Brugere Jul 22 '13 at 07:10
  • Hi Brugere, for the first code I get com.google.zxing.NotFoundException and for second code I get Exception in thread "main" java.awt.image.RasterFormatException: y lies outside the raster – pise Jul 22 '13 at 07:31
  • Can you try adding an Exception catch with the most generic type and print its result ? Maybe it's another exception that occur. – Brugere Jul 22 '13 at 07:35
  • java.awt.image.RasterFormatException: y lies outside the raster at sun.awt.image.BytePackedRaster.createWritableChild(BytePackedRaster.java:1283) at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156) at com.life.BarcodeGeneration.main(BarcodeGeneration.java:67) – pise Jul 22 '13 at 07:43

2 Answers2

0

According to this error you pass a value that is out of the cropped image bounds

java.awt.image.RasterFormatException: y lies outside the raster at sun.awt.image.BytePackedRaster.createWritableChild(BytePackedRaster.java:1283) at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156) at com.life.BarcodeGeneration.main(BarcodeGeneration.java:67)

So I think your error is in this line :

BufferedImage cropedImage = image.getSubimage(1654,-800,width,height );

because it throws the error your code raises getSubimage javadoc. Are you sure you can crop with a negative value of y ?

Brugere
  • 436
  • 4
  • 16
  • This time I have changes the BufferedImage cropedImage = image.getSubimage(381,32,400,300). – pise Jul 22 '13 at 10:15
  • This time I have changed the BufferedImage cropedImage = image.getSubimage(381,32,400,300). I took the help of [mobilefish](http://www.mobilefish.com/services/record_mouse_coordinates/record_mouse_coordinates.php) to get x and y co-ordinates of barcode for image and gave 400, 300 width and height respectively. Please see the image which I am using to read barcode [image](http://i43.tinypic.com/2q9ju4i.jpg) – pise Jul 22 '13 at 10:36
  • after applying above code I am getting com.google.zxing.NotFoundException – pise Jul 22 '13 at 10:56
0

Your exception is now related to your image. As you can see in following link, it can have several origins but i guess yours is the size ;)

Zxing NotfoundException thread

Community
  • 1
  • 1
Brugere
  • 436
  • 4
  • 16
  • After better read I think not that it is a size problem. Stephan Bouwer (last post of thread) had a subImage of 914x400 px. – Brugere Jul 22 '13 at 11:53
  • I do not know how far you have been searching for answers but there is this other thread which could help you i guess : http://stackoverflow.com/questions/2489048/qr-code-encoding-and-decoding-using-zxing?rq=1 – Brugere Jul 22 '13 at 12:36
  • suppose if I want to save the crop Image just to see I am scanning the proper cropped image. Is there any way to save the crop image. – pise Jul 22 '13 at 12:38
  • I think you will have to choose another image object type to save it. But you may be able to display it on screen ? But the easiest way should be encoding text in a Bar code and then try to read it. With a small image size everything should work. – Brugere Jul 22 '13 at 12:52
  • Hi @Brugere, I saved the croped images and adjusted the x,y, width and height accordingly and finally got the success in reading barcode. Below is the code I used to save the corp image. ` BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); bi.getGraphics().drawImage(cropedImage, 0, 0, width, hieght, 0, 0, y + width, y + hieght, null); ` Thank You very much for your help, Regards, Dinesh Pise – pise Jul 23 '13 at 06:05
  • Can you accept an answer to mark this thread as resolved ? Maybe someone could find help here ;) – Brugere Jul 23 '13 at 07:22
  • Yes @Brugere, Please mark it as resolved. Once again thank you very much. Regards, Dinesh Pise – pise Jul 23 '13 at 09:22
  • ^^ your the only one who can mark it as resolved, you have to click on the left of an answer ( the v tag ) in order to accept it ;) – Brugere Jul 23 '13 at 09:54