六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 73|回复: 0

java如何生成图片缩略图,缩小图片,高清图片缩小

[复制链接]

升级  4.67%

13

主题

13

主题

13

主题

秀才

Rank: 2

积分
57
 楼主| 发表于 2013-2-3 14:38:51 | 显示全部楼层 |阅读模式
可以把图片缩小到理想的倍数,也可以根据自己的需要来具体规定图片转化后的大小

对于类型为jpg的图片来说,只需要三个参数就能转化得到自己想要的图片

参数1 存放图片的文件夹
参数2 输出处理后的图片的文件夹
参数3 需要转化的倍数

直接上代码
import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.util.HashMap;import java.util.List;import java.util.ArrayList;import java.io.File;import java.io.IOException;import java.io.FileOutputStream;import java.util.Map;/** * Created by IntelliJ IDEA. * User: tsaowe * Date: 11-9-1 * Time: 下午2:12 */public class ResizeImage {    /**     * @param im            原始图像     * @param resizeTimes   需要缩小的倍数,缩小2倍为原来的1/2 ,这个数值越大,返回的图片越小     * @return              返回处理后的图像     */    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {        /*原始图像的宽度和高度*/        int width = im.getWidth();        int height = im.getHeight();        /*调整后的图片的宽度和高度*/        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);        /*新生成结果图片*/        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);        return result;    }        /**     * @param im            原始图像     * @param resizeTimes   倍数,比如0.5就是缩小一半,0.98等等double类型     * @return              返回处理后的图像     */    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {        /*原始图像的宽度和高度*/        int width = im.getWidth();        int height = im.getHeight();        /*调整后的图片的宽度和高度*/        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);        /*新生成结果图片*/        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);        return result;    }    /**     *     * @param path  要转化的图像的文件夹,就是存放图像的文件夹路径     * @param type  图片的后缀名组成的数组     * @return     */    public List<BufferedImage> getImageList(String path, String[] type) throws IOException{        Map<String,Boolean> map = new HashMap<String, Boolean>();        for(String s : type) {            map.put(s,true);        }        List<BufferedImage> result = new ArrayList<BufferedImage>();        File[] fileList = new File(path).listFiles();        for (File f : fileList) {            if(f.length() == 0)                continue;            if(map.get(getExtension(f.getName())) == null)                continue;            result.add(javax.imageio.ImageIO.read(f));        }        return result;    }    /**     * 把图片写到磁盘上     *     * @param im     * @param path     eg: C://home// 图片写入的文件夹地址     * @param fileName DCM1987.jpg  写入图片的名字     * @return     */    public boolean writeToDisk(BufferedImage im, String path, String fileName) {        File f = new File(path + fileName);        String fileType = getExtension(fileName);        if (fileType == null)            return false;        try {            ImageIO.write(im, fileType, f);            im.flush();            return true;        } catch (IOException e) {            return false;        }    }    public boolean writeHighQuality(BufferedImage im, String fileFullPath) {        try {            /*输出到文件流*/            FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);            /* 压缩质量 */            jep.setQuality(1f, true);            encoder.encode(im, jep);           /*近JPEG编码*/            newimage.close();            return true;        } catch (Exception e) {            return false;        }    }    /**     * 返回文件的文件后缀名     *     * @param fileName     * @return     */    public String getExtension(String fileName) {        try {            return fileName.split("\\.")[fileName.split("\\.").length - 1];        } catch (Exception e) {            return null;        }    }    public static void main(String[] args) throws Exception{        String inputFoler = "c:\\cameraImage" ; /*这儿填写你存放要缩小图片的文件夹全地址*/        String outputFolder = "c:\\output\\";  /*这儿填写你转化后的图片存放的文件夹*/        float times = 0.5f; /*这个参数是要转化成的倍数,如果是1就是转化成1倍*/        ResizeImage r = new ResizeImage();        List<BufferedImage> imageList = r.getImageList(inputFoler,new String[]{"jpg"});        for(BufferedImage i : imageList) {        r.writeHighQuality(r.zoomImage(i,times),outputFolder);}    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表