2013-07-22 84 views
1

我有一个读取java中条形码的代码,如果图像只包含条形码,它工作得非常好,但如果我尝试读取图像形式的条形码,它不起作用。但是,如果我corp条码图像并粘贴并创建新的图像它正在工作。如何在java中读取tif图像的条形码

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

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

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

+2

你有任何的堆栈跟踪?因为创建一个包含条形码区域的缓冲区应该可以得到与自己裁剪图像相同的结果。 – Brugere

+0

嗨Brugere,第一个代码我得到com.google.zxing.NotFoundException和第二个代码我在线程“主”java.awt.image.RasterFormatException异常获得异常:y位于栅格之外 – pise

+0

你可以尝试添加一个异常捕获与最通用的类​​型和打印结果?也许这是另一个例外发生。 – Brugere

回答

0

您的例外现在与您的图片相关。正如你可以在下面的链接看到,它可以有多种来源,但我猜你是大小;)

Zxing NotfoundException thread

+0

经过更好的阅读,我认为不是它是一个尺寸问题。 Stephan Bouwer(最后一篇帖子)有一个914x400像素的子图像。 – Brugere

+0

我不知道你有多远寻找答案,但有这个其他线程可以帮助你,我猜: http://stackoverflow.com/questions/2489048/qr-code-encoding-and-decoding-使用zxing?rq = 1 – Brugere

+0

假设如果我想保存裁剪图像只是为了看到我正在扫描正确的裁剪图像。有没有办法保存裁剪图像。 – pise

0

根据这个错误你传递一个值超出裁剪图像的界定

java.awt.image.RasterFormatException:y位于光栅外面的阳光。 awt.image.BytePackedRaster.createWritableChild(BytePackedRaster.java:1283)在java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156)在com.life.BarcodeGeneration.main(BarcodeGeneration.java:67)

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

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

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

+0

这次我改变了BufferedImage cropedImage = image.getSubimage(381,32400,300)。 – pise

+0

这次我改变了BufferedImage cropedImage = image.getSubimage(381,32400300)。我接受了[手机鱼]的帮助(http://www.mobilefish.com/services/record_mouse_coordinates/record_mouse_coordinates。php)获得图像的x和y坐标条码,并分别给出400,300的宽度和高度。请参阅我正在使用的图像读取条形码[image](http://i43.tinypic.com/2q9ju4i.jpg) – pise

+0

应用上面的代码后我得到com.google.zxing.NotFoundException – pise