Kugelblitz

いつ何時誰の挑戦でも受ける!

JavaでTiffからPNGに変換する

JAI亡き今、JavaでTiffファイルを扱う方法は以下。

pom.xmlに以下を追加。

 <repository>
           <id>geosolutions-it</id>
            <name>ImageIOExt Releases https://github.com/geosolutions-it/imageio-ext/wiki/ImageIOExt</name>
            <url>http://maven.geo-solutions.it/</url>
        </repository>

       <dependency>
            <groupId>it.geosolutions.imageio-ext</groupId>
            <artifactId>imageio-ext-tiff</artifactId>
            <version>1.1.7</version>
       </dependency>

Javaのコードは以下。

package net.treewoods.sample_img;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.activation.FileTypeMap;
import javax.imageio.ImageIO;

public class TiffToPng {
    public static void main(String[] args) throws Exception {
        // geo solutions の tiff 高速化オプション
        System.setProperty(&quot;it.geosolutions.imageio.tiff.lazy&quot;, &quot;true&quot;);

        String from = &quot;./in.tiff&quot;;
        String to = &quot;./out.png&quot;;

        try {
            //readの引数はImageInputStreamも可
            BufferedImage image = ImageIO.read(new File(from));
            BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D off = tmp.createGraphics();
            off.drawImage(image, 0, 0, Color.WHITE, null);
            //writeの引数はOutputStreamも可
            ImageIO.write(tmp, &quot;png&quot;, new File(to));
        } catch (Exception e) {
            System.out.println(&quot;error&quot;);
            throw e;
        }
    }
}
Pocket

他の記事