Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how can i convert image to 2d array?

807597Sep 24 2005 — edited Oct 9 2006
hi there,

i need some help to convert image to 2d array.
if the image has 24 bits, i need to convert it to grayscale 8-bits.

this is what i have done, but the values of the 2D array has more than 8bits...

first, i draw image onto graphics of bufferedimage:
BufImage = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = BufImage.createGraphics();
g.drawImage(i, 0, 0, null);

sceond, use the follwings to convert bufferedImage into 2D array :
int[] rgbs = int[w*h];
bi.getRGB(0, 0, w, h, rgbs, 0, w) ;
for (int j = 0; j<h; j++)
for (int i = 0; i<h; i++){
array[j] = rgbs[j*w+i]; }


can anyone enlighten me on my mistake?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2006
Added on Sep 24 2005
16 comments
3,656 views