I'm using Intent to call and show an image from Gallery, and now I made it enable to get me the coordinates of the image in a TextView using these:
final TextView textView = (TextView)findViewById(R.id.textView);
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x=0;
int y=0;
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
textViewCol.setText("It is RED");
}
/*if(redValue == 255){
if(blueValue == 0)
if(greenValue==0)
textViewCol.setText("It is Red");
}*/
return true; }
});
Now what I need to do is; to get the color (RGB value) of the exact coordinates the user selects and later on assign each to #FF0000, #00FF00 and #0000FF but for now, please help to get the Pixel color based on what I have.
Cheers.
You can get the pixel from the view like this:
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
The Color functions return the value in each channel. So all you have to do is check if Red is 255 and green and blue are 0, than set the textView text to "it is red". Just pay attention that saying that something is red is not simply that the red channel is the greater than zero. 'Cos 255-Green and 255-Red is yellow, of course.
You can also just compare the pixel to different color.
for example:
if(pixel == Color.MAGENTA){
textView.setText("It is Magenta");
}
Hope it helps.
You can modify this for your requirement. This snippet will help you get the pixel color.
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
This works more accurately for me. The key here is to use the View.getDrawingCache instead of DrawableBitmap.
palleteView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent ev) {
// TODO Auto-generated method stub
ImageView img = (ImageView) v;
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
img.setDrawingCacheEnabled(true);
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
img.setDrawingCacheEnabled(false);
try {
int pxl = imgbmp.getPixel(evX, evY);
pickedColorView.setBackgroundColor(pxl);
}catch (Exception ignore){
}
imgbmp.recycle();
return true;
}
});
Related
i have tried below code that is returning me color of specific pixel which i have pass in argument
int x = (int)event.getX();
int y = (int)event.getY();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int[] color={redValue ,blueValue ,greenValue };
btn.setBackgroundColor(getHexColor(color));
public static int getHexColor(int[] color) {
return android.graphics.Color.rgb(color[0], color[1], color[2]);
}
i have also try color palette following link is reference link that is also not returning perfect skin tone color of face
this is i have also tried
anyone have idea how to extract or get face skin tone color ??
Use this code for getting colors from images.
You can follow this tutorial for further information.
https://medium.com/david-developer/extracting-colors-from-images-integrating-picasso-and-palette-b9ba45c9c418
Palette.from(bitmap)
.generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
Palette.Swatch textSwatch = palette.getVibrantSwatch();
if (textSwatch == null) {
Toast.makeText(MainActivity.this, "Null swatch :(", Toast.LENGTH_SHORT).show();
return;
}
backgroundGroup.setBackgroundColor(textSwatch.getRgb());
titleColorText.setTextColor(textSwatch.getTitleTextColor());
bodyColorText.setTextColor(textSwatch.getBodyTextColor());
}
});
Use this
Matrix inverse = new Matrix();
v.getMatrix().invert(inverse);
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
int xCoord = (int) touchPoint[0];
int yCoord = (int) touchPoint[1];
int intColor = ((BitmapDrawable)imageView.getDrawable()).getBitmap().getPixel(xCoord,yCoord);
btn.setBackgroundColor(intColor);
Hope This will help
OR
Simply Change this in you code, don't convert the color pixel to hex.
Use the color pixel directly in setBackGroundColor(pixel)
like
btn.setBackgroundColor(pixel);
This is the full code i have tried
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==(MotionEvent.ACTION_DOWN)){
Matrix inverse = new Matrix();
v.getMatrix().invert(inverse);
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
int xCoord = (int) touchPoint[0];
int yCoord = (int) touchPoint[1];
int intColor = ((BitmapDrawable)imageView.getDrawable()).getBitmap().getPixel(xCoord ,yCoord );
try {
btn.setBackgroundColor(intColor);
}catch (Exception e){
e.printStackTrace();
}
return false;
}
return false;
}
});
Try catch is not necessary but i am putting it only for precaution for any unplanned error
I am trying to get the rgb value from an image but I always get a different value even on the same pixel.
I used following code:
imageView=(ImageView)findViewById(R.id.imageView);
final Bitmap bitmap = ((BitmapDrawable)
imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v,MotionEvent event) {
int x= (int)event.getX();
int y= (int)event.getY();
int pixel = bitmap.getPixel(x, y);
int redValue = Color.red(pixel);
int greenValue = Color.green(pixel);
int blueValue = Color.blue(pixel);
// Toast.makeText(MainActivity.this,String.format("#%02x%02x%02x", redValue,
greenValue, blueValue), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "R= " + redValue + "G= " + greenValue +
"B= " + blueValue, Toast.LENGTH_LONG).show();
// Toast.makeText(MainActivity.this,""+x+"\tY "+y,Toast.LENGTH_LONG).show();
return false;
}
});
I'm using Intent to call and show an image from Gallery, and now I made it enable to get me the coordinates of the image in a TextView using these:
final TextView textView = (TextView)findViewById(R.id.textView);
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x=0;
int y=0;
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
textViewCol.setText("It is RED");
}
/*if(redValue == 255){
if(blueValue == 0)
if(greenValue==0)
textViewCol.setText("It is Red");
}*/
return true; }
});
Now what I need to do is; to get the color (RGB value) of the exact coordinates the user selects and later on assign each to #FF0000, #00FF00 and #0000FF but for now, please help to get the Pixel color based on what I have.
Cheers.
You can get the pixel from the view like this:
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
The Color functions return the value in each channel. So all you have to do is check if Red is 255 and green and blue are 0, than set the textView text to "it is red". Just pay attention that saying that something is red is not simply that the red channel is the greater than zero. 'Cos 255-Green and 255-Red is yellow, of course.
You can also just compare the pixel to different color.
for example:
if(pixel == Color.MAGENTA){
textView.setText("It is Magenta");
}
Hope it helps.
You can modify this for your requirement. This snippet will help you get the pixel color.
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
This works more accurately for me. The key here is to use the View.getDrawingCache instead of DrawableBitmap.
palleteView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent ev) {
// TODO Auto-generated method stub
ImageView img = (ImageView) v;
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
img.setDrawingCacheEnabled(true);
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
img.setDrawingCacheEnabled(false);
try {
int pxl = imgbmp.getPixel(evX, evY);
pickedColorView.setBackgroundColor(pxl);
}catch (Exception ignore){
}
imgbmp.recycle();
return true;
}
});
I am trying to get rgb value at a point on image where user taps. I am using following code to achieve that.
imageView.setOnTouchListener(new ImageView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x=0;
int y=0;
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
}
Log.v("RGB",pixel+ " :R: "+redValue+ " G: "+blueValue+ " B:"+greenValue);
return true; }
});
But it returns same value of RGB for every point on the image that is "-10197916 :R: 100 G: 100 B:100".
I have even used int x=(int)event.getIntX();
int y=(int)event.getIntY();
But result is always same. What did I miss?
You have used:
int x=0;
int y=0;
You need to use:
int x = (int)event.getX();
int y = (int)event.getY();
This one worked,problem was to get right x,y as:
imageView.setOnTouchListener(imgSourceOnTouchListener);
OnTouchListener imgSourceOnTouchListener
= new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] {eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView)view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]);
int y = Integer.valueOf((int)eventXY[1]);
System.out.println(
"touched position: "
+ String.valueOf(eventX) + " / "
+ String.valueOf(eventY));
System.out.println(
"touched position: "
+ String.valueOf(x) + " / "
+ String.valueOf(y));
Drawable imgDrawable = ((ImageView)view).getDrawable();
Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();
System.out.println(
"drawable size: "
+ String.valueOf(bitmap.getWidth()) + " / "
+ String.valueOf(bitmap.getHeight()));
//Limit x, y range within bitmap
if(x < 0){
x = 0;
}else if(x > bitmap.getWidth()-1){
x = bitmap.getWidth()-1;
}
if(y < 0){
y = 0;
}else if(y > bitmap.getHeight()-1){
y = bitmap.getHeight()-1;
}
int touchedRGB = bitmap.getPixel(x, y);
System.out.println("touched color: " + "#" + Integer.toHexString(touchedRGB));
return true;
}};
iView_image1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
int color = 0;
try {
color = bmp.getPixel((int) event.getX(), (int) event.getY());
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
} catch (Exception e) {
}
return false;
}
});
I'm using Intent to call and show an image from Gallery, and now I made it enable to get me the coordinates of the image in a TextView using these:
final TextView textView = (TextView)findViewById(R.id.textView);
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x=0;
int y=0;
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
textViewCol.setText("It is RED");
}
/*if(redValue == 255){
if(blueValue == 0)
if(greenValue==0)
textViewCol.setText("It is Red");
}*/
return true; }
});
Now what I need to do is; to get the color (RGB value) of the exact coordinates the user selects and later on assign each to #FF0000, #00FF00 and #0000FF but for now, please help to get the Pixel color based on what I have.
Cheers.
You can get the pixel from the view like this:
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
The Color functions return the value in each channel. So all you have to do is check if Red is 255 and green and blue are 0, than set the textView text to "it is red". Just pay attention that saying that something is red is not simply that the red channel is the greater than zero. 'Cos 255-Green and 255-Red is yellow, of course.
You can also just compare the pixel to different color.
for example:
if(pixel == Color.MAGENTA){
textView.setText("It is Magenta");
}
Hope it helps.
You can modify this for your requirement. This snippet will help you get the pixel color.
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
This works more accurately for me. The key here is to use the View.getDrawingCache instead of DrawableBitmap.
palleteView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent ev) {
// TODO Auto-generated method stub
ImageView img = (ImageView) v;
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
img.setDrawingCacheEnabled(true);
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
img.setDrawingCacheEnabled(false);
try {
int pxl = imgbmp.getPixel(evX, evY);
pickedColorView.setBackgroundColor(pxl);
}catch (Exception ignore){
}
imgbmp.recycle();
return true;
}
});