Verifying that an Image is Valid

In my application, I am loading in a user specified image to be used as an icon.

My code works fine, except in the case where an image file is invalid.

i.e. You change the file extension on some random file to ".jpg", and try to load it.

The program doesn't crash or throw any exceptions, it simply imports without problem, and displays a blank image whenever I try to display the image.

What I need is some way to verify that an image is corrupt/invalid before continuing.

Here is the simple code that selects the image:

publicclass SelectImageActionextends AbstractAction{

privatestaticfinallong serialVersionUID = 1;

public SelectImageAction(){

super("Select...");

}

publicvoid actionPerformed (ActionEvent evt){

if (imageFileChooser ==null){

imageFileChooser =new JFileChooser();

imageFileChooser.setFileFilter(new FileFilter(){

publicboolean accept(File file){

if (file.isDirectory()){

returntrue;

}

else{

String fn = file.getName();

if (fn.endsWith(".jpg") || fn.endsWith(".JPG") ||

fn.endsWith(".gif") || fn.endsWith(".GIF") ||

fn.endsWith(".png") || fn.endsWith(".PNG")){

returntrue;

}

returnfalse;

}

}

@Override

public String getDescription(){

return"Image File";

}

});

}

imageFileChooser.showOpenDialog(ManageServiceCenterDialog.this);

if (imageFileChooser.getSelectedFile() !=null){

int useImage = JOptionPane.YES_OPTION;

//TODO: IF IMAGE IS INVALID: SHOW ERROR MESSAGE AND REPEAT METHOD

if (currentImage !=null){

useImage = JOptionPane.showConfirmDialog(ManageServiceCenterDialog.this,

"Attaching a new image will clear current image, would you like to proceed?",

"Clear Current Image?", JOptionPane.YES_NO_OPTION);

}

if (useImage == JOptionPane.YES_OPTION){

String imagePath = imageFileChooser.getSelectedFile().getAbsolutePath();

currentImage =new ImageIcon(imagePath).getImage();

}

}

}

}

If I've been unclear in any way, or you need more information, please don't hesitate to ask.

[4372 byte] By [jvelliott@charter.neta] at [2007-11-27 10:52:07]
# 1

Figured it out.

A few other small changes made as well.

public class SelectImageAction extends AbstractAction {

private static final long serialVersionUID = 1;

public SelectImageAction() {

super("Attach...");

}

public void actionPerformed (ActionEvent evt) {

if (imageFileChooser == null) {

imageFileChooser = new JFileChooser();

imageFileChooser.setFileFilter(new FileFilter() {

public boolean accept(File file) {

if (file.isDirectory()) {

return true;

}

else {

String fn = file.getName();

if (fn.endsWith(".jpg") || fn.endsWith(".JPG") ||

fn.endsWith(".gif") || fn.endsWith(".GIF") ||

fn.endsWith(".png") || fn.endsWith(".PNG")) {

return true;

}

return false;

}

}

@Override

public String getDescription() {

return "Image File";

}

});

}

int retval =

imageFileChooser.showOpenDialog(SomeDialog.this);

if (retval == JFileChooser.APPROVE_OPTION) {

int useImage = JOptionPane.YES_OPTION;

String imagePath = imageFileChooser.getSelectedFile().getAbsolutePath();

Image image = new ImageIcon(imagePath).getImage();

//Load the image and check if it is valid.

MediaTracker mt = new MediaTracker(SomeDialog.this);

mt.addImage(image, 1);

boolean imagesuccess = true;

try {

mt.waitForID(1);

} catch (InterruptedException e) {

imagesuccess = false;

}

imagesuccess = imagesuccess & !mt.isErrorID(1);

if(imagesuccess)

{

if (currentImage != null) {

useImage = JOptionPane.showConfirmDialog(SomeDialog.this,

"Attaching a new image will clear current image, would you like to proceed?",

"Clear Current Image?", JOptionPane.YES_NO_OPTION);

}

if (useImage == JOptionPane.YES_OPTION) {

currentImage = image;

}

} else {

JOptionPane.showMessageDialog(SomeDialog.this,

"The selected image is corrupt or not recognized.\nPlease select another image.",

"Invalid Image", JOptionPane.ERROR_MESSAGE);

actionPerformed(evt);

}

}

}

}

jvelliott@charter.neta at 2007-7-29 11:35:37 > top of Java-index,Security,Cryptography...
# 2

Some files will throw exceptons if you

attempt to load them as images using

Java 1.6.

For the ones that don't, you might try using the

PixelGrabber to get a random clutch of points

from around the image, and compare the RGB

values of those points.

If the RGB values are identical in the sample,

it is a good sign that the image is blank, and

therefore corrupt (or very boring!).

AndrewThompson64a at 2007-7-29 11:35:37 > top of Java-index,Security,Cryptography...