Re: Non-verbose, simple way of getting pixels of an image?
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Mon, 14 Jul 2008 23:43:42 +0100
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!
.
- Follow-Ups:
- Re: Non-verbose, simple way of getting pixels of an image?
- From: Andrew Thompson
- Re: Non-verbose, simple way of getting pixels of an image?
- References:
- Non-verbose, simple way of getting pixels of an image?
- From: maestro
- Re: Non-verbose, simple way of getting pixels of an image?
- From: Tom Anderson
- Re: Non-verbose, simple way of getting pixels of an image?
- From: Mark Space
- Non-verbose, simple way of getting pixels of an image?
- Prev by Date: access HTTP request parameters?
- Next by Date: IBM's JVM?
- Previous by thread: Re: Non-verbose, simple way of getting pixels of an image?
- Next by thread: Re: Non-verbose, simple way of getting pixels of an image?
- Index(es):