java 转换tif,如何在java中将TIFF转换为JPEG / PNG

recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.

I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..

Therefore i raise this Question again to wish that can have better explanation and guideline from you all..

There were few issue i'm faced during go through with the solution that proposed:

1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO.

However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.

2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.

3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.

if (this.selectedDO != null) {

String tempDO = this.selectedDO.DONo;

String inPath = "J:\\" + tempDO + ".TIF";

String otPath = "J:\\" + tempDO + ".bin";

File opFile = new File(otPath);

File inFile = new File(inPath);

BufferedInputStream input = null;

BufferedOutputStream output = null;

try {

input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);

output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

int length;

while ((length = input.read(buffer)) > 0) {

output.write(buffer, 0, length);

}

} finally {

try {

output.flush();

output.close();

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.

解决方案

Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.

SeekableStream s = new FileSeekableStream(inFile);

TIFFDecodeParam param = null;

ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);

RenderedImage op = dec.decodeAsRenderedImage(0);

FileOutputStream fos = new FileOutputStream(otPath);

JPEGEncodeParam jpgparam = new JPEGEncodeParam();

jpgparam.setQuality(67);

ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);

en.encode(op);

fos.flush();

fos.close();

p/s: otPath is meant to the path that you would like to store your JPEG image.

For example: "C:/image/abc.JPG";

inFile is the input file which is the TIFF file

At least this method is workable to me. If there is any other better method, kindly share along with us.

Thanks and Regards,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值