import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;public class ResizeImage {
//图像放大或缩小的比例
private static int number = 1;
//定义目标文件的大小;如果要放大,则desth和destw为imh与multiple相乘;否则相除;
private static int desth;
private static int destw; //检查参数信息是否符合要求
private static boolean checkarg(String[] args)
{
        //取得源文件信息并创建输入流
if (args.length!=3)
{
System.out.println("Usage: ResizeImage filename s/l number ");
System.out.println("       filename is the file you want resize;");
System.out.println("       s/l: s is if you resize the image to a small image; otherwise is l");
System.out.println("       number is the multiple you want to resize");
System.out.println("       For Example: ResizeImage myimage.jpg s 3 ");
return false;
}
//检查是否为jpg文件
String result = args[0].substring(args[0].lastIndexOf(".")+1);
if(!result.matches("jpg"))
{
System.out.println("Only resize JPEG files! File extention must be: jpg");
}
//检查是否指定放大或缩小的参数:s or l
  if(!args[1].matches("s")&&!args[1].matches("l"))
{
System.out.println("Please specfic if you want the image smaller or larger");
}
try
{
number = Integer.parseInt(args[2]);
}
catch(NumberFormatException ne)
{
System.out.println("The multiple must be a integer.");
}
if(Integer.parseInt(args[2])>8)
{
System.out.println("File cannot larger than 8 times!");
}
return true;
}
private static void resize(String[] args)
{
try
{
//创建输入流
            String imageFile = args[0];
            InputStream imageIn = new FileInputStream(new File(imageFile));
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
            BufferedImage im = decoder.decodeAsBufferedImage();
            
            //取得源文件大小信息;           
            int imh = im.getHeight(null);
            int imw = im.getWidth(null); if(args[1].matches("l"))
{
desth = imh*number;
destw = imw*number;
}
else if(args[1].matches("s"))
{
desth = imh/number;
destw = imw/number;
}            BufferedImage imout = new BufferedImage(destw, desth, 1);
            Graphics g = imout.getGraphics();
            boolean b = g.drawImage(im, 0, 0, destw, desth, null);
            
            //输出转换后的文件信息;
            FileOutputStream output = new FileOutputStream("output.jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
            encoder.encode(imout);            imageIn.close();
            output.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}    public static void main(String[] args) {
if(!checkarg(args))
return;
resize(args);
System.out.println("Translation Success!");
    }
}这是我以前写的一个转图像的程序
可以直接运行
带参数,可以选择是放大还是缩小以及缩放的比例,输出的jpg图像是output.jpg