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