美文网首页
图片压缩工具

图片压缩工具

作者: MeteorCat | 来源:发表于2021-01-10 16:15 被阅读0次

1.代码如下

public class  ImageZipUtil {

    /**
     * 根据设置的宽高等比例压缩图片文件<br> 先保存原文件,再压缩、上传
     * @param oldFile 要进行压缩的文件   * @param newFile 新文件
     * @param width 宽度 //设置宽度时(高度传入0,等比例缩放)
     * @param height 高度 //设置高度时(宽度传入0,等比例缩放)
     * @param quality 质量   * @return 返回压缩后的文件的全路径
     */
    public static boolean zipImageFile(InputStream oldFile,String srcImgPath, int width, int height,float quality) {
        boolean writeFlag = false;
        if (oldFile == null) {
            return writeFlag;
        }
        try {
            /** 对服务器上的临时文件进行处理 */
            Image srcFile = ImageIO.read(oldFile);
            int w = srcFile.getWidth(null);
            int h = srcFile.getHeight(null);
            double bili;      if(width>0){
                bili=width/(double)w;        height = (int) (h*bili);
            }else{
                if(height>0){
                    bili=height/(double)h;
                    width = (int) (w*bili);
                }
            }
            String subfix = "jpg";
            subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
            BufferedImage buffImg = null;
            if(subfix.equals("png")){
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            }else{
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            }      Graphics2D graphics = buffImg.createGraphics();
            graphics.setBackground(new Color(255,255,255));
            graphics.setColor(new Color(255,255,255));
            graphics.fillRect(0, 0, width, height);
            graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            writeFlag = ImageIO.write(buffImg, subfix, new File(srcImgPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writeFlag;
    }

}

相关文章

  • 图片压缩工具

    图片压缩工具: png optimizer PNGQuantImageMagickPNGGauntletPNGOu...

  • 实用工具和代码库

    1.pngquan---图片有损压缩工具[https://pngquant.org/]2.zip---文件压缩工具...

  • 图片压缩工具

    tinyjpg,tinypng 智图 smush.it(雅虎) 色彩笔 CSS Sprite图片合成 图片转base64

  • 图片压缩工具

    开场小故事 A:“.ipa包打好了吗?”B:“打好了!”A:“多大?”B:“108.1M”A:“太大了吧,能不能小...

  • 图片压缩工具

    方法一:图片画图打开,保存为JPEG格式。 方法二:https://tinypng.com/网站逐个压缩。

  • 图片压缩工具

    我一直用腾讯的微盘存储生活物品的照片和归类,苹果手机拍照后直接上传,非常方便明了。但存在一个问题,拍下来的照片往往...

  • 图片压缩工具

    1.代码如下

  • 有哪些免费在线压缩图片的方法或工具

    一、极速img 专业无损图片压缩工具 首先搜索极速img即可找到这款免费在线压缩工具,点击即可进入;根据页面提示点...

  • 工作工具/网站

    1.图片压缩工具 TinyPNG – Compress PNG images while preserving t...

  • 前端开发 一直受用

    https://tinypng.com =>图片压缩工具 http://huaban.com/pins/10779...

网友评论

      本文标题:图片压缩工具

      本文链接:https://www.haomeiwen.com/subject/alzcoqtx.html