How to achieve jpeg lossless?

与世无争的帅哥 提交于 2019-11-27 06:26:33

问题


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?


回答1:


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




回答2:


The first code snippet has a typo:

param.setCompressionType("JPEG-lS");

It has to be

param.setCompressionType("JPEG-LS");

(all upper case). I think it is otherwise correct and should work.

To query possible types, use param.getCompressionTypes() (as noted in a comment).



来源:https://stackoverflow.com/questions/17187880/how-to-achieve-jpeg-lossless

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!