'How would I split a drawable bitmap image into its 3 RGB components in Android Studio
I want to create 3 images from one bitmap by setting each pixel to the red channel but I keep getting an error with ("java.lang.IllegalStateException") the code that I have now. Original Image Desired Result
This is the result I'm looking for in these pictures. And then to do the same with the green and blue channels.
I've tried a few things but this is what I have currently:
private Bitmap createRGB(Bitmap r) {
//Red image
for (int x = 0; x < r.getWidth(); x++)
{
for (int y = 0; y < r.getHeight(); y++)
{
r.setPixel(x, y, r.getPixel(x, y) & 0xFFFF0000);
}
}
return r;
}
Solution 1:[1]
The original code works seem to be an issue with the scale of the image when going through the loop. The image size of 1200x1100 did not work however, 600x500 produced the correct result. (This is only for my machine, may work for bigger sizes on other android projects)
private Bitmap createRGB(Bitmap r) {
for(int i = 0; i < r.getWidth(); i++){
for(int j = 0; j < r.getHeight(); j++){
r.setPixel(i, j, r.getPixel(i, j) & 0xFFFF0000);
}
}
return r;
}
This works and changing the colour value will also produce Green and Blue results.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Benja ackerman |
