13

最近我在尝试显示图像文件时遇到了问题。不幸的是,图像格式是主要网络浏览器不支持的 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 库读取方法时,它将返回 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。

4

5 回答 5

16

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

otPath是您想要存储 JPEG 图像的路径。例如:“C:/image/abc.JPG”;
inFile是输入文件,它是 TIFF 文件

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

于 2013-07-10T02:31:39.943 回答
9
  1. 添加依赖

     <dependency>
     <groupId>com.github.jai-imageio</groupId>
     <artifactId>jai-imageio-core</artifactId>
     <version>1.3.1</version> </dependency>
    

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

  1. 编码

    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
    
于 2016-11-15T07:05:24.557 回答
7

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

  1. 添加依赖:

    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    
  2. 使用以下 Java8 代码

    public void convertTiffToPng(File file) {
    try {
        try (InputStream is = new FileInputStream(file)) {
            try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
                Iterator<ImageReader> 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();
    }
    }
    
于 2018-05-25T08:29:55.320 回答
1

如果您的目标是 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);
于 2018-07-17T17:51:32.087 回答
0

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

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

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

于 2013-03-15T09:54:22.490 回答