zoukankan      html  css  js  c++  java
  • java 批量压缩图片

    将某个文件夹下面的所有图片复制压缩到指定的文件夹

    package com.dayu.test;
    
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;
    
    public class Img1 {
      //目标文件夹
        private static final String inputPath = "C:\Users\Administrator\Desktop\ssss\";
      //输出文件夹
    private static final String outputPath = "C:\Users\Administrator\Desktop\tempImage\"; private Image img; private int width; private int height; public Img1(String filename) throws IOException { File file = new File(filename); img = ImageIO.read(file); width = img.getWidth(null); height = img.getHeight(null); } public static void main(String[] ags) throws IOException { System.out.println(new Date()); readFile(inputPath); System.out.println(new Date()); } private static void readFile(String inputPath) throws IOException { File file = new File(inputPath); if (file.isDirectory()) { System.out.println("文件夹名称:" + file.getName()); String[] fileList = file.list(); for (String s : fileList) { System.out.println(file.getName() + "下的图片名称:" + s); String newFilePath = inputPath + File.separator + s; File file1 = new File(newFilePath); if (file1.isDirectory()) { readFile(newFilePath); } else { //构建写如果图片 Img1 img1 = new Img1(newFilePath); img1.resize(1000, 1000, s); } } } } private void resize(int w, int h, String fileName) throws IOException { if (width / height > w / h) { //resizeByWidth(w); h = height * w / width; writeImage(w, h, fileName); } else { // resizeByHeight(h); w = width * h / height; writeImage(w, h, fileName); } } private void writeImage(int w, int h, String fileName) throws IOException { //生成图片的平滑度优先级比速度高,生成的图片质量好 BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
         //绘制缩小后的图 bufferedImage.getGraphics().drawImage(img,
    0, 0, w, h, null); //将文件夹下面的所有图片写到一个指定的文件夹下面 String pathname = outputPath + File.separator + "newFile"; File file = new File(pathname); if (!file.exists()) { file.mkdirs(); } File tempFile = new File(pathname + File.separator + fileName);
         //输出到文件流 FileOutputStream fos
    = new FileOutputStream(tempFile);
         //转码 JPEGImageEncoder encoder
    = JPEGCodec.createJPEGEncoder(fos);
         //使用JPEG转码 encoder.encode(bufferedImage);
         //关闭流 fos.close(); } }
  • 相关阅读:
    sql查询
    PHP常用的设计模式
    PHP内存管理和垃圾回收机制
    记一次面试
    获取py文件函数名及动态调用
    正确解决 mysql 导出文件 分隔符 问题
    解决ValueError: cannot convert float NaN to integer
    Python ---接口返回值中文编码问题
    pandas python 读取大文件
    【neo4J】后台关闭后,前端还能打开视图
  • 原文地址:https://www.cnblogs.com/dayu007/p/8968197.html
Copyright © 2011-2022 走看看