
はじめに
画像をアスキーアートに変換します。
事前に以下のアプリケーションを用意します。
- JavE
- http://www.jave.de/download/download.html
 - ※”jave5.zip”のリンクからダウンロード
 
 
ライブラリとして使う方法が見当たらなかったため、Runtime経由でjavaコマンドで起動します。
変換前の画像は変換後の状態と並べて後述します。
実装例
サンプルでは、動作確認しやすいようにmainメソッドで実行できるようにしてあります。
結果だけを確認したい場合は、この記事の一番下のリンク先で使えるようにしてありますのでご覧ください。
ImageToAscii.java
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
 *
 * @author tool-taro.com
 */
public class ImageToAscii {
	public static void main(String[] args) throws IOException, InterruptedException {
		//Javaのパス
		String javaPath = "/usr/java/latest/bin/java";
		//jave5のディレクトリ
		String javeDir = "/usr/local/jave5";
		//jave5.jarのパス
		String javePath = "/usr/local/jave5/jave5.jar";
		//読み取りたい画像ファイルの保存場所
		String inputFilePath = "input.jpg";
		//Ascii文字数(横)
		int width = 200;
		String[] commandArray = new String[6];
		int index = 0;
		commandArray[index++] = javaPath;
		commandArray[index++] = "-jar";
		commandArray[index++] = javePath;
		commandArray[index++] = "image2ascii";
		commandArray[index++] = inputFilePath;
		commandArray[index++] = "width=" + width;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		StringBuilder logBuilder = new StringBuilder();
		StringBuilder errorBuilder = new StringBuilder();
		int status = 0;
		try {
			//作業ディレクトリをjave5ディレクトリに移して処理する
			process = runtime.exec(commandArray, null, new File(javeDir));
			final InputStream in = process.getInputStream();
			final InputStream ein = process.getErrorStream();
			Runnable inputStreamThread = () -> {
				BufferedReader reader = null;
				String line;
				try {
					reader = new BufferedReader(new InputStreamReader(in));
					while (true) {
						line = reader.readLine();
						if (line == null) {
							break;
						}
						logBuilder.append(line).append("\n");
					}
				}
				catch (Exception e) {
				}
				finally {
					if (reader != null) {
						try {
							reader.close();
						}
						catch (Exception e) {
						}
					}
				}
			};
			Runnable errorStreamThread = () -> {
				BufferedReader reader = null;
				String line;
				try {
					reader = new BufferedReader(new InputStreamReader(ein));
					while (true) {
						line = reader.readLine();
						if (line == null) {
							break;
						}
						errorBuilder.append(line).append("\n");
					}
				}
				catch (Exception e) {
				}
				finally {
					if (reader != null) {
						try {
							reader.close();
						}
						catch (Exception e) {
						}
					}
				}
			};
			Thread inThread = new Thread(inputStreamThread);
			Thread errorThread = new Thread(errorStreamThread);
			inThread.start();
			errorThread.start();
			status = process.waitFor();
			inThread.join();
			errorThread.join();
		}
		finally {
			if (process != null) {
				try {
					process.destroy();
				}
				catch (Exception e) {
				}
			}
		}
		System.out.format("変換結果\n%1$s", logBuilder.toString());
	}
}
動作確認
$ javac ImageToAscii.java $ java ImageToAscii (AAが出力されるため省略)
変換前の画像は以下のファイルです。

変換後の文字列はテキストエディタに貼りつけてキャプチャしました。

環境
- 開発
- Windows 10 Pro
 - JDK 1.8.0_74
 - NetBeans IDE 8.1
 
 - 動作検証
- CentOS Linux release 7.2
 - JDK 1.8.0_74
 
 
上記の実装をベースにWebツールも公開しています。
AA変換(アスキーアート生成)|Web便利ツール@ツールタロウ