単純にRGB値の差分を出力します。
単純にRGB値の差分を出力します。
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageDiff { // 入力ファイル public static final String INPUT1 = "data/input1.jpg"; public static final String INPUT2 = "data/input2.jpg"; // 出力ファイル public static final String OUTPUT = "data/output.jpg"; // 操作用画象 private static BufferedImage image1; private static BufferedImage image2; private static BufferedImage output; public static void main(String[] args) { loadImage(); doProcess(); saveImage(); } /** * 画象処理 */ private static void doProcess() { int width = Math.max(image1.getWidth(), image2.getWidth()); int height = Math.max(image1.getHeight(), image2.getHeight()); output = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int rgb1; if ((x <= image1.getWidth()) && (y <=image1.getHeight())) { rgb1 = image1.getRGB(x, y); } else { rgb1 = 0; } int rgb2; if ((x <= image2.getWidth()) && (y <=image2.getHeight())) { rgb2 = image2.getRGB(x, y); } else { rgb2 = 0; } int diff = rgb1 - rgb2; output.setRGB(x, y, diff); } } } /** * 画像の読み込み */ private static void loadImage() { try { image1 = ImageIO.read(new File(INPUT1)); image2 = ImageIO.read(new File(INPUT2)); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } /** * 画像書き込み */ private static void saveImage() { try { ImageIO.write(output, "jpg", new File(OUTPUT)); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }