我想將兩個TIFF圖像以FileInputStream的形式合併到一個Tiff圖像中。雖然圖像正在合併,但輸出文件將以黑色顯示。在比較原始圖像和轉換後的圖像時,我可以看到轉換圖像的位深度變爲1.有人可以提供解決方案嗎? 我使用的代碼是:從FileInputStream轉換爲圖像後輸出圖像變爲黑色
public class MergerTiffUsingBuffer {
public static void main(String[] args) {
File imageFile1 = new File("D:/Software/pdfbox-1.3.1.jar/tiff/FLAG_T24.TIF");
File imageFile2 = new File("D:/Software/pdfbox-1.3.1.jar/tiff/CCITT_3.TIF");
try {
FileInputStream fis1 = new FileInputStream(imageFile1);
FileInputStream fis2 = new FileInputStream(imageFile2);
List<BufferedImage> bufferedImages=new ArrayList<>();
List<FileInputStream> inputStreams=new ArrayList<>();
inputStreams.add(fis1);
inputStreams.add(fis2);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("tiff");
ImageReader reader = (ImageReader) readers.next();
for(FileInputStream inputStream:inputStreams){
ImageInputStream iis = ImageIO.createImageInputStream(inputStream);
reader.setInput(iis);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
OutputStream out = new FileOutputStream("D:/Software/pdfbox-1.3.1.jar/tiff/MergedTiff.TIF");
BufferedImage binarized = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
ImageIO.write(binarized, "tiff", out);
bufferedImages.add(bufferedImage);
}
System.out.println(bufferedImages.size());
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
能否請您闡述一下爲什麼空正在下面一行代碼的作爲參數傳遞給的getWidth()和getHeight():** BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB); **。 –
image.getWidth接受ImageObserver接口作爲輸入,但在這裏它不適用(我猜測),這就是爲什麼使用null。 – Anirban
'null'參數僅僅是「舊」圖像處理的人工產物。它應該是一個ImageObserver。如果您沒有,則應使用http://docs.oracle.com/javase/8/docs/api/java/awt/MediaTracker.html等待圖像加載。在任何情況下:你正在創建一個'BufferedImage.TYPE_BYTE_BINARY',那麼你爲什麼期望與1不同的位深?這是* binary *。 0或1.嘗試一個'BufferedImage.TYPE_INT_RGB'來代替。 – Marco13