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).

1  
// quality = 0 to 100
2  
static byte[] toJPEGWithQuality(double quality, BufferedImage img) ctex {
3  
  // need to drop alpha channel for this to work
4  
  img = dropAlphaChannelFromBufferedImage(img);
5  
  
6  
  new ByteArrayOutputStream stream;
7  
  var jpgWriter = first(ImageIO.getImageWritersByFormatName("jpg"));
8  
  try {
9  
    var params = jpgWriter.getDefaultWriteParam();
10  
    params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
11  
    params.setCompressionQuality(toFloat(quality/100));
12  
  
13  
    jpgWriter.setOutput(new javax.imageio.stream.MemoryCacheImageOutputStream(stream));
14  
    jpgWriter.write(null, new IIOImage(img, null, null), params);
15  
  } finally {
16  
    jpgWriter.dispose();
17  
  }
18  
  
19  
  ret stream.toByteArray();
20  
}

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: 135 / 202
Version history: 5 change(s)
Referenced in: [show references]