java tif 转 png_关于图像:如何在Java中将TIFF转换为JPEG / PNG

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

我已经在网上进行搜索,尽管此链接中发布了类似的问题,如何在Java中将TIF转换为PNG?",但我无法获得建议的结果。

因此,我再次提出这个问题,希望大家能有更好的解释和指导。

在提出建议的解决方案期间,我几乎没有遇到任何问题:

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。

看看ImageMagic(imagemagick.org/script/index.php)。 这是ImageMagic的Java接口(jmagick.org/index.html)

它类似于在链接中提出的ImageMagick,我尝试使用ImageMagick尝试了该帖子中提出的方法,但在ConvertCmd convert = new ConvertCmd();时失败了

经过一些研究和测试,找到了一种将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文件

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

谢谢并恭祝安康,

您可以从此处获取所需的jar:repository.jboss.org/nexus/content/repositories/

对于小图像,这可以正常工作,例如我尝试使用5.7 KB的图像并且可以,但是随后尝试使用80 KB的图像,则得到java.lang.IndexOutOfBoundsException。有什么帮助吗?

出现错误无法实例化JPEGEncodeParam类型

添加依赖

com.github.jai-imageio

jai-imageio-core

1.3.1

https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core

https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1

编码

final BufferedImage tif = ImageIO.read(new File("test.tif"));

ImageIO.write(tif,"png", new File("test.png"));

那多页Tif文件呢?可以将这个lib与一页以上的tif一起使用吗?可以共享一些代码吗?谢谢。

如果页面很多,请执行以下操作:

添加依赖项:

com.github.jai-imageio

jai-imageio-core

1.4.0

使用以下Java8代码

public void convertTiffToPng(File file) {

try {

try (InputStream is = new FileInputStream(file)) {

try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {

Iterator iterator = ImageIO.getImageReaders(imageInputStream);

if (iterator == null || !iterator.hasNext()) {

throw new RuntimeException("Image file format not supported by ImageIO:" + file.getAbsolutePath());

}

// We are just looking for the first reader compatible:

ImageReader reader = iterator.next();

reader.setInput(imageInputStream);

int numPage = reader.getNumImages(true);

// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png

String name = FilenameUtils.getBaseName(file.getAbsolutePath());

String parentFolder = file.getParentFile().getAbsolutePath();

IntStream.range(0, numPage).forEach(v -> {

try {

final BufferedImage tiff = reader.read(v);

ImageIO.write(tiff,"png", new File(parentFolder +"/png/" + name + v +".png"));

} catch (IOException e) {

e.printStackTrace();

}

});

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

我从这里得到javax.imageio.IIOException: 16-bit samples are not supported for Horizontal differencing Predictor :(

如果您的目标是Android,则可以尝试在Github上使用这个出色的Java库,该库提供了许多用于处理,打开和写入.tiff文件的实用程序。

该Git中的一个简单示例显示了如何将TIFF转换为JPEG:

TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();

//Set to true if you want use java exception mechanism

options.throwExceptions = false;

//Available 128Mb for work

options.availableMemory = 128 * 1024 * 1024;

//Number of tiff directory to convert;

options.readTiffDirectory = 1;

//Convert to JPEG

TiffConverter.convertTiffJpg("in.tif","out.jpg", options, progressListener);

如果他不在Android上怎么办?您建议的库使用本机Android库。

确实,我的建议适用于Android。之所以将其包含在此处,是因为我花了很多时间搜索图书馆,并希望在有人需要时共享它。病了,它适用于Android。感谢您的反馈 :)

首先,看看什么是最好的Java图像处理库/方法?对于您的代码,您可以使用

javax.imageio.ImageIO.write(im, type, represFile);

就像您看到的将图像写入文件的示例一样。

我尝试过MKYong的示例,但是当它出现ImageIO.Read时,它返回我为null,导致无法读取TIF格式。.如果我读取JPG和PNG不会出现问题。 TIFF格式

请参阅stackoverflow.com/questions/2898311/,它可能会很有用。

感谢您的建议,尽管我已经在CLASSPATH中添加了JAI jar,但我仍然无法导入它,但我无法添加它。.您能为我提供有关如何添加JAI jar的更多信息吗?非常感谢您的帮助

在JAI中挣扎了一段时间之后,终于无法使用它了。不幸的是,它会提示我一个错误,我对此一无所知。 java.lang.NoClassDefFoundError:无法初始化类javax.media.jai.JAI..i已通过网络搜索,并且大多数人说它在Server旁边找不到JAI库。但是我确定已经在JAI文件中添加了该文件。您对此错误有任何想法吗?

AFAIK,JAI名称空间现已在Java 8中列入黑名单。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值