我正在使用java壓縮圖像。如何將jpeg圖像轉換爲jpeg2000無損壓縮圖像?
我想壓縮jpeg2000中的圖像無損。
請建議解決方案。
File imageFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
File compressedImageFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\decompjai.jpg");
InputStream is = new FileInputStream(imageFile);
OutputStream os = new FileOutputStream(compressedImageFile);
BufferedImage image = ImageIO.read(is);
// get all image writers for JPG format
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jp2k");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
J2KImageWriter
J2KImageWriteParam jwp = (J2KImageWriteParam) writer.getDefaultWriteParam();
// J2KImageWriteParam jwp = new J2KImageWriteParam();
boolean lossless = true;
jwp.setLossless(lossless);
// jwp.setFilter(J2KImageWriteParam.FILTER_97);
if (!lossless) {
jwp.setEncodingRate(8.0/2);
}
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), jwp);
ios.flush();
ios.close();
} catch (IOException e) {
e.printStackTrace();
}
我正在嘗試上面的代碼。
爲什麼JPG2000而不是其他無損格式如PNG?並且以哪種格式獲得圖像?文件,下載,位圖...? – Gumbo 2014-09-04 09:33:41
對我來說這是一個需求。 – Sivam 2014-09-04 09:34:35
Jpeg圖像或PNG圖像。 – Sivam 2014-09-04 09:35:06