Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

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

$
0
0

はじめに

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

サンプル

拡張子取得で以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/6683738.html

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 使用例:String outputData = changeExtention("C:\\xxx.xml", "txt");
   * @see ただし、実際に「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://blogs.yahoo.co.jp/dk521123/6683738.html

OpenCV ~ 基本編 / 画像編集 ~

http://blogs.yahoo.co.jp/dk521123/36401149.html

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>