java tif 属性,Java处理TIF图像

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

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:

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.

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.

I am working on a Windows 8 computer, and using Eclipse. Any help will be really appreciated!

解决方案

Yes, you need 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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值