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

    package com.test;
    
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import org.junit.Test;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     *图片压缩
     */
    public class ImageTest {
        
        private Image img;
        private int width;
        private int height;
    
        @Test
        public void test(){
            try{
                //压缩开始
                ImageTest imgCom = new ImageTest("D://1.png");
                imgCom.resizeFix(400, 400,"D://2.png");
                //压缩结束
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        /**
         * 构造函数
         * @param fileName 图片路径
         * @throws IOException
         */
        public ImageTest(String fileName) throws IOException {
            // 读入文件
            File file = new File(fileName);
            // 构造Image对象
            img = ImageIO.read(file);
            // 得到源图宽
            width = img.getWidth(null);
            // 得到源图长
            height = img.getHeight(null);
        }
    
        /**
         * 按照宽度还是高度进行压缩
         * @param w int 最大宽度
         * @param h  int 最大高度
         * @param filePath 图片路径
         * @throws IOException
         */
        public void resizeFix(int w, int h,String filePath) throws IOException {
            if (width / height > w / h) {
                resizeByWidth(w,filePath);
            } else {
                resizeByHeight(h,filePath);
            }
        }
    
        /**
         * 以宽度为基准,等比例放缩图片
         * @param w int 新宽度
         * @param filePath 图片路径
         * @throws IOException
         */
        public void resizeByWidth(int w,String filePath) throws IOException {
            int h = (int) (height * w / width);
            resize(w, h,filePath);
        }
    
        /**
         * 以高度为基准,等比例缩放图片
         * @param h int 新高度
         * @param filePath 图片路径
         * @throws IOException
         */
        public void resizeByHeight(int h,String filePath) throws IOException {
            int w = (int) (width * h / height);
            resize(w, h,filePath);
        }
    
        /**
         * 强制压缩/放大图片到固定的大小
         * @param w int 新宽度
         * @param h int 新高度
         * @param filePath
         * @throws IOException
         */
        public void resize(int w, int h,String filePath) throws IOException {
            // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
            BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
            // 绘制缩小后的图
            image.getGraphics().drawImage(img, 0, 0, w, h, null);
            File destFile = new File(filePath);
            // 输出到文件流
            FileOutputStream out = new FileOutputStream(destFile);
            // 可以正常实现bmp、png、gif转jpg
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            // JPEG编码
            encoder.encode(image);
            out.close();
        }
    }
    

      

  • 相关阅读:
    spring复杂数据类型传递
    spring表单—乱码解决方案
    spring重定向与转发
    线程在WPF中的使用
    xheditor编辑器上传截图图片抓取远程图片代码
    js window.open()弹出窗口参数说明及居中设置
    INFORMATION_SCHEMA.COLUMNS 查询表字段语句
    SQL 使用Cursor(游标)遍历结果集
    js禁止浏览器滚屏方法
    基础-ADO插入数据后返回自增ID @@IDENTITY
  • 原文地址:https://www.cnblogs.com/qinxu/p/8250781.html
Copyright © 2011-2022 走看看