Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

< > BotCompany Repo | #1035888 // toJPEGWithQuality

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9193L/51K).

// quality = 0 to 100
static byte[] toJPEGWithQuality(double quality, BufferedImage img) ctex {
  // need to drop alpha channel for this to work
  img = dropAlphaChannelFromBufferedImage(img);
  
  new ByteArrayOutputStream stream;
  var jpgWriter = first(ImageIO.getImageWritersByFormatName("jpg"));
  try {
    var params = jpgWriter.getDefaultWriteParam();
    params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    params.setCompressionQuality(toFloat(quality/100));
  
    jpgWriter.setOutput(new javax.imageio.stream.MemoryCacheImageOutputStream(stream));
    jpgWriter.write(null, new IIOImage(img, null, null), params);
  } finally {
    jpgWriter.dispose();
  }
  
  ret stream.toByteArray();
}

Author comment

Began life as a copy of #1007587

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1035888
Snippet name: toJPEGWithQuality
Eternal ID of this version: #1035888/6
Text MD5: 1f1ebc67ecb888c6425d92c6c43cdfa3
Transpilation MD5: 4beac9c9eb9582a2808e260387be6a9e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-07 01:18:44
Source code size: 729 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 69 / 115
Version history: 5 change(s)
Referenced in: [show references]