user52 user52 - 28 days ago 11
Java Question

Converting images from .bmp to jpeg2000 on java and eclipse error

I have seen some examples here and installed Java Advanced Imaging Image I/O Tools on my computer, because obviously it is a requirement of processing JPEG2000 images.

After install this i am able to import libraries

e.g.


import com.sun.media.imageio.plugins.*;


after importing, i should be able to use constructors or methods of that library but i am getting this error:

"Access restriction: The type 'J2KImageWriteParam' is not API (restriction on required library 'C:\Program Files (x86)\Java\jre1.8.0_77\lib\ext\jai_imageio.jar')"

After a litle bit research, i found out that i can change eclipse preferences and ignore that error.

I went through this way: Preferences -> Java -> Compiler -> Errors / Warnings -> Deprecated and Restricted API. Then i changed errors to warnings. But now i can not use that library efficient, cause eclipse suggest me nothing about that library.

My first question is; if there is a better way to do that? Or maybe another way to use this library efficient in eclipse?

EDIT: I found out it was a complication of 32 and 64 bit versions. After installing 32bit JDK and reference the jai_imageio.jar it worked fine.

And second; Can anybody give a plain example to me about converting .bmp image to jpeg2000 image. That would help a lot to me about undesrtanding the context.

Thank you

Answer

You need to have these in your imports:

     import javax.imageio.*;
     import javax.imageio.stream.*;

     import com.sun.media.imageio.plugins.jpeg2000.*;
     import com.sun.media.imageio.stream.*;

and these jars

jai_imageio.jar;jai_codec.jar;jai_core.jar 

This is an example that runs fine for me - but dont know if the produced j2k are valid or anything - use your j2000 viewer to check that.

 public void toJ2000(String inputFile, String outputFile) throws IOException {
J2KImageWriteParam iwp = new J2KImageWriteParam();
FileInputStream fis = new FileInputStream(new File(inputFile));
BufferedImage image = ImageIO.read(fis);
fis.close();

if (image == null) 
{
    System.out.println("If no registered ImageReader claims to be able to read the resulting stream");
}

Iterator writers = ImageIO.getImageWritersByFormatName("JPEG2000");
String name = null;
ImageWriter writer = null;
while (name != "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriter") {
    writer = (ImageWriter) writers.next();
    name = writer.getClass().getName();
    System.out.println(name);
}
File f = new File(outputFile);
long s = System.currentTimeMillis();
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
J2KImageWriteParam param = (J2KImageWriteParam) writer.getDefaultWriteParam();
IIOImage ioimage = new IIOImage(image, null, null);

writer.write(null, ioimage, param);
System.out.println(System.currentTimeMillis() - s);
writer.dispose();
ios.flush();
ios.close();

image.flush();
}

  public static void main(String[] args) {
    TR t=new TR();
    try {
       t.toJ2000("yel.png", "yel.j2k");
    }
    catch (Exception ex) { ex.printStackTrace(); }
  }