0

I'm trying to embed a message inside an image which are provided individually in a jsp page and are sent as a multipart/form-data. It was successfully performed, got the image with data embedded in it and sent back to the jsp page but, when I use ImageIO.write() method, the size of an image increases to almost double or greater.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    ServletOutputStream sos = response.getOutputStream();
    
    Part filePart = request.getPart("img");
    
    try {
        
        BufferedImage image = embed(request.getParameter("msg"), filePart, request.getParameter("key"));
        
        ImageIO.write(image, "png", sos);
        
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    } finally {
        sos.close();
    }
}

If I try to use ImageWriter to compress, I will not be able to extract data from that.

    ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("png").next();
    ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
    jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpgWriteParam.setCompressionQuality(0.1f);
    ImageOutputStream outputStream = ImageIO.createImageOutputStream(sos);
    jpgWriter.setOutput(outputStream);
    IIOImage outputImage = new IIOImage(image, null, null);
    jpgWriter.write(null, outputImage, jpgWriteParam);
    jpgWriter.dispose();

Is there any way to get that buffered image by converting to other object and send to the jsp page so that ImageIO class will not be used. (Note: server sent image is collected using javascript).

The max size should only be (size of the image + size of embedded message).

But using ImageIO class, the size increases. Is there any way to send the file without any change in its size?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I guess you'll have to keep the original as a byte array. – Maurice Perry Nov 09 '21 at 06:59
  • Using `ImageIO` to read/write images doesn't make them larger per se, it's the change of compression algorithm from JPEG to PNG (deflate + predictor) that causes this. If using the same compression method with the same settings, then the size should also be roughly the same. I'm not sure what `embed` method does, is it a visible "watermark" thing, or a hidden message encoded in the pixel data? – Harald K Nov 09 '21 at 08:51
  • @HaraldK **embed** method takes message, key and image as parameters which are received from jsp page and using the key, pseudo random numbers (i.e. random pixels) are generated to store the bits of message inside the image. What does "_the change of compression algorithm from JPEG to PNG (deflate + predictor) that causes this_" mean? I have used both png and jpeg, the result's size is double or more mostly. I have also said that compression will not give me the right output. So, I have asked if there is any other method. – Vinay Kumar P.V. Nov 09 '21 at 13:40
  • 1
    I'm saying it's the change of compression algorithm (dictated by the image format you chose) that cause the growth in size (not which API you used to compress it). Your "compression" code contains `ImageIO.getImageWritersByFormatName("png")`, this is still PNG format, no matter how much you name the variables "jpg"-something... Anyway, to do what you want, using JPEG, you need to hook into the process *after* the lossy part, or you'll... lose information. But this is a completely different problem. See for example [this answer](https://stackoverflow.com/q/20863721/1428606). – Harald K Nov 09 '21 at 13:53
  • @HaraldK But I am saying that the problem arises for png also. – Vinay Kumar P.V. Nov 09 '21 at 13:57
  • 1
    Yes. Exactly, that is expected. With PNG the size will grow (in most cases). With JPEG the size may grow *or shrink* (depending on the compression setting). But JPEG is in any case *lossy*. So you can't put information into the pixels before this lossy process and expect it to come back the same. Thus, see the link in the previous comment (or accept the growth in size). – Harald K Nov 09 '21 at 14:09

0 Answers0