javaspring-bootimage-processingwebpimage-conversion

How can I convert any format of image to compresed WebP byte array in Java?


I'm using below code but this code generate and save image in my localbut I need to convert and process that image into WebP byte Array without saving the image in my local. I'm using 3rd party Library to compress the image.

Library that I used :

<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();
    }
}

So how can I directly get byte array of compressed WebP without saving the file?


Solution

  • 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();
                }
            }
    
    }