PDA

View Full Version : Image Loading



mavvy
11-28-2008, 07:52 PM
I'm stuck on loading an image. I found some sample code and i tried running it however i keep getting the exception:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
at Class.main(Class.java:35)

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
*
*/
public class Class {

public static void main(String[] args) {
Image image = null;
try {
// Read from a file
File sourceimage = new File("arrowleft.gif");
image = ImageIO.read(sourceimage);



} catch (IOException e) {
}

// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}


i don't understand why it doesn't work. the image arrowleft.jpg is in the same folder as the class. Could anyone help please.
Thank you

Twey
11-28-2008, 11:33 PM
I suspect that your file doesn't exist. Since the rest of that initialisation is dependent upon the code in that try block completing successfully, it should be inside the try block. As it is, if an IOException occurs, image will remain null, and the initialisation code will then pass that null pointer to the constructor of ImageIcon.

mavvy
11-28-2008, 11:48 PM
That is correct. Is there a specific location where i should put the image file?I have included in the package with the class. I used print statements to debug it so now i know it stops at
image = ImageIO.read(sourceimage); and doesn't run that piece of code. I dont know what to do?

Twey
11-29-2008, 11:47 AM
Without any path specified, it will be looked for in the current working directory.

mavvy
11-29-2008, 03:01 PM
I found what was wrong with it. I've put the image in C:// directory and it now works.thank you. however i now have a problem with something else. I'm trying to extract pixel data and the printing the array. however when i print the array this is what i get:[[[I@1bf216a. I dont even know what it means. do you?

Twey
11-29-2008, 03:16 PM
That's a shallow representation of the array object. [[[I means int[][][]. The rest, after the @ sign, is the memory address of the array.

mavvy
11-29-2008, 03:40 PM
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:


/*
* 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());
}
}

Twey
11-29-2008, 03:48 PM
Yes, you would. Better languages usually offer a generalised function for doing this sort of thing, but in Java one must do it manually:
void printIntArrayArrayArray(int[][][] xs) {
for (int[][] ys : xs)
for (int[] zs : ys)
for (int z : zs)
System.out.println(z);
}

mavvy
11-29-2008, 05:24 PM
sorry i'm quite new to java, how would i pass the array to that method? and where would i put it, the main method or with the rest of the code?if you could help that would be great, thanks.