How to achieve jpeg-lossess in Java?
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("JPEG-LS").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG-LS");
writer.setOutput(ImageIO.createImageOutputStream(new File("C:\\Users\\RileyRen\\Desktop\\123.jpg")));
writer.write(null, new IIOImage(subBufferedImage, null, null), param);
Will throw an exception:
Exception in thread "main"
java.lang.IllegalArgumentException: Unknown compression type!
at javax.imageio.ImageWriteParam.setCompressionType(ImageWriteParam.java:1041)
at com.demandforce.ImageCrop.main(ImageCrop.java:59)
The param.getCompressionTypes()
only print [JPEG]
.
using JAI
(version 1.1.3):
PlanarImage input = JAI.create("fileload", "C:\\Users\\RileyRen\\Desktop\\test.jpg");
ParameterBlock pb = new ParameterBlock();
pb.addSource(input);
pb.add(x);
pb.add(y);
pb.add(width);
pb.add(height);
PlanarImage output = JAI.create("crop",pb,null);
JAI.create("filestore",output,"C:\\Users\\RileyRen\\Desktop\\123.jpg","JPEG-LS");
Also throw exception:
Exception in thread "main"
java.lang.IllegalArgumentException: FileStore The specified format has no associated registered ImageCodec.
at javax.media.jai.JAI.createNS(JAI.java:1087)
at javax.media.jai.JAI.create(JAI.java:973)
at javax.media.jai.JAI.create(JAI.java:1621)
at com.demandforce.ImageCrop.main(ImageCrop.java:103)
Can you write an sample please?
The first code snippet has a typo:
It has to be
(all upper case). I think it is otherwise correct and should work.
To query possible types, use
param.getCompressionTypes()
(as noted in a comment).Standard java does not have a compression type for JPEG-LS.
You can to download and use the JAI (Java Advanced Imaging) API though which I beleive includes such a compression type.
Can be downloaded from here