import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageTool {    public static void main(String[] args) {
        try {
            ImageTool.resize("E:/1.png", "D:/1.png", 337, 0, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    /**
     * 用途:改变图片尺寸
     * 
     * @param oldUrl 原图片文件绝对路径(含文件名)
     * @param newUrl 目标图片文件绝对路径(含文件名)
     * @param width 目标图片文件宽度
     * @param height 目标图片文件高度
     * @param proportion 是否等比例缩放
     * @return
     * @throws Exception
     * 
     * 此方法支持的图片文件格式有:jpg、gif、png
     * 不支持的图片文件格式有:bmp
     * 其它图片文件格式未测试
     */
    public static boolean resize(String oldUrl, String newUrl, int width, int height, boolean proportion) throws Exception {
        File fileIn = new File(oldUrl);
        File fileOut = new File(newUrl);
        FileOutputStream tempout = null;
        try {
            tempout = new FileOutputStream(fileOut);
        } catch (Exception ex) {
            throw ex;
        }
        Image img = null;
        Applet app = new Applet();
        MediaTracker mt = new MediaTracker(app);
        try {
            img = javax.imageio.ImageIO.read(fileIn);
            mt.addImage(img, 0);
            mt.waitForID(0);
        } catch (Exception e) {
            throw e;
        }
        
        int old_w = img.getWidth(null);
        int old_h = img.getHeight(null);
        if (old_w == -1) {
            return false;
        } else if (height <= 0 && width <= 0) {
            return false;
        } else {
            int new_w;
            int new_h;
            if (height <= 0) {
                new_w = width;
                new_h = (int) Math.round(1.0 * new_w * old_h / old_w);
            } else if (width <= 0) {
                new_h = height;
                new_w = (int) Math.round(1.0 * new_h * old_w / old_h);
            } else if (proportion == true) {//判断是否等比例缩放
                //计算比率
                double rate1 = 1.0 * old_w / width;
                double rate2 = 1.0 * old_h / height;
                if (rate1 > rate2) {
                    new_w = width;
                    new_h = (int) Math.round(1.0 * new_w * old_h / old_w);
                } else {
                    new_h = height;
                    new_w = (int) Math.round(1.0 * new_h * old_w / old_h);
                }
            } else {
                new_w = width;
                new_h = height;
            }            BufferedImage buffImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            Graphics g = buffImg.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, new_w, new_h);
            g.drawImage(img, 0, 0, new_w, new_h, null);
            g.dispose();
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
            try {
                encoder.encode(buffImg);
                tempout.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
        return true;
    }
}这个程序用来很多次感觉很好用的,可是现在有一张图片转换之后明显跟原图不一样了,不知道是怎么一回事,请各位高手指教!!!

解决方案 »

  1.   

    可能原因:这是一个png图片里面有透明像素
    你转成JPEG肯定没法保证一致了,JPEG不支持透明像素的
    改了下你的方法,你用这个再处理透明像素看看import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.image.BufferedImage;
    import java.io.File;import javax.imageio.ImageIO;public class ImageTool {    public static void main(String[] args) {
            try {
                ImageTool.resize("E:/1.png", "E:/2.png", 337, 0, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    /**
         * 用途:改变图片尺寸
         * 
         * @param oldUrl 原图片文件绝对路径(含文件名)
         * @param newUrl 目标图片文件绝对路径(含文件名)
         * @param width 目标图片文件宽度
         * @param height 目标图片文件高度
         * @param proportion 是否等比例缩放
         * @return
         * @throws Exception
         * 
         * 此方法支持的图片文件格式有:jpg、gif、png
         * 不支持的图片文件格式有:bmp
         * 其它图片文件格式未测试
         */
        public static boolean resize(String oldUrl, String newUrl, int width, int height, boolean proportion) throws Exception {
            File fileIn = new File(oldUrl);
            File fileOut = new File(newUrl);
            Image img = null;
            Applet app = new Applet();
            MediaTracker mt = new MediaTracker(app);
            try {
                img = javax.imageio.ImageIO.read(fileIn);
                mt.addImage(img, 0);
                mt.waitForID(0);
            } catch (Exception e) {
                throw e;
            }
            
            int old_w = img.getWidth(null);
            int old_h = img.getHeight(null);
            if (old_w == -1) {
                return false;
            } else if (height <= 0 && width <= 0) {
                return false;
            } else {
                int new_w;
                int new_h;
                if (height <= 0) {
                    new_w = width;
                    new_h = (int) Math.round(1.0 * new_w * old_h / old_w);
                } else if (width <= 0) {
                    new_h = height;
                    new_w = (int) Math.round(1.0 * new_h * old_w / old_h);
                } else if (proportion == true) {//判断是否等比例缩放
                    //计算比率
                    double rate1 = 1.0 * old_w / width;
                    double rate2 = 1.0 * old_h / height;
                    if (rate1 > rate2) {
                        new_w = width;
                        new_h = (int) Math.round(1.0 * new_w * old_h / old_w);
                    } else {
                        new_h = height;
                        new_w = (int) Math.round(1.0 * new_h * old_w / old_h);
                    }
                } else {
                    new_w = width;
                    new_h = height;
                }            BufferedImage buffImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics g = buffImg.createGraphics();
                g.drawImage(img, 0, 0, new_w, new_h, null);
                g.dispose();
                ImageIO.write(buffImg, "PNG", fileOut);
            }
            return true;
        }
    }