首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片处理: android的图像到Ascii图像

图片处理: android的图像到Ascii图像
EN

Stack Overflow用户
提问于 2012-01-12 16:22:50
回答 1查看 2.1K关注 0票数 0

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

代码语言:javascript
复制
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;
}

}

app已经运行,但是字符串效果和图片不一样,我还能修改什么?

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

EN

回答 1

Stack Overflow用户

发布于 2012-01-12 16:29:40

几年前也有一个类似的问题:How do ASCII art image conversion algorithms work?

希望它能对你有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8831935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档