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

    使用java对图片进行缩放,

    pom依赖

            <!-- 照片处理 -->
            <dependency>
                <groupId>com.mortennobel</groupId>
                <artifactId>java-image-scaling</artifactId>
                <version>0.8.6</version>
            </dependency>


    java范例代码

    package com.mypackage;
    
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URI;
    
    import javax.imageio.ImageIO;
    import javax.imageio.stream.ImageOutputStream;
    
    import com.mortennobel.imagescaling.DimensionConstrain;
    import com.mortennobel.imagescaling.ResampleOp;
    
    
    public class ImageUtil {
    
        /**
         * 默认缩放比例
         */
        public final static int SCALE = 10;
    
        /**
         * 默认缩放后图片宽度   
         */
        //public final static int WIDTH = ApplicationConfig.getInstance().getThumbnailWidth();
        public final static int WIDTH = 100;
    
        /**
         * 默认缩放后图片高度
         */
        //public final static int HEIGHT = ApplicationConfig.getInstance().getThumbnailHeight();
        public final static int HEIGHT = 100;
    
        /**
         * gif
         */
        public final static String GIF = "gif";
    
        /**
         * png
         */
        public final static String PNG = "png";
    
    
        public static byte[] getImageByUrl(URI uri) {
            File file = new File(uri);
            if (file.exists() && file.isFile()) {
                return file2byte(file);
            }
            return null;
        }
    
        public static byte[] getImageByPath(String path) {
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                return file2byte(file);
            }
            return null;
        }
    
        // public static saveImage
        public static byte[] file2byte(File file) {
            byte[] buffer = null;
            try {
                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] b = new byte[1024];
                int n;
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                fis.close();
                bos.close();
                buffer = bos.toByteArray();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return buffer;
        }
    
        /**
         * 按默认比例缩放图片。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @return 缩略图文件流
         */
        public static InputStream getThumbnailByDefaultScale(InputStream src,
                String fileSuffix) throws Exception {
    
            return getThumbnailByScale(src, fileSuffix, SCALE);
    
        }
    
        /**
         * 按比例缩放图片。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @param scale 图片缩放比例
         * @return 缩略图文件流
         */
        public static InputStream getThumbnailByScale(InputStream src,
                String fileSuffix, int scale) throws Exception {
    
            if(GIF.equals(fileSuffix.toLowerCase())) {
                return src;
            }
            InputStream is = null;
            try {
                BufferedImage image = ImageIO.read(src);
                int width = image.getWidth(null);
                int height = image.getHeight(null);
                int w = width / scale;
                int h = height / scale;
    
                ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(w, h, true));
                BufferedImage to = rsop.filter(image, null);
    
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(to, fileSuffix, imOut);
    
                is = new ByteArrayInputStream(bs.toByteArray());
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            }
            return is;
        }
    
        /**
         * 按默认宽度高度缩放。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @return 缩略图文件流
         */
        public static InputStream getThumbnail(InputStream src,
                String fileSuffix) throws Exception {
    
            return compressImage(src, fileSuffix, WIDTH, HEIGHT);
    
        }
        
        /**
         * 指定宽度高度缩放。<br>
         * 图片的实际宽高不足时返回null
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @param _width 缩略图宽度
         * @param _height 缩略图高度
         * @return 缩略图文件流
         */
        public static InputStream getThumbnail(InputStream src,
                String fileSuffix, int _width, int _height) throws Exception {
    
            if(GIF.equals(fileSuffix.toLowerCase())) {
                return src;
            }
            InputStream is = null;
            try {
                BufferedImage bi2 = ImageIO.read(src);
                //原图宽高
                int width = bi2.getWidth(null);
                int height = bi2.getHeight(null);
                if(width < _width || height < _height){
                    return null;
                }
                //缩放后宽高
                int newWidth = 0;
                int newHeight = 0;
                if(width <= _width && height <= _height) {
                    _width = width;
                    _height = height;
                }
                //计算按原图的横向纵向最大比例方向缩放
                //横向图片的场合
                if (width / _width > height / _height) {
                    newWidth = _width;
                    newHeight = _width * height / width;
                } else {
                    newHeight = _height;
                    newWidth = _height * width / height;
                }
    
                ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true));
                BufferedImage to = rsop.filter(bi2, null);
    
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(to, fileSuffix, imOut);
    
                is = new ByteArrayInputStream(bs.toByteArray());
    
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            } finally{
                //关于原来的流
                if(src != null){
                    src.close();
                }
            }
            return is;
    
        }
    
        /**
         * 根据原图压缩为系统允许的最大的尺寸的图片。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @return 缩略图文件流
         */
        public static InputStream compressMaxImage(InputStream src,
                String fileSuffix, int maxWidth) throws Exception {
    
            return compressImage(src, fileSuffix, maxWidth);
    
        }
    
        /**
         * 固定图片宽高缩放(按原图的横向纵向最大比例方向缩放)。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @param _width 缩略图宽度
         * @return 缩略图文件流
         */
        public static InputStream compressImage(InputStream src,
                String fileSuffix, int _width, int _height) throws Exception {
    
            if(GIF.equals(fileSuffix.toLowerCase())) {
                return src;
            }
            InputStream is = null;
            try {
                BufferedImage bi2 = ImageIO.read(src);
                //原图宽高
                int width = bi2.getWidth(null);
                int height = bi2.getHeight(null);
                //缩放后宽高
                int newWidth = 0;
                int newHeight = 0;
                if(width <= _width && height <= _height) {
                    _width = width;
                    _height = height;
                }
                //计算按原图的横向纵向最大比例方向缩放
                //横向图片的场合
                if (width / _width > height / _height) {
                    newWidth = _width;
                    newHeight = _width * height / width;
                } else {
                    newHeight = _height;
                    newWidth = _height * width / height;
                }
    
                ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true));
                BufferedImage to = rsop.filter(bi2, null);
    
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(to, fileSuffix, imOut);
    
                is = new ByteArrayInputStream(bs.toByteArray());
                //关于原来的流
                if(src != null){
                    src.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            }
            return is;
    
        }
    
        /**
         * 固定图片宽度缩放。
         *
         * @param src 源图片文件流
         * @param fileSuffix 后缀名
         * @param _width 缩略图宽度
         * @return 缩略图文件流
         */
        public static InputStream compressImage(InputStream src,
                String fileSuffix, int _width) throws Exception {
    
            if(GIF.equals(fileSuffix.toLowerCase())) {
                return src;
            }
            InputStream is = null;
            try {
                BufferedImage bi2 = ImageIO.read(src);
                //原图宽高
                int width = bi2.getWidth(null);
                int height = bi2.getHeight(null);
                //缩放后宽高
                int newWidth = 0;
                int newHeight = 0;
                if(width < _width) {
                    _width = width;
                }
                newWidth = _width;
                //计算按原图的横向纵向最大比例方向缩放
                //横向图片的场合
                newHeight = height * _width / width;
    
                ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true));
                BufferedImage to = rsop.filter(bi2, null);
    
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(to, fileSuffix, imOut);
    
                is = new ByteArrayInputStream(bs.toByteArray());
                //关于原来的流
                if(src != null){
                    src.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            }
            return is;
    
        }
    
    }
  • 相关阅读:
    表的创建与管理
    以传值和传引用的方式传递参数 IN OUT NOCOPY
    PLSQL中的三种参数模式IN、OUT、IN OUT
    用python写GPU上的并行计算程序,有什么库或者编译器?
    cupy中tensor数据类型与numpy以及pytorch中相互转换
    c++ string split
    Java 读取大文件
    Linux 使用系列
    安装以太坊环境
    服务器排查问题相关命令
  • 原文地址:https://www.cnblogs.com/zhao1949/p/6508378.html
Copyright © 2011-2022 走看看