0

I am trying to get coordinates for a font in Android TextView.

To get this, I am planning to type in TextView, go horizontally and vertically throughout the TextView, and see which pixels are black and which pixels are white. Black is part of the text, white is not.

Could you tell me how to distinguish in TextView which pixels are black and which pixels are white?

Thanks!

Eddev
  • 890
  • 1
  • 10
  • 21

1 Answers1

2

I would like to share my idea. If you are planning to iterate textview pixels and match color, you first need to get bitmap from textview.

TextView textview = (TextView) findViewById(R.id.text_title);
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache();        
Bitmap bitmap = textview.getDrawingCache();

After that, you can simply check the pixel color by following method:

for(int x = 1; x <= width; x++)
   for(int y = 1; y <= height; y++) {

int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

//now check if black or white color
if(Color.argb(1,redValue, greenValue , blueValue) == Color.BLACK) {
//do work for black pixel
}
else {
//white pixel
}
}

Hope it helps.

NightFury
  • 13,436
  • 6
  • 71
  • 120
  • Android is not recognizing the symbol width and height in your code. Could you please explain how to use these variables? – Eddev Mar 28 '16 at 20:18
  • Width and height variables represent width and height of Textview respectively. Replace them with appropriate function calls – NightFury Mar 28 '16 at 20:21
  • Thank you. I tried using the textview.getWidth() and getHeight(), but this returns 0. Another thing is that textview.getDrawingCache(), the Bitmap, returns a null Bitmap object. How do I solve this? – Eddev Mar 28 '16 at 21:53
  • Well, you can find solution [here](http://stackoverflow.com/a/22834974/437146) and [here](http://stackoverflow.com/questions/5566758/bitmap-from-textview-getdrawingcache-always-null). You can find more solutions out there on net :) – NightFury Mar 29 '16 at 04:44