these few lines retrieve an array of all pixels of an image.
ImageInputStream is = ImageIO.createImageInputStream(f);
Iterator iter = ImageIO.getImageReaders(is);
ImageReader imageReader = (ImageReader) iter.next();
imageReader.setInput(is);
BufferedImage image = imageReader.read(0);
final byte[] a = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
How can I retrieve actual R G B channels for a pixel (for example 0,0)?
EDIT
I’m using this array because of faster access to the image. That’s why I’m not using higher APIs’ methods like getRGB or getSample…
Assuming
image.getType() == TYPE_INT_RGBthen you have an array like this:You can access the values of pixel (x,y) via
There are several other imagetypes to consider as you can read in the documentation.
The access method basically stays the same, you just have different color ordering or an additional alpha channel. However, for those 4 channel pixels you have to adjust the formular for first channel of the pixel (x,y) to