• Java压缩图片的实现类


    package com.function;
    
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    /*
     * 这是一个压缩图片的实现类
     */
    public class Tools_CompressPictures {
        public void compressPicture(String srcImage,String tarImage) {
            File srcImageFile = new File(srcImage);
            File tarImageFile = new File(tarImage);
            // 生成图片转化对象
            AffineTransform transform = new AffineTransform();
            // 通过缓存读入缓存对象
            BufferedImage image = null;
            try {
                image = ImageIO.read(srcImageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int imageWidth = image.getWidth();//原图片的高度
            int imageHeight = image.getHeight();//原图片的宽度
            int changeWidth = 640;//压缩后图片的高度
            int changeHeight = 480;//压缩后图片的宽度
            double scaleWidth = 0;// 定义小图片和原图片比例
            double scaleHeight = 0;
            scaleWidth = (double) changeWidth / (double) imageWidth;
            scaleHeight = (double) changeHeight / (double) imageHeight;
         // 生成转换比例
            transform.setToScale(scaleWidth, scaleHeight);
            // 生成转换操作对象
            AffineTransformOp transOp = new AffineTransformOp(transform, null);
            //生成压缩图片缓冲对象
            BufferedImage basll = new BufferedImage(changeWidth, changeHeight,
                    BufferedImage.TYPE_3BYTE_BGR);
            //生成缩小图片
            transOp.filter(image, basll);
            try {
                //输出缩小图片
                ImageIO.write(basll, "jpg",tarImageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
        }
    
    }
     


  • 相关阅读:
    jQuery Deferred和Promise的使用介绍:
    asp.net客户端IP跟踪
    jquery常用的一些方法
    前端音频流播放
    c# Http请求下载二进制流文件
    iView表格行验证问题
    【已解决】Https请求—未能创建 SSL/TLS 安全通道
    安全开发规范
    数据库设计规范
    高性能开发规范
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708644.html
Copyright © 2020-2023  润新知