Java处理TIF图像 [英] Java Handling TIF Images

查看:288
本文介绍了Java处理TIF图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何成功将.tif文件加载到Java中的Image实例中?

My question is, what how can I successfully load a .tif file into an Image instance in Java?

现在让我们详细介绍一下。我已经在stackoverflow上阅读了很多关于如何在Java中处理/转换TIF图像的线程。我已经尝试了很多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:

我正在处理Processing草图中的帧屏幕,并使用Xuggler将它们编码为视频。在草图中,我使用Processing的save(文件filePath)函数将当前帧保存到文件中。我以前有保存(img+ i +。jpg),但是在创建jpg时发生的压缩会使录制速度降低到9-10fps,所以我切换了文件扩展名看看我是否能得到不同的结果,保存(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