pom.xml

Javaコード

package com.mycompany.jaiexam;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) {
        
        // サポートする画像形式の読み出し
        for (String name : ImageIO.getReaderFormatNames()) {
            System.out.println("R " + name);
        }
        for (String name : ImageIO.getWriterFormatNames()) {
            System.out.println("W " + name);
        }
       
        // geo solutions の tiff 高速化オプション
        System.setProperty("it.geosolutions.imageio.tiff.lazy", "true");
        
        try {
            // TIFF 画像の読み込みと ○ を書く
            BufferedImage srcImg = ImageIO.read(new File("/Users/atsushi/Downloads/lena_std.tif"));
            Graphics2D srcG2 = (Graphics2D) srcImg.getGraphics();
            srcG2.setStroke(new BasicStroke(8.0f));
            srcG2.setPaint(Color.green);
            srcG2.draw(new Ellipse2D.Double(30, 40, 150, 160));
            srcG2.dispose();
            
            // 縮小
            double scale = 0.5;
            BufferedImage destImg 
                = new BufferedImage(
                    (int)(srcImg.getWidth() * scale),
                    (int)(srcImg.getHeight() * scale),
                    BufferedImage.TYPE_3BYTE_BGR);
            
            Graphics2D destG2 = (Graphics2D) destImg.getGraphics();
            
            Map<RenderingHints.Key, Object> hints = new HashMap<>();
            hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            destG2.setRenderingHints(hints);
            
            AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
            destG2.drawImage(srcImg, tx, null);
            destG2.dispose();
            
            // PNG で書き出し
            ImageIO.write(destImg, "png", new File("/Users/atsushi/Downloads/lena.png"));

        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

実行結果

マルチページ tiff を作る

package com.mycompany.jaiexam;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

public class App2 {

    public static void main(String[] args) {
        try {
            Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tif");
            ImageWriter writer = writers.next();

            writer.setOutput(ImageIO.createImageOutputStream(new FileOutputStream("/Users/atsushi/Downloads/out.tiff")));
            
            ImageWriteParam param = writer.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionType("JPEG");
            param.setCompressionQuality(0.8f);
            
            // TIFF に使える圧縮方式を列挙
            for(String type : param.getCompressionTypes()){
                System.out.println(type);
            }

            writer.prepareWriteSequence(null);
            writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/apple.jpg"), param);
            writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/orange.jpg"), param);
            writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/grape.jpg"), param);
            writer.endWriteSequence();
            
        } catch (IOException ex) {
            Logger.getLogger(App2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static final IIOImage createIIOImage(final String path) throws IOException {
        // 画像読み込み
        BufferedImage img = ImageIO.read(new File(path));

        // サムネイル用画像作成
        BufferedImage thumbnail = new BufferedImage(32, 32, BufferedImage.TYPE_3BYTE_BGR);

        Graphics2D g2 = (Graphics2D) thumbnail.getGraphics();
        AffineTransform tx = AffineTransform.getScaleInstance(32.0 / (double) img.getWidth(), 32.0 / (double) img.getHeight());
        g2.drawImage(img, tx, null);
        g2.dispose();

        List<BufferedImage> thumbnails = new ArrayList<>();
        thumbnails.add(thumbnail);

        IIOImage image = new IIOImage(img, thumbnails, null);
        return image;
    }
}

マルチページ tiff を分割する

package com.mycompany.jaiexam;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class App3 {

    public static void main(String[] args) {

        try {
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("tif");
            ImageReader reader = readers.next();
            ImageInputStream iis = ImageIO.createImageInputStream(new File("/Users/atsushi/Downloads/out.tiff"));
            reader.setInput(iis, false);
            int size = reader.getNumImages(true);
            for (int cnt = 0; cnt < size; cnt++) {
                BufferedImage img = reader.read(cnt);
                ImageIO.write(img, "png", new File("/Users/atsushi/Downloads/multi-" + cnt + ".png"));
            }
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

G4形式の tiff を作る


Java#JavaSE


添付ファイル: fileg4.png 2740件 [詳細] filetiff.png 2946件 [詳細] filelena.png 2641件 [詳細] filelena_std.tif 3525件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-03-23 (土) 23:49:57 (4295d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=73b1d0a653
ISBN10
ISBN13
9784061426061