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

最近我在尝试显示图像文件时遇到问题.不幸的是,图像格式是TIFF格式,主要的网络浏览器不支持(因为我知道只有Safari支持这种格式).由于某些限制,我必须将此格式转换为主浏览器支持的其他格式.但是,当我尝试转换格式时,它给我带来了很多问题.

我在网上搜索过,虽然在这个链接How do I convert a TIF to PNG in Java?中发布了类似的问题“但我不能得到它提出的结果..

因此,我再次提出这个问题,希望能得到更好的解释和指导.

在完成提议的解决方案时,我遇到的问题很少:

1)根据Jonathan Feinberg提出的答案,需要安装JAI和JAI / ImageIO.

但是,在我安装了它们之后,我仍然无法在Netbean 7.2中导入该文件. NetBean 7.2仍然建议导入默认的imageIO库.

2)当我使用默认的ImageIO库Read方法时,它将返回NULL值,我无法继续进行.

3)我还尝试了其他方法,例如使用BufferedOutputStream方法将TIFF文件转换为BIN文件,但结果文件大于11 MB,这太大而无法加载并最终加载失败.

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();

}

}

因此,希望能够得到所有人的帮助和建议,以便我可以将TIFF格式转换为其他格式,如JPEG / PNG.

解决方法:

经过一些研究和测试,发现了一种将TIFF转换为JPEG的方法,很抱歉等待很长时间只上传了这个答案.

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是指您想要存储JPEG图像的路径.

例如:“C:/image/abc.JPG”;

inFile是输入文件,它是TIFF文件

至少这种方法对我来说是可行的.如果还有其他更好的方法,请与我们分享.

谢谢并恭祝安康,

标签:tiff,java,image,jai

来源: https://codeday.me/bug/20190923/1815537.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值