All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eworkcloud.web.util.ImageUtils Maven / Gradle / Ivy

There is a newer version: 2.5.8
Show newest version
package com.eworkcloud.web.util;

import com.luciad.imageio.webp.WebPReadParam;
import com.luciad.imageio.webp.WebPWriteParam;
import lombok.SneakyThrows;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public abstract class ImageUtils {

    private static BufferedImage colorConvert(BufferedImage image) throws Exception {
        if (image.getType() == BufferedImage.TYPE_CUSTOM) {
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_USHORT_565_RGB) {
            // TYPE_USHORT_565_RGB 不能转 WEBP
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_USHORT_555_RGB) {
            // TYPE_USHORT_555_RGB 不能转 WEBP
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            // TYPE_BYTE_GRAY 不能转 WEBP
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_USHORT_GRAY) {
            // TYPE_USHORT_GRAY 不能转 WEBP
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_BYTE_BINARY) {
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else if (image.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
            return Thumbnails.of(image).imageType(BufferedImage.TYPE_INT_ARGB)
                    .scale(1.0).asBufferedImage();
        } else {
            return image;
        }
    }

    /**
     * 编码WebP格式
     *
     * @param input 图片流
     * @return 字节流
     */
    @SneakyThrows
    public static byte[] webpEncode(BufferedImage input) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
        writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);

        ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);
        writer.setOutput(imageOutputStream);

        BufferedImage image = colorConvert(input);
        writer.write(null, new IIOImage(image, null, null), writeParam);
        // 必须关闭,否则无字节流
        imageOutputStream.close();
        return outputStream.toByteArray();
    }

    /**
     * 编码WebP格式
     *
     * @param input 输入流
     * @return 字节流
     */
    @SneakyThrows
    public static byte[] webpEncode(InputStream input) {
        return webpEncode(ImageIO.read(input));
    }

    /**
     * 解码WebP格式
     *
     * @param input  输入流
     * @param format 格式
     * @return 字节流
     */
    @SneakyThrows
    public static byte[] webpDecode(InputStream input, String format) {
        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);

        ImageInputStream imageInputStream = new MemoryCacheImageInputStream(input);
        reader.setInput(imageInputStream);

        BufferedImage image = reader.read(0, readParam);
        return toByteArray(image, format);
    }

    /**
     * 转换图片对象
     *
     * @param input  图片流
     * @param format 格式
     * @return 字节流
     */
    @SneakyThrows
    public static byte[] toByteArray(BufferedImage input, String format) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(input, format, outputStream);
        return outputStream.toByteArray();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy