Coding with Paint (FIXED)

1 points by mosh_java ↗ HN
i noticed this topic (http://i.imgur.com/QlGpd.gif) shared here, so i made a code to generate Bitmaps using text :D

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays;

/ * * @author MErsan / public class BitmapToText {

    static final int BITMAP_SMALLEST_SIZE = 56;//Calculated using 1*1 pixels image

    public static void main(String[] args) throws IOException {
        String text = "hello every one\n my name is Mosh Ersan";

        text = text.toUpperCase();
        byte[] textData = text.getBytes();
        int dataLen = textData.length;
        int pixels = dataLen / 3;
        pixels += (dataLen % 3);

        int bitmapWidth = (int) Math.sqrt(pixels);
        int bitmapHeight = bitmapWidth;

        int dataSize = (bitmapHeight * bitmapWidth);

        byte[] initialData = new byte[54];
        Arrays.fill(initialData, (byte) 0);

        byte[] endData = new byte[2];
        Arrays.fill(endData, (byte) 0);

        initialData[0] = (byte) 66;//Deafult Values
        initialData[1] = (byte) 77;//Deafult Values
        initialData[2] = (byte) (BITMAP_SMALLEST_SIZE + dataSize); //File Size in Bytes
        initialData[10] = (byte) 54;//Points to Pixels Array
        initialData[14] = (byte) 40;// I Don't
        initialData[18] = (byte) bitmapWidth;//Bitmap Width
        initialData[22] = (byte) bitmapHeight;//Bitmap Height
        initialData[26] = (byte) 1;// :S
        initialData[28] = (byte) 24;//Color Depth
        initialData[34] = (byte) 8; // And this :D

        File f = new File("D://test.bmp");
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);

        fos.write(initialData);
        fos.write(textData);
        fos.write(endData);

        fos.close();

    }
}

2 comments

[ 3.1 ms ] story [ 5.4 ms ] thread
I covered this .gif picture in-depth on my blog: http://bit.ly/yPuel0

This script of yours basically automates the steps needed to create a bitmap with text.