Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: buffered image to RGB array

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

    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;
     
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: buffered image to RGB array

    See the following link to the API, in particular the getRGB method
    BufferedImage (Java Platform SE 6)

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: buffered image to RGB array

    getRGB worked! Thanks...

Similar Threads

  1. Double buffered Image is flickering
    By jay-wo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2011, 11:21 PM
  2. Getting pixels from an image, and saving them in an array?
    By jtvd78 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 14th, 2011, 08:48 PM
  3. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 03:16 PM
  4. Urgent help required - converting image to array
    By Paulious in forum Algorithms & Recursion
    Replies: 0
    Last Post: September 1st, 2010, 06:24 AM
  5. Array to image
    By oxxxis in forum Collections and Generics
    Replies: 1
    Last Post: January 19th, 2010, 06:46 PM