2

I am compressing a image using java.

I want to compress the image in jpeg2000 lossless.

Please suggest solution.

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();  
}  

I am trying with the above code.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
Sivam
  • 1,099
  • 3
  • 12
  • 25

2 Answers2

2

Check out this JPEG2000 Java Encoder/Decoder project. (you can find the source code here)

Gumbo
  • 1,716
  • 1
  • 15
  • 22
  • Is another example you know means please post. The above link i cannot able to find some fruitful examples. – Sivam Sep 04 '14 at 09:44
  • 1
    @Sivam You are expected to read the DOCUMENTATION yourself. SO is not a coding service. – Durandal Sep 04 '14 at 12:28
0

I'd first try javax.ImageIO as your system may have a provider for the JPEG2000 format.

Otherwise: http://www.deic.uab.cat/~francesc/ is apparently for java.

Alex
  • 24
  • 3