java中判斷圖片格式並且等比例壓縮圖片


最近項目中需要判斷上傳的圖片必須是png,jpg,gif三種格式的圖片,並且當圖片的寬度大於600px時,壓縮圖片至600px,並且等比例的壓縮圖片的高度。

具體的實現形式:

大致的思路是:

  1. 判斷根據文件名判斷圖片的格式是否是png,jpg,gif三種圖片文件   定義了 isImageFile 方法
  2. 如果是的,則進入到scaleImage(String imgSrc, String imgDist)方法中判斷圖片大小,如果圖片大小合適,則直接利用copyFile(File fromFile, File toFile)方法復制圖片
  3. 在縮放圖片中利用到java.awt里面的幾個類,並且利用BufferedImage可以加快圖片的壓縮速度。
package com.ctbri.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;


public class PictureChange {

    static String suffix = "";

    public static void main(String[] args) {
        String newfilebase = "E:/A_xia_program/image/";
        File file = new File(newfilebase + 1);
        File[] oldfiles = file.listFiles();
        for (File file2 : oldfiles) {
            if (isImageFile(file2)) {
                if (!scaleImage(newfilebase + 1 + "/" + file2.getName(), newfilebase + 11 + "/" + file2.getName())) {
                    System.out.println(file2.getName()+"轉化成功!");
                }
            }
        }
    }


    public static boolean isImageFile(File file) {

        String fileName = file.getName();

        //獲取文件名的后綴,可以將后綴定義為類變量,共后面的函數使用
        suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

        // 聲明圖片后綴名數組
        if (!suffix.matches("^[(jpg)|(png)|(gif)]+$")) {
            System.out.println("請輸入png,jpg,gif格式的圖片");
            return false;
        }
        return true;
    }

    public static boolean scaleImage(String imgSrc, String imgDist) {
        try {
            File file = new File(imgSrc);
            if (!file.exists()) {
                return false;
            }

            InputStream is = new FileInputStream(file);
            Image src = ImageIO.read(is);
            if (src.getWidth(null) <= 600) {
                File tofile = new File(imgDist);
                copyFile(file, tofile);
                is.close();
                return true;
            }

            //獲取源文件的寬高
            int imageWidth = ((BufferedImage) src).getWidth();
            int imageHeight = ((BufferedImage) src).getHeight();

            double scale = (double) 600 / imageWidth;

            //計算等比例壓縮之后的狂傲
            int newWidth = (int) (imageWidth * scale);
            int newHeight = (int) (imageHeight * scale);

            BufferedImage newImage = scaleImage((BufferedImage) src, scale, newWidth, newHeight);

            File file_out = new File(imgDist);
            ImageIO.write(newImage, suffix, file_out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    //用於具體的轉化
    public static BufferedImage scaleImage(BufferedImage bufferedImage, double scale, int width, int height) {
        int imageWidth = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale);
        AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

        return bilinearScaleOp.filter(bufferedImage, new BufferedImage(width, height, bufferedImage.getType()));
    }


    //復制文件
    public static void copyFile(File fromFile, File toFile) throws IOException {
        FileInputStream ins = new FileInputStream(fromFile);
        FileOutputStream out = new FileOutputStream(toFile);
        byte[] b = new byte[1024];
        int n = 0;
        while ((n = ins.read(b)) != -1) {
            out.write(b, 0, n);
        }
        ins.close();
        out.close();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM