Thank you, I didn't realise that. How would i then print the array contents rather than the memory address. Would i have to write a loop? This is the code I am using:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package finalproject;
/**
*
*/
import java.awt.image.*;
import java.awt.*;
// notice here that I extends an image observer, this is
import java.io.File;
// not necessary, but I need the ImageObserver to be passed as
import java.io.IOException;
// a paramater to get the width and height of the image
import javax.imageio.ImageIO;
public final class MyGifPixelGrabber implements ImageObserver
{
private Image m_image=null; // pointer to original image
private Object m_pixels=null; // will contains either array of
// (for gif, it will be bytes)
private int m_iNumOfColors=0;
private int m_iWidth, m_iHeight;
private ColorModel m_colorModel;
private int [] pixelArray;
MyGifPixelGrabber(Image img)
{
m_image=img;
//BufferedImage bi = null;
try {
img = ImageIO.read(new File("c:\\strawberry.jpg"));
} catch (IOException ex) {
}
}
public void grabPixels()
{
m_iWidth=m_image.getWidth(this);
m_iHeight=m_image.getHeight(this);
// the parameter false below tells java that we want to open indexed
// file. When used with a gif file, this will cause
// palletized mode grab. When used with jpg, you'll get DirectColorModel
// data
// you can set that to true and it will cause all images to be grabbed
// in DirectColorModel
// CONSTRUCT
PixelGrabber pixelGrabber=new PixelGrabber(m_image, 0,0,
m_iWidth, m_iHeight, true);
// GRAB
try
{
pixelGrabber.grabPixels();
}
catch (Exception e)
{
System.out.println("PixelGrabber exception");
}
// GET
m_pixels=(Object)pixelGrabber.getPixels();
// get the palette of the image, if possible
m_colorModel=pixelGrabber.getColorModel();
// if loading a gif, you will get IndexedColorModel, if jpg, you will
// get DirectColorModel
if (!(m_colorModel instanceof IndexColorModel))
{
// not an indexed file (ie: not a gif file)
}
else
{
m_iNumOfColors=((IndexColorModel)m_colorModel).getMapSize();
}
}
// you'd need to cast the return values, which will be an array of bytes
// or an array of ints. if the file is a gif file, it will return an
// array of bytes, if jpg, you will get an array of ints
public Object getPixels()
{
return (int[]) m_pixels;
}
public int getWidth()
{
return m_iWidth;
}
public int getHeight()
{
return m_iHeight;
}
// this won't contain a valid value if you're grabbing
// a non palletized image (such as jpgs)
public int getNumOfColors()
{
return m_iNumOfColors;
}
// returns the red component value of a pixel
public int getRed(int pixel)
{
if ((m_colorModel instanceof IndexColorModel))
return ((IndexColorModel)m_colorModel).getRed(pixel);
else
return ((DirectColorModel)m_colorModel).getRed(pixel);
}
// returns the green component value of a pixel
public int getGreen(int pixel)
{
if ((m_colorModel instanceof IndexColorModel))
return ((IndexColorModel)m_colorModel).getGreen(pixel);
else
return ((DirectColorModel)m_colorModel).getGreen(pixel);
}
// returns the blue component value of a pixel
public int getBlue(int pixel)
{
if ((m_colorModel instanceof IndexColorModel))
return ((IndexColorModel)m_colorModel).getBlue(pixel);
else
return ((DirectColorModel)m_colorModel).getBlue(pixel);
}
public void destroy()
{
m_image=null;
m_pixels=null;
}
// we need this method just because we're extending ImageObserver.
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height)
{
return true;
}
public static void main(String[] args) {
Image im = null;
try {
im = ImageIO.read(new File("c:\\strawberry.jpg"));
} catch (IOException ex) {
}
MyGifPixelGrabber grab = new MyGifPixelGrabber(im);
grab.grabPixels();
System.out.println(grab.getPixels());
System.out.println(grab.getHeight());
}
}
Bookmarks