Results 1 to 6 of 6
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Store image in an 2D array

    I ve got a jpg image. I want to store the RGB values in an array. I found

    Java Code:
    Image img = null;
     img = ImageIO.read(new File("C:/Users/zenitis/Desktop/right brainers/sike1.jpg"));
    int w = img.getWidth(null);
    int h = img.getHeight(null);
    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
    this command. I wandering what it returns me in pixels array? Has anyone idea about how can i store an image to array?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Store image in an 2D array

    Use a BufferedImage (that's what that read returns) which will allow you to find out what the type of the image is (RGB, ARGB, etc etc), which will tell you what the format of those ints is for each pixel.

  3. #3
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Re: Store image in an 2D array

    I got finally i use this

    String temp = Integer.toString(pixels[i]);
    int red = Color.decode(temp).getRed();
    int blue = Color.decode(temp).getBlue();
    int green = Color.decode(temp).getGreen();

    It retunrs me the RGB values.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Store image in an 2D array

    There is no need to take the path int --> String --> Color --> int. You can create a Color object given the pixel value directly:

    Java Code:
    Color temp= new Color(pixels[i]);
    int red= temp.getRed();
    int green= temp.getGreen();
    int blue= temp.getBlue();
    You can even do without creating a temporary Color object by using a bit of bit fiddling ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Store image in an 2D array

    Surely that only works with RGB?
    If the image is in any other format then it won't work, and you'll end up with funky results.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Store image in an 2D array

    Buffered is really the way to go, as people have said. A buffered image is already pretty much a 2d array with color values, hence it's MASSIVE memory requirement. If the only thing you need are the pixel color values, you can grab those directly from the buffered image. Really no need to copy the color data into an actual array, even if you plan to manipulate it. You can do all of that directly to a bufferedImage!

Similar Threads

  1. image store process
    By sandeep23k in forum New To Java
    Replies: 4
    Last Post: 08-14-2010, 06:32 PM
  2. can store image&text in rms seperately but NOT together :(
    By wildheart25c in forum CLDC and MIDP
    Replies: 2
    Last Post: 03-26-2010, 01:27 PM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. Class to store Image+Text
    By Gudradain in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 08:32 AM
  5. How to store/retrieve PNG image in/from RMS
    By jason-nexFIT-mobileXware in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-10-2008, 04:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •