How to make jpeg lossless in java?

Is there anyone who can tell me how to write a jpeg file using lossless compression in java?

I read bytes using the following code to edit bytes

 WritableRaster raster = image.getRaster(); DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer(); 

And I need to write the bytes again as a jpeg file without compression in lossy .

+6
source share
1 answer

The JAI package offers the ability to save lossless JPEG formats. Set the compression type to JPEG-LS or JPEG-LOSSLESS , depending on which option you want.

I'm not sure what you really want lossless JPEG. This is a separate format that is not particularly relevant to the normal JPEG format. In general, it is not well supported; To store lossless images, you are usually better off with something like PNG.

If you need to perform lossless transcoding (i.e. a set of cropping and rotation operations that you can bypass without breaking the boundaries of the DCT matrices), you usually do this with the jpegtran , since there is currently no binding, as far as I know Java to the IJG library.

ETA:

You know how to do this with JAI.

I have not tried it myself (the code below is not verified), but this should be a direct call to setCompressionType . (Of course, right in Java still means negotiating with mazes of meandering little objects to establish what will be a simple switch elsewhere :)

 ImageWriter writer= (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param= writer.getDefaultWriteParam(); param.setCompressionMode(param.MODE_EXPLICIT); param.setCompressionType("JPEG-LS"); writer.setOutput(ImageIO.createImageOutputStream(new File(path))); writer.write(null, new IIOImage(image, null, null), param); 

JPEG-LS is the new lossless JPEG standard. It compresses more than the original JPEG-LOSSLESS standard, but support is even worse. Most JPEG-enabled applications cannot do anything with them.

+8
source

Source: https://habr.com/ru/post/898429/


All Articles