文章标题

来源:互联网 发布:镜仙网络电影 编辑:程序博客网 时间:2024/06/11 02:19

使用ImageIO存储压缩图片

import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.IIOImage;import javax.imageio.ImageIO;import javax.imageio.ImageTypeSpecifier;import javax.imageio.ImageWriter;import javax.imageio.metadata.IIOMetadata;import javax.imageio.plugins.jpeg.JPEGImageWriteParam;import javax.imageio.stream.ImageOutputStream;import org.w3c.dom.Element;    public static void main(String[] args) {        String srcFilePath = "";        String destFilePath = "";        String name = "55fa1022357369855cb0a6048398a813.jpeg";        saveThumbImg(srcFilePath, destFilePath, name, 200, 200, 0.5f);    }    /**     * @param srcFilePath      *          图片所在的文件夹路径     * @param destFilePath      *          存放路径     * @param name      *          图片名称     * @param w     *          目标宽度     * @param h      *          目标高度     * @param pre      *          缩放百分比     */    public static void saveThumbImg(String srcFilePath, String destFilePath,String name, int w, int h, float pre){        Image src ;        try {            //构造Image对象            src = ImageIO.read(new File(srcFilePath + File.separator + name));            String img_ThumbName = destFilePath + File.separator + "c_" + name;            //获取图片高宽            int old_w = src.getWidth(null);            int old_h = src.getHeight(null);            //设置新图长宽            int new_w = 0;            int new_h = 0;            double w2 = (old_w*1.00)/(w*1.00);            double h2 = (old_h*1.00)/(h*1.00);            //根据长宽留白,形成一个正方形            BufferedImage oldImg;            if(old_w > old_h){                oldImg = new BufferedImage(old_w, old_w, BufferedImage.TYPE_INT_RGB);            } else if(old_h > old_w){                oldImg = new BufferedImage(old_h, old_h, BufferedImage.TYPE_INT_RGB);            } else {                oldImg = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB);            }            Graphics2D g = oldImg.createGraphics();            g.setColor(Color.white);            if(old_w > old_h){                g.fillRect(0, 0, old_w, old_w);                g.drawImage(src, 0, (old_w-old_h)/2, old_w, old_h, Color.white, null);            } else {                if(old_w<old_h){                    g.fillRect(0, 0, old_h, old_h);                    g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h, Color.white, null);                } else {                    g.drawImage(src.getScaledInstance(old_w, old_h, Image.SCALE_SMOOTH), 0, 0, null);                }            }            g.dispose();            src = oldImg;            if(old_w > w){                new_w = (int)Math.round(old_w/w2);            } else {                new_w = old_w;            }            if(old_h > h){                new_h = (int)Math.round(old_h/h2);            } else {                new_h = old_h;            }            BufferedImage image_to_save = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_BGR);            image_to_save.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null);            //输出到文件流            FileOutputStream fos = new FileOutputStream(img_ThumbName);            saveAsJPEG(100, image_to_save, pre, fos);            fos.close();            System.out.println("完成");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public static void saveAsJPEG(Integer dpi, BufferedImage image_to_save, float JPEGcompression, FileOutputStream fos) throws IOException{        ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);        imageWriter.setOutput(ios);        IIOMetadata imageMetadata = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image_to_save),null);        if(dpi != null && !dpi.equals("")){            Element tree = (Element)imageMetadata.getAsTree("javax_imageio_jpeg_image_1.0");            Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);            jfif.setAttribute("", Integer.toString(dpi));            jfif.setAttribute("Ydensity", Integer.toString(dpi));        }        if(JPEGcompression >= 0 && JPEGcompression <= 1f){            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam)imageWriter.getDefaultWriteParam();            jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);            jpegParams.setCompressionQuality(JPEGcompression);        }        imageWriter.write(imageMetadata, new IIOImage(image_to_save,null,null),null);        ios.close();        imageWriter.dispose();    }
原创粉丝点击