图片格式转换public void toJPG(String source, String dest, int quality) { if (dest == null || dest.trim().equals("")) dest = source; if (!dest.toLowerCase().trim().endsWith("jpg")) { dest += ".jpg"; System.out.println("Overriding to JPG, output file: " + dest); } if (quali...
public void toJPG(String source, String dest, int quality) { if (dest == null || dest.trim().equals("")) dest = source; if (!dest.toLowerCase().trim().endsWith("jpg")) { dest += ".jpg"; System.out.println("Overriding to JPG, output file: " + dest); } if (quality < 0 || quality > 100 || (quality + "") == null || (quality + "").equals("")) { System.out.println("quality must between ’0’ and ’100’"); System.out.println("set to DEFAULT value:’75’"); quality = 75; } try { JPGOptions options = new JPGOptions(); options.setQuality(quality); ImageProducer image = Jimi.getImageProducer(source); JimiWriter writer = Jimi.createJimiWriter(dest); writer.setSource(image); // 加入属性设置,非必要 // /* writer.setOptions(options); // */ writer.putImage(dest); } catch (JimiException je) { System.err.println("Error: " + je); } } 在进行格式转换时,并不需要关心原图的格式,只要要转换的图片格式在jimi的解码格式范围内,就可以完全透明的进行decode过程: ImageProducer image = Jimi.getImageProducer(source); 实际的解码只需要以行代码,就这么简单。 编码过程最简单的也只需要很少的代码: JimiWriter writer = Jimi.createJimiWriter(dest); writer.setSource(image); // 加入属性设置,非必要 // /* writer.setOptions(options); // */ writer.putImage(dest); 以上代码就完成了图片编码到输出到os生成转换后文件的全过程。 上面代码中的 writer.setOptions(options)是用来对输出文件的属性进行相关的设置,每种格式的属性都不一 样,com.sun.jimi.core.options.*中针对每种图片编码格式的相关属性提供了getter和setter方法,可以方便的进行设 置: com.sun.jimi.core.options.*中针对每种图片编码格式的相关属性提供了getter和setter方法,可以方便的进行设置: JPGOptions options = new JPGOptions(); options.setQuality(quality); 格式转换就这么简单。 jimi不支持的encode格式的解决 public boolean convertToJPG(String sSourceImage, String sDestImage, int nQuality){ if (sSourceImage == null || sSourceImage.trim().equals("")){ return false; } if (sDestImage == null || sDestImage.trim().equals("")){ sDestImage = sSourceImage.substring(0, sSourceImage.lastIndexOf(".")) + ".jpg"; }else if (!sDestImage.endsWith(".jpg")){ sDestImage += ".jpg"; } File tSourceImageFile = new File(sSourceImage); if (!tSourceImageFile.exists()){ return false; }else if (!tSourceImageFile.canRead()){ return false; }else if (!tSourceImageFile.isFile()){ return false; } tSourceImageFile = null; try{ JPGOptions tJPGOptions = new JPGOptions(); if (nQuality < 0 || nQuality > 100){ tJPGOptions.setQuality(75); }else{ tJPGOptions.setQuality(nQuality); } ImageProducer tImageProducer = Jimi.getImageProducer(sSourceImage); JimiWriter tJimiWriter = Jimi.createJimiWriter(sDestImage); tJimiWriter.setSource(tImageProducer); tJimiWriter.setOptions(tJPGOptions); tJimiWriter.putImage(sDestImage); tImageProducer = null; tJimiWriter = null; tJPGOptions = null; }catch (JimiException je){ je.printStackTrace(); return false; }catch (Exception ex){ ex.printStackTrace(); return false; } return false; }
本文档为【图片格式转换】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。