Java 处理 TIF 图像 [英] Java Handling TIF Images
问题描述
我的问题是,如何在 Java 中将 .tif 文件成功加载到 Image 实例中?
My question is, what how can I successfully load a .tif file into an Image instance in Java?
现在让我提供更多细节.我已经阅读了很多关于如何在 Java 中处理/转换 TIF 图像的 stackoverflow 线程.我已经尝试了很多 stackoverflow 的建议(我说的是建议,因为很少有人会回来说一些对他们有用的东西).我很确定我需要使用 Java Advanced Imaging (JAI) 库,但我想我不知道如何使用它.现在让我解释一下这个项目:
Let me give some more detail now. I have read a lot of the threads on stackoverflow on how to handle/convert TIF images in Java. I have tried a lot of the stackoverflow suggestions (I say suggestions, because rarely do the people asking come back and say something has worked for them). I am pretty sure I need to use the Java Advanced Imaging (JAI) library, but I don't think I know how to use it. Let me explain the project now:
我正在从处理草图中获取帧屏幕,并使用 Xuggler 将它们编码为视频.在草图中,我使用 Processing 的 save(file filePath) 函数将当前帧保存到文件中.我曾经有 save("img"+i+".jpg"),但是创建 jpg 时发生的压缩使录制速度减慢到 9-10fps,所以我切换了文件扩展名看看我是否能得到不同的结果,save("img"+i".tif") 是最快的,允许我以大约 22-23fps 的速度录制.
I am taking screens of frames from a Processing sketch, and encoding them into video using Xuggler. In the sketch I use Processing's save(file filePath) function to save the current frame to a file. I used to have save("img"+i+".jpg"), but the compression that takes place when creating a jpg was slowing down the recording to 9-10fps, so I switched the file extension to see if I could get varying results, and save("img"+i".tif") was the fastest, allowing me to record at about 22-23fps.
处理可以将图像保存为 .tif 文件.不幸的是,Java 无法在没有库的情况下加载 .tif 文件.我曾经有过代码:
Image img = Toolkit.getDefaultToolkit().getImage("pics/img"+i+".jpg");
那行代码会将 .jpg 文件加载到 img 中,我会很好地对视频进行编码.但是这行代码(tif 而不是 jpg):
Image img = Toolkit.getDefaultToolkit().getImage("pics/img"+i+".tif");
不会加载任何图像.我仍然可以使用 Xuggler 对我的视频进行编码,但是图像是空白的,所以我认为这种方法无法加载我的 .tif 文件.
Processing can save images as .tif files. Unfortunately though, Java cannot load .tif files back without a library. I used to have the code:
Image img = Toolkit.getDefaultToolkit().getImage("pics/img"+i+".jpg");
That line of code would load up the .jpg files into img and I'd be good to encode the video. But this line of code (tif instead of jpg):
Image img = Toolkit.getDefaultToolkit().getImage("pics/img"+i+".tif");
Will not load up any image. I am still able to encode my video with Xuggler, but the images are blank, so I think this method is not working to load my .tif files.
我在 Windows 8 计算机上工作,并使用 Eclipse.任何帮助将不胜感激!
I am working on a Windows 8 computer, and using Eclipse. Any help will be really appreciated!
推荐答案
是的,您需要 JAI.
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import java.awt.Image;
import java.awt.image.RenderedImage;
...
static Image load(byte[] data) throws Exception{
Image image = null;
SeekableStream stream = new ByteArraySeekableStream(data);
String[] names = ImageCodec.getDecoderNames(stream);
ImageDecoder dec =
ImageCodec.createImageDecoder(names[0], stream, null);
RenderedImage im = dec.decodeAsRenderedImage();
image = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
return image;
}
这篇关于Java 处理 TIF 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!