当前位置:   article > 正文

JAVA 实现jpg/tif/bmp 等图片格式互相转换,解决RenderedOp资源不能释放

renderedop

需要jai_core.jar和jai_codec.jar包

实现格式转换,但此方法中的RenderedOp资源不能释放,生成之后原图不能删除;

  1. public class ImgCast {
  2. public static void main(String[] args) {
  3. /* tif转换到jpg格式 */
  4. try {
  5. String input2 = "F:/T20150120002001001.jpg";
  6. String output2 = "F:/T20150120002001001_01.jpg";
  7. RenderedOp src2 = JAI.create("fileload", input2);
  8. OutputStream os2 = new FileOutputStream(output2);
  9. JPEGEncodeParam param2 = new JPEGEncodeParam();
  10. //指定格式类型,jpg 属于 JPEG 类型
  11. ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2);
  12. enc2.encode(src2);
  13. os2.close();
  14. /*tif转换到bmp格式*/
  15. String inputFile = "F:/T20150120002001001.jpg";
  16. String outputFile = "F:/T20150120002001001_01.bmp";
  17. RenderedOp src = JAI.create("fileload", inputFile);
  18. OutputStream os = new FileOutputStream(outputFile);
  19. BMPEncodeParam param = new BMPEncodeParam();
  20. ImageEncoder enc = ImageCodec.createImageEncoder("BMP", os,param);
  21. enc.encode(src);
  22. os.close();//关闭流
  23. //其他的一样的方式转换 ...
  24. } catch (FileNotFoundException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }

RenderedOp src = JAI.create("fileload", inputFile); 这种方式去处理. 但最后我发现 SRC 的资源始终不能释放,即使我调用 src.dispose(),也一样不可以,也找不到 close flush等方法。 导致原图不能删除;

下面第一种情况是用上面的方法,但不能删除,第二种情况是另一种方法,没有资源不能释放被占用情况

第一种情况:(不能释放,删除)

  1. // 强制将图片转成JPEG
  2. String input = sourcePath + File.separator + fileName;
  3. String output = sourcePath + File.separator + fileName.replace(".", "_01.");
  4. File inputFile = new File(input);
  5. File outputFile = new File(output);
  6. //使用RenderedOp后src资源一直存在,input的文件始终删不掉
  7. RenderedOp src = JAI.create("fileload", input);
  8. OutputStream os = new FileOutputStream(output);
  9. JPEGEncodeParam param = new JPEGEncodeParam();
  10. ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os, param);
  11. src.dispose();
  12. src.removeSources();
  13. enc.encode(src);
  14. os.flush();
  15. os.close();
  16. src.removeSinks();

第二种情况:(解决不能释放,删除问题)

  1. // 强制将图片转成JPEG
  2. String input = sourcePath + File.separator + fileName;
  3. String output = sourcePath + File.separator + fileName.replace(".", "_01.");
  4. FileSeekableStream stream = new FileSeekableStream(input);
  5. PlanarImage in = JAI.create("stream", stream);
  6. OutputStream os = null;
  7. os = new FileOutputStream(output);
  8. JPEGEncodeParam param = new JPEGEncodeParam();
  9. ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os, param);
  10. try {
  11. enc.encode(in);
  12. os.flush();
  13. os.close();
  14. stream.close();
  15. } catch (IOException e) {
  16. Constant.bLog.error("JPEG error"+e.toString());
  17. }
  18. //删除源文件
  19. FileUtils.forceDelete(new File(input));
  20. //将新生成的文件名字改为原始文件名
  21. File sFile = new File(input);
  22. if(!sFile.exists()){
  23. File fDest = new File(output);
  24. fDest.renameTo(new File(input));
  25. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/715945
推荐阅读
相关标签
  

闽ICP备14008679号