0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

いといろふ #irof_history

Last updated at Posted at 2012-12-22

これは、いろふ Advent Calendarの19日目のエントリーです。

前日は、@datsuns さんの「いろふさんと遊びたいん?ほれだったらvimでしたらええでよ! #irof_history」でした。

はじめに

前日までのCalendarを振り返ってみると、二次元から三次元まで様々な事象に存在するいろふさんですが、偶然にもとあるの事象にはまだおられないということに気づきました。
ちょっと具現化してみようと思いました。

アスキーアート

利用したライブラリはこちら→ http://www.roqe.org/jitac/
mavenレポジトリにはありませんでした。
ので、jarをダウンロードする必要があります。

ライブラリを利用して、jpegからhtmlに変換してみます。
下のシェルスクリプトを実行すると、jpegから変換してくれます。
利用するファイルは、こちら

いろふ

先ほどダウンロードしたライブラリのjarと同じ場所に設置して、ファイル名を「irof.jpeg」に変更してください。

convert.sh
# !/bin/bash 

echo 'convert start.'

noise_option="$1"

if [ -z $noise_option ]; then
  noise_option=0
fi

echo "noise_option: [$noise_option]"

if [ -e irof.html ]; then
	rm irof.html
fi

java -jar jitac-0.2.0.jar -H -o irof.html -n $noise_option irof.jpeg

if [ -e irof.html ]; then
	echo "convert success!"
fi

exit 0

htmlにしてみたので、HTTPサーバーで表示してみます。
せっかくなのでGroovyでHTTPサーバーを立ち上げてみます。
参考にさせて頂いたのはこちら→ Groovyで簡易HTTPサーバー

単純に表示するだけでは面白くないので、ライブラリについているノイズ機能を利用してみます。

irof_to_ascii.groovy

import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer

class Converter {

  void convert(int parcent = 0) {
		def process = "./convert.sh ${parcent/100}".execute()
		println "${process.text}"
	}

}

def PORT = 9000
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0)

server.createContext('/', new HttpHandler() {

	@Override
    public void handle(HttpExchange exchange) throws IOException {
				        
    	def noise = [ 0, 10, 20]

    	def random = new Random().nextInt(noise.size)

		new Converter().convert(noise[random])

		def file = new File('irof.html')

		assert file.canRead()

        def bytes = file.text.bytes

        exchange.getResponseHeaders().add("Content-Type", "text/html")
        exchange.sendResponseHeaders(200, bytes.length)
		exchange.getResponseBody().write(bytes)
        exchange.close()
    }

})
server.start()

server.sh
# !/bin/bash 

groovy irof_to_ascii.groovy

これで、http://localhost:9000/ にアクセスするたびに、表情が変わった?ように見えます。

おわりに

このように@irofさんはアスキーアートになってしまいました。
様々な形態に変幻自在な@irofさんは、次にどんな風に我々を驚かしてくれるのでしょうか。

明日は、@tan_go238さんのirofコマンドをbrewでインストールしてみるです。
よろしくお願いします。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?