如何在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。
经过一些研究和测试,发现了一种将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文件
至少这种方法对我来说是可行的。 如果还有其他更好的方法,请与我们分享。
感谢致敬,
-
添加依赖
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"));
在多页的情况下,工作如下:
-
添加依赖:
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(); } }
首先,看看什么是最好的Java图像处理库/方法? 。 对于您的代码,您可以使用
javax.imageio.ImageIO.write(im, type, represFile);
就像你可以看到写一个图像到文件的例子 。
您可以在Github上尝试这个优秀的Java库 ,它提供了许多用于处理,打开和编写.tiff文件的实用程序。
一个简单的例子来自Git showng如何将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);