0

i have read http://www.switchonthecode.com/neat-software/image-to-ascii-iphone-app , we can change the the size of the letters, change from color to grayscale to black and white.and There are some nice free "image to ASCII art" conversion sites like this one: ASCII-art.org

How does such an image conversion algorithm work?i want to do the effect in the android,but i mo any clue, some advice,any link and math method.

edit:i modify the code as :

public class TestAsciiBitmapActivity extends Activity {

private static  String BLACK = "@";
private static String CHARCOAL = "#";
private static String DARKGRAY = "8";
private static String MEDIUMGRAY = "&";
private static String MEDIUM = "o";
private static String GRAY = ":";
private static String SLATEGRAY = "*";
private static  String LIGHTGRAY = ".";
private static String WHITE = " ";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView tv=(TextView) findViewById(R.id.tv);
    Button bu=(Button) findViewById(R.id.bu);
    Drawable dw = getResources().getDrawable(R.drawable.a2);
    final Bitmap bitmap = ((BitmapDrawable) dw).getBitmap();

    bu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(GrayscaleImageToASCII(bitmap));
        }
    });
}

public static String GrayscaleImageToASCII(Bitmap bmp)
{
    StringBuilder html = new StringBuilder();


    try
    {
        // Create a bitmap from the image



        // The text will be enclosed in a paragraph tag with the class

        // ascii_art so that we can apply CSS styles to it.

       // html.append("<br/&rt;");

        // Loop through each pixel in the bitmap

        for (int y = 0; y < bmp.getHeight(); y++)
        {
            for (int x = 0; x < bmp.getWidth(); x++)
            {
                // Get the color of the current pixel

               int c = bmp.getPixel(x, y);

                // To convert to grayscale, the easiest method is to add

                // the R+G+B colors and divide by three to get the gray

                // scaled color.



              int  r = Color.red(c);
              int g = Color.green(c);
              int b = Color.blue(c);


                // Get the R(ed) value from the grayscale color,

                // parse to an int. Will be between 0-255.

                int  rValue = (r + g + b) / 3;

                // Append the "color" using various darknesses of ASCII

                // character.

                html.append(getGrayShade(rValue));

                // If we're at the width, insert a line break

               // if (x == bmp.getWidth() - 1)
                //    html.append("&lt;br/&rt");
            }
        }

        // Close the paragraph tag, and return the html string.

     //   html.append("&lt;/p&rt;");

        return html.toString();
    }
    catch (Exception exc)
    {
        return exc.toString();
    }
    finally
    {
        bmp.recycle();
    }
}

private static  String getGrayShade(int redValue)
{
    String asciival = " ";

    if (redValue >= 230)
    {
        asciival = WHITE;
    }
    else if (redValue >= 200)
    {
        asciival = LIGHTGRAY;
    }
    else if (redValue >= 180)
    {
        asciival = SLATEGRAY;
    }
    else if (redValue >= 160)
    {
        asciival = GRAY;
    }
    else if (redValue >= 130)
    {
        asciival = MEDIUM;
    }
    else if (redValue >= 100)
    {
        asciival = MEDIUMGRAY;
    }
    else if (redValue >= 70)
    {
        asciival = DARKGRAY;
    }
    else if (redValue >= 50)
    {
        asciival = CHARCOAL;
    }
    else
    {
        asciival = BLACK;
    }

    return asciival;
}

}

the app is run,but the string effect not the same as the image,what can i modify again?

edit two: the code is correct,the effect is not good because:mobile screen is samll,if i can output print to txt file,everything is cool. so my another question is :if i can the txt file convert to bitmap,so i can scale it so that people can read it use phone? thank you

Animesh
  • 4,926
  • 14
  • 68
  • 110
pengwang
  • 19,536
  • 34
  • 119
  • 168

1 Answers1

1

There was a question a few years ago with a similar question: How do ASCII art image conversion algorithms work?

Hopefully it helps you.

Community
  • 1
  • 1
Tim
  • 6,692
  • 2
  • 25
  • 30