Reputation: 2669
I have not been into image processing. I want to read a .jpeg image file in JAVA and draw pixels on the canvas based on the color values. i.e. draw all black pixels first, then draw all grey pixels, and so on.....and white pixels at the end.
I also want to introduce a very small gap between each pixel drawn so that I can see how the image is being drawn.
Any help would be appreciated.
Upvotes: 2
Views: 6691
Reputation: 7706
Here is a short, stripped down, instruction example that will get you started. This code breaks down the RGB values from the image. You then do whatever you need do with the data.
public static BufferedImage exampleForSO(BufferedImage image) {
BufferedImage imageIn = image;
BufferedImage imageOut =
new BufferedImage(imageIn.getWidth(), imageIn.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
int width = imageIn.getWidth();
int height = imageIn.getHeight();
int[] imageInPixels = imageIn.getRGB(0, 0, width, height, null, 0, width);
int[] imageOutPixels = new int[imageInPixels.length];
for (int i = 0; i < imageInPixels.length; i++) {
int alpha = (imageInPixels[i] & 0xFF000000) >> 24;
int red = (imageInPixels[i] & 0x00FF0000) >> 16;
int green = (imageInPixels[i] & 0x0000FF00) >> 8;
int blue = (imageInPixels[i] & 0x000000FF) >> 0;
// Make any change to the colors.
if ( conditionCheckerForRedGreenAndBlue ){
// bla bla bla
} else {
// yada yada yada
}
// At last, store in output array:
imageOutPixels[i] = (alpha & 0xFF) << 24
| (red & 0xFF) << 16
| (green & 0xFF) << 8
| (blue & 0xFF);
}
imageOut.setRGB(0, 0, width, height, imageOutPixels, 0, width);
return imageOut;
}
Upvotes: 4