Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

read an image

can anyone help me to fix the code?
i am reading an image name "lena512.PGM".
After i compile the code, there are no errors, but when i run the program,

this error occur
Exception in thread "main" java.lang.NoClassDefFoundError: ij/ImageJ (wrong name
: ij/imageJ)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:11
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at ij.imageJ.main(imageJ.java:10)
Press any key to continue...

package ij;
import ij.io.*;

public class imageJ
{
public static void main(String[] args)
{
int width=512;
int height=512;
ImagePlus s = new ImagePlus("lena512.PGM");
System.out.print("Hello world");

}
}
[1534 byte] By [denny_k] at [2007-11-11 9:50:25]
# 1 Re: read an image
read your error carefully:

Exception in thread "main" java.lang.NoClassDefFoundError: ij/ImageJ (wrong name
: ij/imageJ)

once it's written with a capital "I" and once it's written with a lowercase "i". So what you should do - check the name of the class (the actual name of the class file!) and compare it to the class name in your java code. Java can't find your class - bet it has something to do with the name ;-)

little hint... you should stick to the rules - class names should start with a capital letter (sun conventions).
marble at 2007-11-11 22:32:22 >
# 2 Re: read an image
read your error carefully:

Exception in thread "main" java.lang.NoClassDefFoundError: ij/ImageJ (wrong name
: ij/imageJ)

once it's written with a capital "I" and once it's written with a lowercase "i". So what you should do - check the name of the class (the actual name of the class file!) and compare it to the class name in your java code. Java can't find your class - bet it has something to do with the name ;-)

little hint... you should stick to the rules - class names should start with a capital letter (sun conventions).

thanx for the tips. but im facing another problem now. I've changed the class name. and this problem occurs:

Exception in thread "main" java.lang.VerifyError: (class: ij/ImagePlus, method:
waitForImage signature: (Ljava/awt/Image; )V) Bad type in putfield/putstatic
at ij.ImageJ.main(ImageJ.java:10)
Press any key to continue...

I put the image and the java file in the same folder.
the code is:

package ij;
import ij.io.*;

public class ImageJ
{
public static void main(String[] args)
{
int width=512;
int height=512;
ImagePlus s = new ImagePlus("lena512.PGM");
System.out.print("Hello world");

}
}

sorry, im a newbie in java programmer.
denny_k at 2007-11-11 22:33:27 >
# 3 Re: read an image
send the whole (!) code to m.blevins@hs-mannheim.de - i will try to figure out your problem.
marble at 2007-11-11 22:34:31 >
# 4 Re: read an image
i have sent the code and the image to your e-mail address. ^^
denny_k at 2007-11-11 22:35:25 >
# 5 Re: read an image
hey,

i wrote a quick and dirty class to read in the picture and display it... for some reason there is a lot of distortion... if someone has an idea why...

import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

import javax.swing.JFrame;

