Ridiculous image converting problem

I have no idea what I'm doing here, as this next code will show you, but the idea is to have a simple program without an interface that simply takes a bmp image file of my choosing (hard coded choosing), and makes a jpg image out of it. I've just heard it was possible and gave it a shot.

import java.io.*;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

publicclass ImageChanger{

public BufferedImage bi;

public File out;

public File toJPG(File in){

try{

bi = ImageIO.read(in);

File out =new File("Jpeg Image");

ImageIO.write(bi,"jpg", out);

}catch (IOException io){

io.printStackTrace();

}

return out;

}

publicstaticvoid main(String[] args){

ImageChanger ic =new ImageChanger();

File image =new File("images/BMP Image.bmp");

File jpeg =new File("images/JPG Image.jpg");

if (!jpeg.exists())

try{

jpeg.createNewFile();

}catch (IOException ex){ex.printStackTrace();}

jpeg = ic.toJPG(image);

}

}

So that (probably obviously) does not work. I get "Cannot read file" exceptions and "Cannot find path specified" errors. The "BMP Image.bmp" is an existing file in the /images/ directory. The other file should be the output file. Any ideas?

[2506 byte] By [llampwalla] at [2007-11-27 2:42:54]
# 1
No one can help with this? It can't be that hard
llampwalla at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 2
> No one can help with this? It can't be that> hardRather than ask this, maybe you should look at the posting currently about 4 lower than yours - http://forum.java.sun.com/thread.jspa?threadID=5166420&tstart=0 .
sabre150a at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 3

Try doing a bit more input validation: public static void main(String[] args) {

File image = new File("images/BMP Image.bmp");

if (!image.exists()) {

System.err.println("Input file '"+image.getCanonicalPath()+"' does not exist");

System.exit(1);

}

File jpeg = new File("images/JPG Image.jpg");

new ImageChanger().toJPG(image);

}

YAT_Archivista at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 4
hmm ok now it creates the new JPG file, but theres nothing in it still.
llampwalla at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 5

Btw I changed my code up a little bit to use a FileInputStreamReader. I figured it sounded appropriate.

public class ImageChanger {

public BufferedImage bi;

public File out;

public FileImageInputStream fiis;

public File toJPG(File in) {

try {

fiis = new FileImageInputStream(in);

bi = ImageIO.read(fiis);

File out = new File("Jpeg Image");

ImageIO.write(bi, "jpg", out);

} catch (IOException io) {

io.printStackTrace();

}

return out;

}

public static void main(String[] args) {

ImageChanger ic = new ImageChanger();

File image = new File("src/BMP Image.bmp");

if (!image.exists()) {

try {

System.err.println("Input file '"+image.getCanonicalPath()+"' does not exist");

System.exit(1);

} catch (IOException ex) {

ex.printStackTrace();

}

}

File jpeg = new File("src/JPG Image.jpg");

if (!jpeg.exists())

try {

jpeg.createNewFile();

} catch (IOException ex) {ex.printStackTrace();}

jpeg = ic.toJPG(image);

}

}

llampwalla at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 6
Now I actually think that took me farther from a solution.
llampwalla at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...
# 7
Ok well I guess no one knows how. I'll move on to a new project.
llampwalla at 2007-7-12 3:07:59 > top of Java-index,Java Essentials,Java Programming...