android Bitmap getPixel - android

I need to get the color of a pixel in order to compare it with a color from my color.xml file, but all values are negative and this comparison will always return a false result. How to get the proper color value? This color may be transparent. I've read this but I need an answer, not a link to theory.
bmp.getPixel(n.x, n.y) is returning zero when I'm expecting to return a propper value for color #00FFFFFF
Thanks

You could do something like this:
int pixel = Color.RED; //bmp.getPixel(n.x, n.y);
int a = Color.alpha(pixel);
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
String color = String.format("#%02X%02X%02X%02X", a, r, g, b); //#FFFF0000 for RED color
but instead of Color.RED you can put your bmp.getPixel(...) method.
Hope that helps
Best Regards

Related

Create color int from alpha, red, green, blue in Android

How do I create a color int from alpha, red, green, blue values (all from 0-255)?
I need this color int to set a view's background color.
I tried this:
protected int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
String[] comps = colorString.split("\\+");
int myColor = 0;
if(comps.length == 3){
int a = 255;
int r = Integer.parseInt(comps[0]);
int g = Integer.parseInt(comps[1]);
int b = Integer.parseInt(comps[2]);
myColor = Color.argb(a, r, g, b);
} else if (comps.length == 4){
int a = Integer.parseInt(comps[3]);
int r = Integer.parseInt(comps[0]);
int g = Integer.parseInt(comps[1]);
int b = Integer.parseInt(comps[2]);
myColor = Color.argb(a, r, g, b);
}
return myColor;
}
However, when I use the result for setting a views background color, both example colorString are of the same red???
Thanks a lot.
You can convert it by using the argb(int red, int green, int blue) method from Color class like this :
int convertedColor= Color.argb(red, green, blue);
and set it into your view like this:
yourView.setBackgroundColor(convertedColor);
Color opaqueRed = Color.valueOf(0xffff0000); // from a color int
Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f);
int nonTransparentRed = Color.argb(255, 255, 0, 0);
From your question, it seems you're not annotating the return value of the function by #ColorInt.
Change your function to
#ColorInt
private int colorStringToColor(){...};
Also, in your function, annotate
#ColorInt int myColor = 0;
Android has android.graphics.Color class, which provides methods to operate on Colors.
To get a ColorInt from ARGB values, you can use Color.argb(int alpha, int red, int green, int blue);
method, which will return you corresponding ColorInt value.
You can find more about Color class from here
android.graphics.Color
Your code is perfect there is no issue.
One thing is that, if you send this "214+13+22+85" value as integer then you will get the result value as the sum of these values. so may you have done mistake there.
import android.graphics.Color;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txt);
textView.setTextColor(colorStringToColor("214+13+22+235"));
}
public int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
String[] comps = colorString.split("\\+");
int myColor = 0;
if(comps.length == 3){
int a = 255;
int r = Integer.parseInt(comps[0]);
int g = Integer.parseInt(comps[1]);
int b = Integer.parseInt(comps[2]);
myColor = Color.argb(a, r, g, b);
} else if (comps.length == 4){
int a = Integer.parseInt(comps[3]);
int r = Integer.parseInt(comps[0]);
int g = Integer.parseInt(comps[1]);
int b = Integer.parseInt(comps[2]);
myColor = Color.argb(a, r, g, b);
}
return myColor;
}
I Implement your code in my project, and I checked it by well. What I saw there! It's working.
I'm going to go ahead an agree with Prince here, your code is fine. But, also offer a theory. You are setting the view color and it's stripping the transparency. Check your numbers are they different? They should be. When you feed them into setBackgroundColor they are do the same thing. If I recall correctly it doesn't actually set the alpha. Try stripping off the alpha and using in view.setAlpha(color>>24) after you set the color. Is that functionally different?
Prince is feeding it into setTextColor rather than setBackgroundColor.
view.setBackgroundColor(color);
view.setAlpha(color>>24);

How can we invert hex-color code?

