java生成高清縮略圖

by57 9年前發布 | 1K 次閱讀 Java

可以把圖片縮小到理想的倍數,也可以根據自己的需要來具體規定圖片轉化后的大小對于類型為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;

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);
        }
    }
}

 本文由用戶 by57 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!