[java] bmp->jpg

koly

Как конвертировать bmp в jpg на платформе 1.4.2?
Пробовал такой код - не работает:

/**
* JpegConverter
*
* This class loads all BMP images in a given directory and saves each as a JPEG
* file in the same directory. This code is specific to BMP, but it could be
* easily extended to read images of any type that ImageIO handles.
*/
public class JpegConverter {

public static void main(String args[]) {
// Default directory is current directory, overridden by -dir parameter
String imagesDir = ".";
for (int i = 0; i < args.length; ++i) {
if (args[i].equals("-dir") && i + 1) < args.length {
imagesDir = args[++i];
}
}
// directory that holds original images
File cwd = new File(imagesDir);
File files[] = cwd.listFiles;
for (int i = 0; i < files.length; ++i) {
String fileName = files[i].getName;
// converstion to lower case just for ease of replacing the
// filename extension
String fileNameLC = fileName.toLowerCase;
if (fileName.endsWith("bmp" {
try {
// Replace original "bmp" filename extension with "jpg"
int extensionIndex = fileNameLC.lastIndexOf("bmp");
String fileNameBase = fileName.substring(0, extensionIndex);
BufferedImage img = ImageIO.read(files[i]);
// create new JPEG file
File convertedImgFile =
new File(imagesDir + File.separator +
fileNameBase + "jpg");
// store original file out in JPEG format
ImageIO.write(img, "jpeg", convertedImgFile);
} catch (Exception e) {
System.out.println("Problem with " + files[i]);
}
}
}
}
}

взято отсюда: http://weblogs.java.net/blog/chet/archive/2004/07/imageio_just_an.html

anton7805

а что за классы BufferedImage и ImageIO? в стандарте java вроде нет таких, найди спецификацию по ним , и добавь jar-ник в проект, вот и заработает наверно

anton7805

сэнкс за сцылку .
Аффтору : а какие ошибки выдает ?

koly

В общем, все идем сюда :
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/
http://java.sun.com/j2se/1.5.0/docs/guide/imageio/
Метод пристального вглядывания показывает, что в 1.5 появился в сравнении с 1.4.2 плугин javax.imageio.plugins.bmp, который как раз и отвечает за чтение bmp
Оставить комментарий
Имя или ник:
Комментарий: