Results 1 to 6 of 6
Thread: Store image in an 2D array
- 12-23-2011, 11:44 AM #1
Store image in an 2D array
I ve got a jpg image. I want to store the RGB values in an array. I found
this command. I wandering what it returns me in pixels array? Has anyone idea about how can i store an image to array?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);
- 12-23-2011, 12:47 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 12-23-2011, 12:55 PM #3
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.
- 12-23-2011, 01:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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:
You can even do without creating a temporary Color object by using a bit of bit fiddling ...Java Code:Color temp= new Color(pixels[i]); int red= temp.getRed(); int green= temp.getGreen(); int blue= temp.getBlue();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-23-2011, 01:40 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 12-23-2011, 03:53 PM #6
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
-
image store process
By sandeep23k in forum New To JavaReplies: 4Last Post: 08-14-2010, 06:32 PM -
can store image&text in rms seperately but NOT together :(
By wildheart25c in forum CLDC and MIDPReplies: 2Last Post: 03-26-2010, 01:27 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Class to store Image+Text
By Gudradain in forum New To JavaReplies: 2Last Post: 11-23-2008, 08:32 AM -
How to store/retrieve PNG image in/from RMS
By jason-nexFIT-mobileXware in forum CLDC and MIDPReplies: 0Last Post: 09-10-2008, 04:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks