2013-07-22 104 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