public class PGMTest {

// quick testing class - just to point the direction...

public static void main(String[] args) {

int picWidth;
int picHeight;
int picColors;

byte[][] data2D = null; // raw picdata as 2dim array

String filePath = "C:\\lena.pgm";



try {

// prepare stream tokenizer and buffered reader
// streamtokenizer is the easy way to read in tokens (="words")
// and is used just to read in header information from the pictures

FileInputStream fileInputStream = new FileInputStream(filePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
StreamTokenizer streamTokenizer = new StreamTokenizer(bufferedReader);

// comments can only be made with # within the file ('//' and '/* */' are not allowed!)
streamTokenizer.commentChar('#');
streamTokenizer.eolIsSignificant(false);
streamTokenizer.slashStarComments(false);
streamTokenizer.slashSlashComments(false);


try {


////////////////////
// READING HEADER //
////////////////////


// the header information is the beginning of your file (open it with a text editor like notepad)
// which contains so called meta information (information which do not actually belong to the picture)
// like file format, number of colors, height and width of your pics.
// comments can be written into the header with '#'.
// the file you sent me looked like this when i opened it:
//
// P5
// # Created by Imlib
// 512 512
// 255


// checking if it is a pgm file (pgm files start with "p5")
int type = streamTokenizer.nextToken(); // reading next "word"

// if first token is a number (TT_NUMBER) then quit else get the string value and check if it's "P5"
// it has to be P5, else it's not a PGM file then quit
if( (type==StreamTokenizer.TT_NUMBER) || (streamTokenizer.sval.compareTo("P5")!=0) ) {
System.err.println("ERROR: " + filePath + " is not a PGM-file!");
System.exit(-1);
}


// reading the file header which contains information about the picture (x/y size and color)

streamTokenizer.nextToken(); // reading next "word"
picWidth = (int)streamTokenizer.nval; // get width

streamTokenizer.nextToken(); // reading next "word"
picHeight = (int)streamTokenizer.nval; // get height

streamTokenizer.nextToken(); // reading next "word"
picColors=(int)streamTokenizer.nval+1; // max. color value + 1


//////////////////////////////////////////////
// READING BINARY DATA - THE ACTUAL PICTURE //
//////////////////////////////////////////////

// because the height and width is known since we read the header information
// we can now build up an byte array which will contain the raw picture information
// by running through a row loop which again contains a column loop.
// the data read

// from now on we have binary data
data2D = new byte [picWidth] [picHeight];

// reading binary data and put it into the byte array data2D.
for (int row=0; row<picHeight; row++) {
for (int col=0; col<picWidth; col++) {
data2D[col][row]=(byte)(bufferedReader.read());
}
}

fileInputStream.close();
}

catch(IOException e) {
System.err.println("Error while reading file " + filePath);
System.exit(1);
}
}

catch(FileNotFoundException e) {
System.err.println("Error: File " + filePath + " not found!");
System.exit(1);
}




////////////////////////
// DISPLAYING PICTURE //
////////////////////////
//
// after all reading is done, present the picture in a window
// i have done this very quick and dirty and it doesn't look exactly
// like the picture you sent me, but you get the idea ...
// actually i don't know if the errors already occure during the file reading - maybe the values are cut off?
// or maybe you just need to find a way to display greyscale pics - i am not so familiar with that topic.
// was late at night - as usual...

drawIt(data2D);
}




public static void drawIt(byte [][] data2D) {


JFrame defaultFrame ;
GraphicsEnvironment graphicsEnvironment ;
GraphicsDevice defaultScreenDevice ;
int screenWidth ;
int screenHeight ;
int size ;
Image offscreen ;
MemoryImageSource memImageSource ;
int[] pixels ;
Graphics graphic ;

graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment() ;
defaultScreenDevice = graphicsEnvironment.getDefaultScreenDevice() ;
defaultFrame = new JFrame(defaultScreenDevice.getDefaultConfiguration()) ;

defaultFrame.setUndecorated(false) ;
defaultFrame.setIgnoreRepaint(true);

defaultScreenDevice.setFullScreenWindow(defaultFrame);

DisplayMode[] affichages = defaultScreenDevice.getDisplayModes();

int i;
for(i=0;i<affichages.length;i++) {
if((affichages[i].getWidth()==1024)&&(affichages[i].getHeight()==768)&&(affichages[i].getBitDepth()==32))
defaultScreenDevice.setDisplayMode(affichages[i]);
}
screenWidth = defaultFrame.getWidth();
screenHeight = defaultFrame.getHeight();
size = screenWidth * screenHeight;
pixels = new int[size];

memImageSource = new MemoryImageSource(screenWidth, screenHeight, pixels, 0, screenWidth);
offscreen = Toolkit.getDefaultToolkit().createImage(memImageSource);
graphic = defaultFrame.getGraphics();

defaultFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
defaultFrame.setVisible(true);

for (int row = 0; row < data2D.length; row++) {
for (int col = 0; col <data2D[0].length; col++) {
pixels[col * screenWidth + row] = (data2D[row][col] << 24) ^ 0xFF000000;
}
}
memImageSource.newPixels();
graphic.drawImage(offscreen, 0, 0, null);
}
}
marble at 2007-11-11 22:36:24 >
# 6 Re: read an image
hey thanks marble. it helps me a lot....
denny_k at 2007-11-11 22:37:33 >