LoginSignup
8
6

More than 5 years have passed since last update.

画像→AA

Last updated at Posted at 2016-02-11

はじめに

画像をアスキーアートに変換します。
事前に以下のアプリケーションを用意します。

ライブラリとして使う方法が見当たらなかったため、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 (IOException e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (IOException 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 (IOException e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (IOException 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が出力されるため省略)

変換前の画像は以下のファイルです。
c43ffd45-ca14-9958-b185-882ee7d4e40b.jpeg

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

環境

  • 開発

    • Windows 10 Pro
    • JDK 1.8.0_112
    • NetBeans IDE 8.2
  • 動作検証

    • CentOS Linux release 7.2
    • JDK 1.8.0_112

上記の実装をベースにWebツールも公開しています。
AA変換(アスキーアート生成)|Web便利ツール@ツールタロウ

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6