Java BufferedImage getting red, green and blue individually
Java's Color class can do the conversion:
Color c = new Color(image.getRGB());
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
Isolating Red/Green/Blue Channel in Java BufferedImage
Color
in java is defined in a packed integer,that is in a 32 bit integer the first 8 bits are alpha, next 8 are red, next 8 are green and last 8 are blue.
Suppose the following is an 32 bit integer representing a color.Then,
AAAAAAAA RRRRRRRR GGGGGGGG
^Alpha ^Red ^Green ^Blue
That is, each of alpha, red, green and blue are basically 8 bits with values from 0 to 255 (the color range). So when you would want to combine these individual components back into the 32 bit integer color you should write
color=alpha<<24 | red<<16 | green<<8 | blue
So as per the rules change the code to the following
if(channel.equals(EIsolateChannel.ISOLATE_RED_CHANNEL))
{
newPixel = newPixel | iRed<<16;
//Can also write newPixel=iRed , since newPixel is always 0 before this
}
if(channel.equals(EIsolateChannel.ISOLATE_GREEN_CHANNEL))
{
newPixel = newPixel | iGreen<<8;
}
if(channel.equals(EIsolateChannel.ISOLATE_BLUE_CHANNEL))
{
newPixel = newPixel | iBlue;
}
Note : I have ORed the newPixel
before each component to allow display of multiple channels simultaneously, i.e you could display red and green with blue turned off.
UPDATE
The second error you are getting is due to the fact that you are not resetting the value of newPixel
after each iteration. So to fix it add the line newPixel=0
within the loop.
Your code should be
newPixel=0; //Add this line
iAlpha=new Color(img.getRGB(x, y)).getAlpha();
iRed=new Color(img.getRGB(x, y)).getRed();
iGreen=new Color(img.getRGB(x, y)).getGreen();
iBlue=new Color(img.getRGB(x, y)).getBlue();
For added efficiency I would suggest using bitshifts
for obtaining the red, green, blue, and the alpha.
int rgb = img.getRGB(x,y);
iAlpha = rgb>>24 & 0xff;
iRed = rgb >>16 & 0xff;
iGreen = rgb>>8 & 0xff;
iBlue = rgb & 0xff;
This code would run faster as it does not creates 4 Color
objects for each pixel in the source image
Java - Red, Green, Blue to getRGB
Using the Color class:
new Color(r, g, b).getRGB()
Converting Red, Green and Blue to RGB
Abdul's answer is great, but it can be really slow when creating new objects of class Color
thousands of times. The simplest way would be:
int rgb = (red << 16 | green << 8 | blue);
Using Red and Blue values to colour a pixel (BufferedImage)?
You question is really unclear. Ask your teacher what it is you are trying to achieve and then maybe we can help doing so. Here are some elements that can help you.
Get red, green and blue for an RGB number
You got the first part almost right. Remember the colors returned by getRGB()
are on 8 bits each, so here is how to get them:
int red = (rgb[i][j] >> 16) & 0xFF;
int green = (rgb[i][j] >> 8) & 0xFF;
int blue = rgb[i][j] & 0xFF;
Build an RGB number from red, green and blue values
If your problem is how to recombine rgb colors into one number, here is how to do that:
pixelColor = (red << 16) + (green << 8) + blue
Doing what you want with it
The average of red and blue is really obvious, this is the formula you pointed out:
int average = (red + blue) / 2;
If what you want is a purple pixel using the average of red and green, then you just have to set red and blue to the average value, and green to 0:
int pixelColor = (average << 16) + average // using average for red and blue, and 0 for green
EDIT: Seems that you don't want to take out the green after all.
int pixelColor = (average << 16) + (green << 8) + average // using average for red and blue, and keep the green unchanged
EDIT: Or maybe you want the green to take the average of red and blue?
int pixelColor = (red << 16) + (average << 8) + blue; // keep red and bllue, and put their average as the green color
Which byte(Alpha, Red, Green, Blue) is wrong when converting byte array with color space of 8BitARGB to BufferedImage.TYPE_4BYTE_ABGR
In the code for ARGB_to_ABGR
you probably wanted to write i % 4
where you now have length % 4
. As it is currently, I guess it is doing nothing at all.
get colour of pixel in buffered image in the form of a color object
Check out the Color API.
You can create a Color object using the rgb value returned from the buffered image getRGB(...) method.
Picasso showing blue red and green arrows on top corner
Use picasso.setIndicatorsEnabled(false)
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.fit().centerInside().placeholder(null)
.into(imgView, new Callback()
{
@Override
public void onSuccess()
{
}
@Override
public void onError()
{
Picasso.with(mContext)
.load(url)
.fit().centerInside()
.into(imgView, new Callback()
{
@Override
public void onSuccess()
{
}
@Override
public void onError()
{
}
});
}
});
The color shows the source of image which is being displayed
Red color indicates that image is fetched from network.
Green color indicates that image is fetched from cache memory.
Blue color indicates that image is fetched from disk memory.
Related Topics
How to Disable 'X-Frame-Options' Response Header in Spring Security
Does Variable = Null Set It for Garbage Collection
Can Someone Please Explain Me @Mapsid in Hibernate
Enable Partial Compilation in Intellij Idea
Spring Choose Bean Implementation at Runtime
Information About _Java_Options
How to Search in a List of Java Object
Accessing a File Inside a .Jar File
Ordering of Elements in Java Hashset
How to Change the Name of a Java Application Process
Count Int Occurrences with Java8
Creating Runnable Jar with External Files Included
Jackson Change JSONignore Dynamically
Why Does My Aes Encryption Throws an Invalidkeyexception
When to Use a List Over an Array in Java
Parsing a Hexadecimal String to an Integer Throws a Numberformatexception