如何在Java中实现jpeg-lossess?
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);
会抛出异常:
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)
仅param.getCompressionTypes()
打印[JPEG]
。
使用JAI
(版本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");
同时抛出异常:
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)
你能写一个样品吗?
答案 0 :(得分:3)
答案 1 :(得分:1)
第一个代码段有一个拼写错误:
param.setCompressionType("JPEG-lS");
必须是
param.setCompressionType("JPEG-LS");
(全部大写)。 我认为这是正确的,应该有效。
要查询可能的类型,请使用param.getCompressionTypes()
(如评论中所述)。