【Java】 画像編集 ~ 画像を縦・横に結合する ~

■ はじめに

 * Javaの標準APIのみで同じサイズの画像を縦もしくは横に結合する

■ サンプル

Main.java

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 画像を縦・横に結合する
 */
public class Main {
  /**
   * 使用例
   * @param args
   */
  public static void main(String[] args) {
    try {
      joinImages("sample1.png", "sample2.png", "resultForVerticalJoinedImage.png", true);
      joinImages("sample1.png", "sample2.png", "resultForHorizontalJoinedImage.png", false);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * 画像の結合
   * @param sourceImagePath 元画像パス
   * @param joiningImagePath 結合する画像パス
   * @param resultImagePath 結合した画像パス
   * @param isForVertical true:縦結合、false:横結合
   * @throws FileNotFoundException
   * @throws IOException
   */
  public static void joinImages(
    String sourceImagePath, String joiningImagePath, String resultImagePath, boolean isForVertical)
      throws FileNotFoundException, IOException {
    BufferedImage sourceImage = ImageIO.read(new FileInputStream(sourceImagePath));
    BufferedImage joiningImage = ImageIO.read(new FileInputStream(joiningImagePath));

    int joinedImageWidth = isForVertical ?
        sourceImage.getWidth() : sourceImage.getWidth() + joiningImage.getWidth();
    int joinedImageHeight = isForVertical ?
        sourceImage.getHeight() + joiningImage.getHeight() : sourceImage.getHeight();
    BufferedImage joinedImage = new BufferedImage(joinedImageWidth, joinedImageHeight, sourceImage.getType());

    Graphics graphics = joinedImage.getGraphics();
    graphics.drawImage(sourceImage, 0, 0, null);
    int joinedImageXAxis = isForVertical ? 0 : sourceImage.getWidth();
    int joinedImageYAxis = isForVertical ? sourceImage.getHeight() : 0;
    graphics.drawImage(joiningImage, joinedImageXAxis, joinedImageYAxis, null);
    graphics.dispose();

    String imageExtention = getExtention(sourceImagePath);
    ImageIO.write(joinedImage, imageExtention, new File(resultImagePath));
  }

  /**
   * 拡張子を取得する
   * 
   * @param filePath 取得したいパスまたはファイル名
   * @return 拡張子(nullの場合は、エラー)
   * @see xmlファイル(xxx.xml)から、拡張子xmlを取得したい場合
   * @see 使用例<ef><bc><9a>String outputData = changeExtention("C:\\xxx.xml", "txt");
   * @see ただし<e3><80><81>実際に「C:\\xxx.xml」がないと、エラーを返す
   */
  public static String getExtention(String filePath) {
    String result = null;

    // 引数チェック
    if (filePath == null) {
      return null;
    }

    // 取得したいデータinputDataが、ファイルかどうかを確認
    File inFile = new File(filePath);
    if (!inFile.isFile()) {
      return null;
    }

    String fileName = inFile.getName(); // フルパスからファイル名だけを取得
    // 拡張子前の"."がどの位置にあるかどうかを取得する
    int postion = fileName.lastIndexOf(".");
    if (postion != -1) {
      // 拡張子ある場合
      result = fileName.substring(postion + 1);
    } else {
      // 拡張子ない場合(実装依存)
      result = ""; // 拡張子をそのまま付加
    }
    return result;
  }
}

参考文献

http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q11102248301  

関連記事

OpenCV ~ 基本編 / 画像編集 ~
https://dk521123.hatenablog.com/entry/2016/09/28/231829