zoukankan      html  css  js  c++  java
  • 图片压缩成指定大小

    不用截取图片,按指定大小截取。

    package com.boco.vnms.common.util;

    import java.awt.Color; 
    import java.awt.Graphics2D; 
    import java.awt.Image; 
    import java.awt.image.BufferedImage; 
    import java.awt.image.ConvolveOp; 
    import java.awt.image.Kernel; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.InputStream; 
    import org.apache.commons.fileupload.FileItem; 
    import org.apache.log4j.Logger;     
    import com.sun.image.codec.jpeg.JPEGCodec; 
    import com.sun.image.codec.jpeg.JPEGImageEncoder; 

    public class ImageUtil { 
        /** 
         * Logger for this class 
         */
        private static final Logger logger = Logger.getLogger(ImageUtil.class);     
        public static void imageScale(String srcFilePath, String targetFilePath, 
                int width, int height) { 
             imageScale(new File(srcFilePath), new File(targetFilePath), width, 
                    height); 
        } 
        public static void imageScale(File srcFile, File targetFile, int width, int height) { 
            try { 
                Image image = javax.imageio.ImageIO.read(srcFile); 
                image = image.getScaledInstance(width, height, 
                        Image.SCALE_AREA_AVERAGING); 
                // Make a BufferedImage from the Image. 
                BufferedImage mBufferedImage = new BufferedImage(width, height, 
                        BufferedImage.TYPE_INT_RGB); 
                Graphics2D g2 = mBufferedImage.createGraphics();             
                g2.drawImage(image, 0, 0, width, height, Color.white, null); 
                g2.dispose(); 
                float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, 
                        -0.125f, -0.125f, -0.125f, -0.125f }; 
                Kernel kernel = new Kernel(3, 3, kernelData2); 
               ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 
                mBufferedImage = cOp.filter(mBufferedImage, null); 
      
               File targetDir = targetFile.getParentFile(); 
                if (!targetDir.exists()) 
                    targetDir.mkdirs(); 
     
                FileOutputStream out = new FileOutputStream(targetFile); 
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
                encoder.encode(mBufferedImage); 
                out.close(); 
            } catch (Exception e) { 
               logger.error( 
                        "imageScale(String, String, int, int) - 图片压缩出错 - srcFilePath="
                                + srcFile.getPath() + ", targeFilePath="
                               + targetFile.getPath() + ", width=" + width 
                               + ", height=" + height, e); 
            } 
        } 
      
       public void imageScale(FileItem fileItem, String targetFilePath, int width, 
                int height) { 
            try { 
                InputStream input = fileItem.getInputStream(); 
                Image image = javax.imageio.ImageIO.read(input); 
      
                image = image.getScaledInstance(width, height, 
                        Image.SCALE_AREA_AVERAGING); 
                BufferedImage mBufferedImage = new BufferedImage(width, height, 
                        BufferedImage.TYPE_INT_RGB); 
                Graphics2D g2 = mBufferedImage.createGraphics(); 
      
                g2.drawImage(image, 0, 0, width, height, Color.white, null); 
                g2.dispose(); 
      
                float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, 
                        -0.125f, -0.125f, -0.125f, -0.125f }; 
                Kernel kernel = new Kernel(3, 3, kernelData2); 
                ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 
                mBufferedImage = cOp.filter(mBufferedImage, null); 
      
                File target = new File(targetFilePath); 
                File targetDir = target.getParentFile(); 
                if (!targetDir.exists()) 
                    targetDir.mkdirs(); 
      
                FileOutputStream out = new FileOutputStream(targetFilePath); 
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
                encoder.encode(mBufferedImage); 
                out.close(); 
                input.close(); 
            } catch (Exception e) { 
                logger.error( 
                        "imageScale(String, String, int, int) - 图片压缩出错 - fileItem="
                               + fileItem.getName() + ", targetFilePath="
                          + targetFilePath + ", width=" + width + ", height="
                         + height, e); 
       } 
      } 

    public static void main(String[] args) {
       imageScale("D:/xx.jpg", "D:/xx.jpg",200, 100);
    }
    }

  • 相关阅读:
    .NET ------- 根据关键字查询后,点击详细页面对关键字标红
    .NET ------ 禁止文本输入的三种方式
    .NET ---- 借助repeater在行中进行下拉框编辑 (前端赋值给下拉框)
    Android ------ Android Studio 生成 apk 文件
    CentOS 8 Stream 简单的网络配置
    最受欢迎的 10 本编程书籍(文末附地址)
    优秀程序员必须掌握的 8 种通用数据结构
    一次阿里 P7 的面经,分享给大家
    如何在 Windows 上运行 Linux? 这里有一份攻略!
    为啥程序员下班后从不关电脑?
  • 原文地址:https://www.cnblogs.com/rmsSpring/p/imageCut.html
Copyright © 2011-2022 走看看