如何在java中读取tif图像的条形码

如何在java中读取tif图像的条形码,java,Java,我有一个在java中读取条形码的代码,如果图像只包含条形码,它工作得非常好,但如果我尝试在图像表单中读取条形码,它就不工作了。但是,如果我公司的条码图像和粘贴,并创建新的图像,它是工作 从上面的场景中,我发现如果一个图像只包含条形码,代码工作正常,但是如果它也包含一些其他数据,那么它就会失败 请在下面找到我用来读取条形码的代码 package com.life; import java.awt.image.BufferedImage; import java.io.FileInputStrea

我有一个在java中读取条形码的代码,如果图像只包含条形码,它工作得非常好,但如果我尝试在图像表单中读取条形码,它就不工作了。但是,如果我公司的条码图像和粘贴,并创建新的图像,它是工作

从上面的场景中,我发现如果一个图像只包含条形码,代码工作正常,但是如果它也包含一些其他数据,那么它就会失败

请在下面找到我用来读取条形码的代码

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();
    }  


}

}
是否有任何方法可以读取准确的图像位置,例如:仅使用x轴和y轴读取图像中的条形码

下面是我试图读取特定图像位置但不起作用的代码

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();
    }  
}
但是上面的代码不起作用。请告知如何读取图像形式的条形码

问候,,
Pise

根据此错误,您传递的值超出了裁剪图像的边界


java.awt.image.RasterFormatException:y位于com.life.BarcodeGeneration.main(BarcodeGeneration.java:67)的java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156)的sun.awt.image.BytePackedMaster.createWritableChild(BytePackedMaster.java:1283)处的光栅之外

所以我认为你的错误在这一行:

BuffereImage cropedImage=image.getSubimage(1654,-800,宽度,高度)

因为它会抛出代码引发的错误。
您确定可以使用负值y进行裁剪吗?

您的异常现在与图像相关。正如您在下面的链接中所看到的,它可以有几个来源,但我猜您的来源是尺寸;)


是否有堆栈跟踪?因为创建一个包含条形码区域的缓冲区应该会得到与自己裁剪图像相同的结果。嗨,Brugere,对于第一个代码,我得到com.google.zxing.NotFoundException,对于第二个代码,我在线程“main”中得到Exceptionjava.awt.image.RasterFormatException:y位于RasterFormatException之外您可以尝试添加一个最通用类型的异常捕获并打印其结果吗?也许这是发生的另一个异常。java.awt.image.RasterFormatException:y位于sun.awt.image.BytePackedMaster.createWritableChild(BytePackedMaster.java:1283)的java.awt.image.BuffereImage.getSubimage(BuffereImage.java:1156)的com.life.BarcodeGeneration.main(BarcodeGeneration.java:67)的光栅之外这次我更改了BuffereImage cropedImage=image.getSubimage(381,32400300)。这次我更改了BuffereImage cropedImage=image.getSubimage(381,32400300)。我借助于获得图像条形码的x坐标和y坐标,并分别给出了400、300宽度和高度。请看我用来读取条形码的图片,在应用上述代码后,我得到com.google.zxing.notfound例外更好地阅读后,我认为这不是尺寸问题。Stephan Bouwer(线程的最后一篇文章)有一个914x400 px的子图像。我不知道您搜索答案的距离有多远,但我想还有另一个线程可以帮助您:假设我想保存裁剪图像只是为了查看我正在扫描正确的裁剪图像。有没有办法保存裁剪图像。我想你必须选择另一种图像对象类型来保存它。但你们可以在屏幕上显示它吗?但最简单的方法应该是用条形码编码文本,然后尝试读取它。使用较小的图像大小,一切都应该正常。Hi@Brugere,我保存了交叉图像,并相应地调整了x、y、宽度和高度,最终成功读取条形码。下面是我用来保存公司图片的代码BuffereImage bi=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);bi.getGraphics().drawImage(CropImage,0,0,width,hight,0,0,y+width,y+hight,null);`非常感谢你的帮助,问候,迪内什·皮斯