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]

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);
}
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);
}
}