My Web API will send the background color for my views, Background color range is from white #ffffff to black #000000. So I can not set any fix text color for my information text.
What is the best way to set my text color?
I'm thinking to invert the background color and set it as my text color. But I don't know How can I invert Any color or Hex color code.
e.g if my web(background) color is #00ff11 then my text color will be #ff00ee.
For this, I search over stack but did not find any method for color conversion.
Thanks
Try this.
This will give rgb of your hex color. Now you can invert your colors as below.
int invertColor(String myColorString) {
int color = (int)Long.parseLong(myColorString, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
int invertedRed = 255 - r;
int invertedGreen = 255 - g;
int invertedBlue = 255 - b;
int invertedColor = Color.rgb(invertedRed, invertedGreen, invertedBlue);
return invertedColor.toString();
}
private PaintType getNegativePaintType(String hexa) {
int color = Color.parseColor(hexa);
return new SolidColor((color & 0xFF000000) | (~color & 0x00FFFFFF));
}
getNegativePaintType("#00ff11") will return "#ff00ee"
Independently of the platform your using, you can achieve that using RGB values simply by subtracting the used color from it's base (255).
So for example:
color = (r,g,b)
invertedColor = (255-r,255-g,255-b)
Then you could transform it to hexadecimal value or whatever you need.

Increasing intensity of RGB

I am stuck in a problem and dont know where to look at.
I need to increase intensity of particular color in image, like R, G, or Blue.
When I am doing that, certain colors are not rendering properly.
Below is the image that I took for testing:
Now when I increase like Green color:
A = Color.alpha(p);
R = Color.red(p);
G = (int)(Color.green(p)*1.2);
B = Color.blue(p);
This is what I get:
What can be the solution to fix those Pink patches.
Thanks
Ty clamping the values so that they don't go over 255. Like this:
A = Color.alpha(p);
R = Color.red(p);
G = Math.min((int)(Color.green(p)*1.2), 255);
B = Color.blue(p);

how to create a CMYK image from a RGB BITMAP in android

just like the title of the question, i want to ask how to create a CMYK image when i have a RGB image as a bitmap.
I had reads question that generate the CMYK value from RGB value just like this code
public static int[] rgbToCmyk(int red, int green, int blue)
{
int black = Math.min(Math.min(255 - red, 255 - green), 255 - blue);
if (black!=255) {
int cyan = (255-red-black)/(255-black);
int magenta = (255-green-black)/(255-black);
int yellow = (255-blue-black)/(255-black);
return new int[] {cyan,magenta,yellow,black};
} else {
int cyan = 255 - red;
int magenta = 255 - green;
int yellow = 255 - blue;
return new int[] {cyan,magenta,yellow,black};
}
}
but, after i have the CMYK value i still don't understand where to place the value just like Bitmap.setBitmap() on android.
as i know the setBitmap function use the RGB value, not CMYK value...
there are a way to change the image color type to CMYK in android?
I'm a newbie in android, correct me if I wrong..
The best thing is a library call to convert the format. Try android-lib-magick, described in the Stack Overflow question Android CMYK mode.

Color detection in a static image - OpenCV Android

I have an image with four squares red, green, blue and yellow. I need to get the rgb values of each squares. I'm able to get the rgb of the whole image, but i want it for a specific section.
The image which i am going to get will be from the camera and stored onto the SDCard
I don't know if I understand you exactly, but here it comes.
You need to create BufferedImage object to get RGB value:
File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);
You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:
Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));
After that it's easy to get red, green and blue values individually:
int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();
EDIT:
I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:
Bitmap img = BitmapFactory.decodeFile(yourFilePath);
From then the operation is pretty much the same:
int leftTop = img.getPixel(0, 0);
...
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
Use this to crop your image.
Now to detect the color of the image take a pixel from the square and detect it's color with this.
After finding the RGB value use a simple conditional statement to see if the square is red blue or green.
I got it this way
int topLeftIndex = squareImage.getPixel(0, 0);
int R1 = (topLeftIndex >> 16) & 0xff;
int G1 = (topLeftIndex >> 8) & 0xff;
int B1 = topLeftIndex & 0xff;
and same way with
int bottomLeftIndex=squareImage.getPixel(0, picHeight - 1);
int topRightIndex=squareImage.getPixel(picWidth -1 , 0);
int bottomRightIndex=squareImage.getPixel(picWidth -1, picHieght - 1);

Categories

Resources