buffered image to RGB array
My final goal is to import a JPEG image and convert it into an array that follows an RGB format. The array I want the image to go to is image[2][height][width]. Where image[0][height][width] is the red color scale from 0 to 255, image[1][height][width] is the green color scale from 0 to 255 and image[2][height][width] is the blue color scale from 0 to 255.
Below is the code I have so far. Basically, I can import an image to the "buffered" image, and I can write an image to a PNG file from an array of my choosing (in this case the array named "image") that follows the RGB format. But I can't convert the buffered image to an array. Does anyone know the missing code I need?
Thanks!
Code :
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class test {
//counters
int version = 100;
int imageVersion = 100;
//instantiate variables for dimensions of image array to be created
int height;
int width;
int depth = 2;
public static void main (String[] args) {
new test();
}
public test(){
//reads a jpeg image from a specified file path and writes it to a specified array
File e = new File( "C:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/Week 6/Images/readImage.jpg" );
BufferedImage bi;
try {
bi = ImageIO.read(e);
width = bi.getWidth();
height = bi.getHeight();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//final image array for output of fireworks
int image[][][] = new int[depth][height][width];
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
for (int k = 0; k < depth; ++k) {
//what do i put here to write the buffered image into the array named "image" that follows an RGB format.
}
}
}
createPNG(image);
}
//Method to write array to an image file
public void createPNG (int[][][] imagePNG) {
// -- write image to file
//System.out.println("writing to file");
try {
// -- move image into BufferedImage object
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int pixel = (imagePNG[0][i][j] << 16) | (imagePNG[1][i][j] << 8) | (imagePNG[2][i][j]);
bi.setRGB(j, i, pixel);
}
}
File outputfile = new File("C:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/Week 6/Images/randomcolors_"+imageVersion+".png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
System.out.println("image write error");
}
//System.out.println("done");
++imageVersion;
}
}
Re: buffered image to RGB array
See the following link to the API, in particular the getRGB method
BufferedImage (Java Platform SE 6)
Re: buffered image to RGB array