jvm 替换过时的Sun软件包将Tiff转换为Jpeg

qlzsbp2j  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(123)

我们有一段旧代码,它仍然使用SunJAIApis从tiff文件创建jpeg

private File createJPEG(String tifFilePath){

    FileOutputStream fos = null;
    SeekableStream s = null;

    try {

      s = new FileSeekableStream(tifFilePath);
      TIFFDecodeParam param = null;
      ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
      RenderedImage op = dec.decodeAsRenderedImage(0);

      File jpgFile = new File(tifFilePath.replace("tif","jpg"));
      fos = new FileOutputStream(jpgFile);

      JPEGEncodeParam jpgparam = new JPEGEncodeParam();
      jpgparam.setQuality(67);

      ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
      en.encode(op);
      fos.flush();
    }catch (IOException ex){
      LOGGER.error(ex);
    }finally {
      if(fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if(s != null){
        try {
          s.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return null;
  }

现在,这在一个具有较新Java版本的系统中不再起作用,当运行此代码时,我得到错误noclassdeffounderror com/sun/image/codec/jpeg/jpegcodec
这些是导入:

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.TIFFEncodeParam;

现在我明白你不应该再使用com.sun软件包了,我得到这个错误是因为JPEGCodec在我们新系统中使用的Java运行时中不再存在了。但是我该如何替换这些导入呢?

bttbmeg0

bttbmeg01#

上面的代码应该很容易直接移植到ImageIO
我尽可能地保留了变量名,使其更容易理解,并重写为使用try-with-resources。我相信这应该可以工作:

private File createJPEG(String tifFilePath) {
    try (ImageInputStream s = ImageIO.createImageInputStream(new File(tifFilePath))) {
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("TIFF"); // Could also use ImageIO.getImageReaders(s) to support any input format
        if (!readers.hasNext()) {
            return null; // ...or throw new IllegalArgumentException or similar
        }

        ImageReader dec = readers.next();
        dec.setInput(s);

        // The above code doesn't seem to use param, but if needed that is also possible
        ImageReadParam param = null; // dec.getDefaultReadParam() or new TIFFImageReadParam()
        RenderedImage op = dec.readAsRenderedImage(0, param); // Could als use dec.read(0, param) in most cases 
        
        File jpgFile = new File(tifFilePath.replace("tif", "jpg"));

        try (ImageOutputStream fos = ImageIO.createImageOutputStream(jpgFile)) {
            JPEGImageWriteParam jpgparam = new JPEGImageWriteParam(Locale.getDefault());
            jpgparam.setCompressionMode(MODE_EXPLICIT);
            jpgparam.setCompressionQuality(0.67f); // You might want to tune this parameter to get same quality/compression ratio as JAI

            ImageWriter en = ImageIO.getImageWritersByFormatName("JPEG").next();
            en.setOutput(fos);
            en.write(null, new IIOImage(op, null, null), jpgparam);
        }
    } catch (IOException ex) {
        LOGGER.error(ex);
    }

    return null; // What the original code does, probably you want to return jpgFile?
}

相关问题