简体   繁体   中英

Getting pixel colors from Android TextView

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM