我尝試了以下代碼来完成讀取和寫入tiff圖像的任務:
// Define the source and destination file names.
String inputFile = /images/FarmHouse.tif
String outputFile = /images/FarmHouse.bmp
// Load the input image.
RenderedOp src = JAI.create("fileload", inputFile);
// Encode the file as a BMP image.
FileOutputStream stream =
new FileOutputStream(outputFile);
JAI.create("encode", src, stream, BMP, null);
// Store the image in the BMP format.
JAI.create("filestore", src, outputFile, BMP, null);
但是,当我執行代碼時,出現以下錯誤訊息:
Caused by: java.lang.IllegalArgumentException: Only images with either 1 or 3 bands
can be written out as BMP files.
at com.sun.media.jai.codecimpl.BMPImageEncoder.encode(BMPImageEncoder.java:123)
at com.sun.media.jai.opimage.EncodeRIF.create(EncodeRIF.java:79)
有什麼想法可以解決這个問题吗?
最新回復
- 6月前1 #
- 6月前2 #
FileInputStream in = new FileInputStream(imgFullPath); FileChannel channel = in.getChannel(); ByteBuffer buffer = ByteBuffer.allocate((int)channel.size()); channel.read(buffer); tiffEncodedImg = Base64.encode(buffer.array());
使用此內容(即" tiffEncodedImg"的值)作為HTML中img標簽的src值
讀取TIFF並輸出BMP的最簡單方法是使用ImageIO類:
要使其正常工作,您唯一需要做的另一件事就是確保已將JAI ImageIO JAR添加到類路徑中,因為如果没有此庫中的插件,JRE不会處理BMP和TIFF. / p>
如果由於某種原因不能使用JAI ImageIO,則可以使其与現有代碼一起使用,但是您必须做一些額外的工作.為您正在載入的TIFF建立的颜色模型可能是BMP不支援的索引颜色模型.您可以通過提供帶有JAI.KEy_REPLACE_INDEX_COLOR_MODEL键的呈現提示,將其替換為JAI.create(" format",...)操作。
將从檔案中讀取的圖像寫入臨時圖像,然後寫出臨時圖像可能会有些運气:
我想知道您是否遇到了与常規JAI相同的索引颜色模型問题.理想情况下,除了最簡單的情况之外,您應该使用ImageIO類来获取ImageReader和Imagewriter例項,以便可以相應地調整讀寫引數,但是ImageIO.read()和.write()可以精巧地為您提供 你想要什麼。