中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java生成高清縮略圖

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

可以把圖片縮小到理想的倍數(shù),也可以根據(jù)自己的需要來具體規(guī)定圖片轉(zhuǎn)化后的大小對于類型為jpg的圖片來說,只需要三個參數(shù)就能轉(zhuǎn)化得到自己想要的圖片參數(shù)1存放圖片的文件夾參數(shù)2 輸出處理后的圖片的文件夾參數(shù)3 需要轉(zhuǎn)化的倍數(shù)

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   需要縮小的倍數(shù),縮小2倍為原來的1/2 ,這個數(shù)值越大,返回的圖片越小
     * @return              返回處理后的圖像
     */
    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {
        /*原始圖像的寬度和高度*/
        int width = im.getWidth();
        int height = im.getHeight();

        /*調(diào)整后的圖片的寬度和高度*/
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);

        /*新生成結(jié)果圖片*/
        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   倍數(shù),比如0.5就是縮小一半,0.98等等double類型
     * @return              返回處理后的圖像
     */
    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
        /*原始圖像的寬度和高度*/
        int width = im.getWidth();
        int height = im.getHeight();

        /*調(diào)整后的圖片的寬度和高度*/
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);

        /*新生成結(jié)果圖片*/
        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  要轉(zhuǎn)化的圖像的文件夾,就是存放圖像的文件夾路徑
     * @param type  圖片的后綴名組成的數(shù)組
     * @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);
            /* 壓縮質(zhì)量 */
            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\\";  
        /*這兒填寫你轉(zhuǎn)化后的圖片存放的文件夾*/
        float times = 0.5f; 
        /*這個參數(shù)是要轉(zhuǎn)化成的倍數(shù),如果是1就是轉(zhuǎn)化成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);
        }
    }
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java獲得硬盤和主板的序列號代碼

下一篇:Java獲取某年某周的第一天