leftso
89


前言

随着科技进步,大家手机拍的照片也从之前的几百KB变成几MB或者10MB了,有些情况我们需要压缩一下图片节省网络资源。这里将会讲解如何采用Java语音进行图片的无损压缩(有点不明显的)

Java无损压缩图片依赖包

        <!--图片压缩等操作-->
		<dependency>
			<groupId>net.coobird</groupId>
			<artifactId>thumbnailator</artifactId>
			<version>0.4.8</version>
		</dependency>

Java 图片无损压缩工具类代码

$title(ImageCompressUtil.java)
package net.xqlee.project.utils.image;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 图片压缩工具
 */
public class ImageCompressUtil {
    /**
     * 流方式输入输出图片压缩
     *
     * @param src     文件输入流(单个)
     * @param target  压缩后文件输出流(单个)
     * @param quality 压缩质量
     * @throws IOException
     */
    public static void compress(InputStream src, OutputStream target, float quality) throws IOException {
        Thumbnails.of(src).scale(1)
                .outputQuality(quality).toOutputStream(target);
    }

    /**
     * 流方式输入输出图片压缩
     *
     * @param src     文件输入流(单个)
     * @param target  压缩后文件输出流(单个)
     * @param quality 压缩质量
     * @throws IOException
     */
    public static void compress(InputStream src, OutputStream target, float quality, String format) throws IOException {
        Thumbnails.of(src).scale(1)
                .outputFormat(format)//输出格式
                .outputQuality(quality).toOutputStream(target);
    }

    /**
     * 压缩并重置大小(流式)
     *
     * @param src
     * @param target
     * @param quality
     * @param width
     * @param height
     * @throws IOException
     */
    public static void compressAndsize(InputStream src, OutputStream target, float quality, int width, int height) throws IOException {
        Thumbnails.of(src).size(width, height).keepAspectRatio(false).outputQuality(quality).toOutputStream(target);
    }

    /**
     * @param src
     * @param target
     * @param quality
     * @param width
     * @param height
     * @param format
     * @throws IOException
     */
    public static void compressAndsize(InputStream src, OutputStream target, float quality, int width, int height, String format) throws IOException {
        Thumbnails.of(src).size(width, height).keepAspectRatio(false)
                .outputFormat(format)//输出格式
                .outputQuality(quality).toOutputStream(target);
    }

    /**
     * 根据宽比例压缩(流式)
     *
     * @param src
     * @param target
     * @param quality
     * @param width
     * @throws IOException
     */
    public static void compressAndsizeWidth(InputStream src, OutputStream target, float quality, int width) throws IOException {
        Thumbnails.of(src).width(width).keepAspectRatio(true).outputQuality(quality).toOutputStream(target);
    }

    /**
     * 根据宽比例压缩(流式)
     *
     * @param src
     * @param target
     * @param quality
     * @param width
     * @throws IOException
     */
    public static void compressAndsizeWidth(InputStream src, OutputStream target, float quality, int width, String format) throws IOException {
        Thumbnails.of(src).width(width).keepAspectRatio(true)
                .outputFormat(format)//输出格式
                .outputQuality(quality).toOutputStream(target);
    }


    /**
     * 根据宽比例压缩(流式)
     *
     * @param src
     * @param target
     * @param quality
     * @param height
     * @throws IOException
     */
    public static void compressAndsizeHeight(InputStream src, OutputStream target, float quality, int height) throws IOException {
        Thumbnails.of(src).height(height).keepAspectRatio(true).outputQuality(quality).toOutputStream(target);
    }

    /**
     * 根据宽比例压缩(流式)
     *
     * @param src
     * @param target
     * @param quality
     * @param height
     * @throws IOException
     */
    public static void compressAndsizeHeight(InputStream src, OutputStream target, float quality, int height, String format) throws IOException {
        Thumbnails.of(src).height(height).keepAspectRatio(true)
                .outputFormat(format)//输出格式
                .outputQuality(quality).toOutputStream(target);
    }

    /**
     * 压缩并添加图片水印(流式)
     *
     * @param src      原图
     * @param target   输出图
     * @param mark     水印图片
     * @param position 水印位置
     * @param opacity  水印透明度 0.0f-1.0f
     * @param quality  图片质量 0-1
     * @throws IOException
     */
    public static void compressAndWatermark(InputStream src, OutputStream target, InputStream mark, Positions position, float opacity, float quality) throws IOException {
        Thumbnails.of(src)
                .scale(1)
                .watermark(position, ImageIO.read(mark), opacity)
                .outputQuality(quality)
                .toOutputStream(target);
    }

    /**
     * 压缩并添加图片水印(流式)
     *
     * @param src      原图
     * @param target   输出图
     * @param mark     水印图片
     * @param position 水印位置
     * @param opacity  水印透明度 0.0f-1.0f
     * @param quality  图片质量 0-1
     * @throws IOException
     */
    public static void compressAndWatermark(InputStream src, OutputStream target, InputStream mark, Positions position, float opacity, float quality, String format) throws IOException {
        Thumbnails.of(src).scale(1)
                .watermark(position, ImageIO.read(mark), opacity)
                .outputFormat(format)//输出格式
                .outputQuality(quality).toOutputStream(target);
    }

    public static void main(String[] args) {
        try {
//                compress("D:\\workplace\\IMG_20181024_105224.jpg","D:\\workplace\\IMG_20181024_105224-zp.jpg",0.8f);
//             compressAndFormat("D:\\workplace\\IMG_20181024_105224.jpg","D:\\workplace\\IMG_20181024_105224-zp.png","png",0.8f);
//            compressAndsize("D:\\workplace\\IMG_20181024_105224.jpg", "D:\\workplace\\IMG_20181024_105224-zp-size.jpg", 0.8f, 1200, 400);
//            FileInputStream src=new FileInputStream(new File("D:\\test.jpg"));

//            InputStream icon=new FileInputStream(new File("d:\\icon.png"));

//            OutputStream target=new FileOutputStream(new File("D:\\test-size.jpg"));

//            compressAndsize(src, target, 0.9f, 484, 300,"jpg");

//            compressAndWatermark(src,target,icon,Positions.BOTTOM_RIGHT,1,0.8f);
//            src.close();
//            icon.close();
//            target.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上代码就是采用Google的一个工具进行图片的压缩,效果还是非常不错的。注意png压缩不明显,jpg图片压缩大小效果明显一些。
收工阴脸
 
暂无评论