2012-01-12 18 views
0

私はhttp://www.switchonthecode.com/neat-software/image-to-ascii-iphone-appを読み取っています。文字のサイズを変更して、カラーからグレースケールを黒と白に変更できます。無料の「ASCIIアートへの画像」変換がありますASCII-art.org画像処理:アンドロイドのAscii画像への画像

このような画像変換アルゴリズムはどのように機能しますか?私はアンドロイドで効果を出そうとしていますが、私は任意のヒント、いくつかのアドバイス、任意のリンクと数学の方法です。

編集:私のようにコードを変更:

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

}

アプリが実行されますが、画像のように、文字列の効果と同じではない、私は再び何を修正することができますか?

編集2:コードが正しい、効果が良くないために:携帯電話の画面がtamファイルに出力できれば、すべてがクールです。 だから私の別の質問は:私はビットマップに変換することができますので、私は人々がそれを使用して電話を読むことができるようにスケールすることができますか?ありがとう

答えて