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 を作る †
- pom.xml : 画像を 2 値化するために image4j を依存ライブラリに追加する
<dependency>
<groupId>org.jclarion</groupId>
<artifactId>image4j</artifactId>
<version>0.7</version>
</dependency>
- プログラム
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.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import net.sf.image4j.util.ConvertUtil;
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);
param.setCompressionType("CCITT T.4");
// // TIFF に使える圧縮方式を列挙
// for(String type : param.getCompressionTypes()){
// System.out.println(type);
// }
// メタデータ
// ImageTypeSpecifier spec
// = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
ImageTypeSpecifier spec
= ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
IIOMetadata meta = writer.getDefaultImageMetadata(spec, param);
writer.prepareWriteSequence(null);
writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/apple.jpg", meta), param);
writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/orange.jpg", meta), param);
writer.writeToSequence(createIIOImage("/Users/atsushi/Downloads/grape.jpg", meta), param);
writer.endWriteSequence();
} catch (IOException ex) {
Logger.getLogger(App2.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static final IIOImage createIIOImage(final String path, final IIOMetadata meta) 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(ConvertUtil.convert1(img), thumbnails, meta);
return image;
}
}
- 実行結果
Java#JavaSE