如何在 Java 中将任何格式的图像转换为压缩的 WebP 字节数组?

问题描述 投票:0回答:1

我正在使用下面的代码,但此代码在我的本地生成并保存图像,但我需要将该图像转换并处理为 WebP 字节数组,而不将图像保存在我的本地。我正在使用第 3 方库来压缩图像。

我使用的图书馆:

<dependency> 
    <groupId>org.sejda.imageio</groupId> 
    <artifactId>webp-imageio</artifactId> 
    <version>0.1.6</version> 
</dependency>



public static void main(String[] args) {    

String s = "https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg";
    
    try {
        URL url = new URL(s.replaceAll(" ", "%20"));
        InputStream is = null;
        try {
            is = url.openStream();
        } catch (IOException e) {
            return;
        }
         BufferedImage image = ImageIO.read(is);
         
          ImageWriter writer =           ImageIO.getImageWritersByMIMEType("image/webp").next();
         
          WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
          
          writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          //Set lossy compression
          writeParam.setCompressionType(writeParam.getCompressionTypes()              [WebPWriteParam.LOSSY_COMPRESSION]);

          //Set 80% quality.
          writeParam.setCompressionQuality(0.1f);

          // Save the image
          writer.setOutput(new FileImageOutputStream(new File("filePath")));

          writer.write(null, new IIOImage(image, null, null), writeParam);
          
        System.out.println("Complete");
        
        
    } catch (FileUploadException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

那么如何在不保存文件的情况下直接获取压缩后的WebP的字节数组呢?

java spring-boot image-processing webp image-conversion
1个回答
0
投票
public static void main(String[] args) {    

        String s = 
"https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg";

        URL url = new URL(s.replaceAll(" ", "%20"));
        InputStream is = null;
        try {
            is = url.openStream();
        } catch (IOException e) {
            return;
        }
        ByteArrayInputStream byteInputStrm = null;
        BufferedImage originalImage = ImageIO.read(is);
        ImageIO.write(originalImage, "jpg", byteOutStrm);
        byte[] orgImgByteArray = byteOutStrm.toByteArray();
        byteInputStrm = new ByteArrayInputStream(orgImgByteArray);
        ByteArrayOutputStream baos = null;
        ImageOutputStream imgOutStrm = null;
        try {
            BufferedImage image = ImageIO.read(byteInputStrm);
            ImageWriter writer = 
            ImageIO.getImageWritersByMIMEType("image/webp").next();
            baos = new ByteArrayOutputStream();
            imgOutStrm = ImageIO.createImageOutputStream(baos);
            writer.setOutput(imgOutStrm);
            WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
            writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            writeParam.setCompressionType(writeParam.getCompressionTypes() 
            [WebPWriteParam.LOSSY_COMPRESSION]);
            writeParam.setCompressionQuality(0.4f);   //set compression quality
            writer.write(null, new IIOImage(image, null, null), writeParam);
            imgOutStrm.close();
            byte[] byteArray = baos.toByteArray();//final Answer

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}
© www.soinside.com 2019 - 2024. All rights reserved.