I am trying to get the color from an image where the user touches the image. I am able to get the x,y coordinates and can calculate the pixels from it using Matrix, however my issue is it is not giving me the right color.
private void getColor(MotionEvent event, Button capture) {
float HeightRatio = (float) image.getHeight() / (float) imageView.getHeight();
float WidthRatio = (float) image.getWidth() / (float) imageView.getWidth();
Matrix inverse = new Matrix();
imageView.getImageMatrix().invert(inverse);
float[] touchPoint = new float[]{event.getX(), event.getY()};
i2.setX(event.getX());
i2.setY(event.getY());
inverse.mapPoints(touchPoint);
int x = Integer.valueOf((int) touchPoint[0]);
int y = Integer.valueOf((int) touchPoint[1]);
x = (int) (x * WidthRatio);
y = (int) (y * HeightRatio);
if (x < 0) {
x = 0;
} else if (x > image.getWidth() - 1) {
x = image.getWidth() - 1;
}
if (y < 0) {
y = 0;
} else if (y > image.getHeight() - 1) {
y = image.getHeight() - 1;
}
i2.setBackgroundColor(image.getPixel(x, y));
i2.setVisibility(View.VISIBLE);
capture.setBackgroundColor(image.getPixel(x, y));
}
This is the method i am using to get the color of the touched coordinate.
Thanks, Vipin