图片处理:图像到安卓的Ascii图像



我读过http://www.switchonthecode.com/neat-software/image-to-ascii-iphone-app,我们可以更改字母的大小,从彩色到灰度再到黑白。还有一些不错的免费"图像到ASCII艺术"转换网站,比如:ASCII-art.org

这样的图像转换算法是如何工作的?我想在android中做效果,但我不知道任何线索、一些建议、任何链接和数学方法。

编辑:我将代码修改为:

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;
}

}

应用程序正在运行,但字符串效果与图像不一样,我可以再次修改什么?

编辑二:代码是正确的,效果不好,因为:手机屏幕是samll,如果我能输出打印到txt文件,一切都很酷。所以我的另一个问题是:如果我能把txt文件转换成位图,这样我就可以缩放它,让人们可以用手机阅读它?感谢

几年前有一个类似的问题:ASCII艺术图像转换算法是如何工作的?

希望它能帮助你。