Re: Non-verbose, simple way of getting pixels of an image?



On Mon, 14 Jul 2008, Mark Space wrote:

Tom Anderson wrote:
On Mon, 14 Jul 2008, maestro wrote:

I just want to do somethins as simple as this:

import somelibrary

image = openfile('filename.jpg')

pixels = []
for x from 1 to image.xlength()
for y from 1 to image.ylength()
pixles.append(image.getPixel(x,y))

public int[] getPixels(String filename) throws IOException {
BufferedImage img = ImageIO.read(new File(filename)) ;
return img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()) ;
}

Not bad. This could even be static. It does require three imports though.

Ah, ya got me! Yeah, i'm slightly cheating by skipping the imports.

SSCCE:

package imagegetbuftest;

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class MyUtils {

public static int[] getPixels(String filename)
throws IOException {
BufferedImage img = ImageIO.read(new File(filename)) ;
return img.getRGB(0, 0, img.getWidth(), img.getHeight(),
null, 0, img.getWidth()) ;
}
}

Perfect. Although i'd leave out the package declaration if it's only a SSCCE.

I do wish they'd had a convenience form of getRGB that didn't take the buffer and buffer-related parameters. And possibly even one that didn't take any parameters, just returned the whole image, as we do here.

tom

--
Get my pies out of the oven!
.