java 实现批量图片任意文件格式相互转换

    技术2024-05-11  83

    package com.gblfy.util; import com.sun.jimi.core.Jimi; import com.sun.jimi.core.JimiException; import com.sun.jimi.core.JimiWriter; import com.sun.jimi.core.options.JPGOptions; import com.sun.jimi.core.options.PNGOptions; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.ImageProducer; import java.io.File; import java.io.IOException; /******************************************************************************* * * @direction: 图像格式转换类(转换时不需要关心源图的格式) * @support : GIF(no compressed encoding), JPEG, TIFF, PNG, PICT, BMP, Targa, * ICO, CUR, XBM, XPM, PCX, DCX * @author gblfy * @date 2020-07-17 ******************************************************************************/ public class ImageConverUtil { /** * png转jpg * * @param imgfile 源图片绝对路径 * @param format 后缀名 * @param formatFile 转换后的.jpg文件 * @throws IOException */ public static void pngConverterjPG(File imgfile, String format, File formatFile) { try { imgfile.canRead(); BufferedImage bi = ImageIO.read(imgfile); BufferedImage newBufferedImage = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); newBufferedImage.createGraphics().drawImage(bi, 0, 0, Color.WHITE, null); ImageIO.write(newBufferedImage, format, formatFile); } catch (Exception e) { e.printStackTrace(); } } /** * 任意图片转换 * <P> * 1.png格式建议使用pngConverterjPG * 2.其他格式使用imageConvertCommon * </P> * * @param sSourceImage 源图片 * @param sDestImage 转换后的图片 * @param fileSuffix 转换后的图片扩展名 * @return */ public static boolean imageConvertCommon(String sSourceImage, String sDestImage, String fileSuffix) { if (sSourceImage == null || sSourceImage.trim().equals("")) { System.out.println("JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 要转换的源图像文件路径不能为空!"); return false; } if (sDestImage == null || sDestImage.trim().equals("")) { sDestImage = sSourceImage.substring(0, sSourceImage.lastIndexOf(".")) + fileSuffix; } else if (!sDestImage.endsWith(fileSuffix)) { sDestImage += fileSuffix; } //检查源图像文件 File tSourceImageFile = new File(sSourceImage); if (!tSourceImageFile.exists()) { System.out.println("JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 要转换的源图像文件路径不存在!"); return false; } else if (!tSourceImageFile.canRead()) { System.out.println("JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "(): 要转换的源图像文件路径不可读!"); return false; } else if (!tSourceImageFile.isFile()) { System.out.println("JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 要转换的源图像路径不是一个有效的文件名!"); return false; } long lRunStartTime = System.currentTimeMillis(); try { BufferedImage bi = ImageIO.read(tSourceImageFile); if (bi == null) { //图片解码错误 System.out.println("Jre:" + System.getProperty("java.version")); System.out.println(" JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 要转换的源图像解码错误!"); return false; } if (!ImageIO.write(bi, fileSuffix.replace(".", ""), new File(sDestImage))) { System.out.println(" JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 转换图像格式发生异常!"); return false; } long lRunEndTime = System.currentTimeMillis(); long lRunTime = lRunEndTime - lRunStartTime; System.out.println(" JimiImageUtil.convertToJPG() 运行时间 : " + lRunTime + " 毫秒"); } catch (Exception e) { System.out.println(" JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 转换图像格式发生异常!"); return false; } return true; } /** * 图片转gif * * @param sSourceImage 源图片的绝对路径 D:\5\1.jpg D:\5\1.png * @param sDestImage 转换后的gif图片路径 D:\5\1.gif * @return */ public static boolean imageConvertToGIF(String sSourceImage, String sDestImage) { if (sSourceImage == null || sSourceImage.trim().equals("")) { System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像文件路径不能为空!"); return false; } if (sDestImage == null || sDestImage.trim().equals("")) { sDestImage = sSourceImage.substring(0, sSourceImage.lastIndexOf(".")) + ".gif"; } else if (!sDestImage.endsWith(".gif")) { sDestImage += ".gif"; } //检查源图像文件 File tSourceImageFile = new File(sSourceImage); if (!tSourceImageFile.exists()) { System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像文件路径不存在!"); return false; } else if (!tSourceImageFile.canRead()) { System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像文件路径不可读!"); return false; } else if (!tSourceImageFile.isFile()) { System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像路径不是一个有效的文件名!"); return false; } try { BufferedImage bi = ImageIO.read(tSourceImageFile); if (bi == null) { //图片解码错误 System.out.println("Jre:" + System.getProperty("java.version")); System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像解码错误!"); return false; } if (!ImageIO.write(bi, "gif", new File(sDestImage))) { System.out.println(" JimiImageUtil.convertToGIF() : 转换图像格式发生异常!"); return false; } } catch (Exception e) { System.out.println(" JimiImageUtil.convertToGIF() : 转换图像格式发生异常!"); return false; } return true; } /** * 图片转Tif * * @param sSourceImage * @param sDestImage * @return */ public static boolean imageConvertToTif(String sSourceImage, String sDestImage) { if (sSourceImage == null || sSourceImage.trim().equals("")) { System.out.println(" JimiImageUtil.convertToGIF() : 要转换的源图像文件路径不能为空!"); return false; } if (sDestImage == null || sDestImage.trim().equals("")) { sDestImage = sSourceImage.substring(0, sSourceImage.lastIndexOf(".")) + ".tif"; } else if (!sDestImage.endsWith(".tif")) { sDestImage += ".tif"; } //检查源图像文件 File tSourceImageFile = new File(sSourceImage); if (!tSourceImageFile.exists()) { System.out.println(" JimiImageUtil.convertToTif() : 要转换的源图像文件路径不存在!"); return false; } else if (!tSourceImageFile.canRead()) { System.out.println(" JimiImageUtil.convertToTif() : 要转换的源图像文件路径不可读!"); return false; } else if (!tSourceImageFile.isFile()) { System.out.println(" JimiImageUtil.convertToTif() : 要转换的源图像路径不是一个有效的文件名!"); return false; } try { BufferedImage bi = ImageIO.read(tSourceImageFile); if (bi == null) { //图片解码错误 System.out.println("Jre:" + System.getProperty("java.version")); System.out.println(" JimiImageUtil.convertToTif() : 要转换的源图像解码错误!"); return false; } if (!ImageIO.write(bi, "tif", new File(sDestImage))) { System.out.println(" JimiImageUtil.convertToTif() : 转换图像格式发生异常!"); return false; } } catch (Exception e) { System.out.println("JimiImageUtil.convertToTif() : 转换图像格式发生异常!"); return false; } return true; } /** * 转换图像格式为 JPG * * @param sSourceImage, 其它格式的源图像文件路径 * @param sDestImage, 目标 JPG 图像文件存放路径 * @param nQuality, 品质, 0-100, 数值越高品质越好 * @return */ public static boolean imageConvertToJPG(String sSourceImage, String sDestImage, int nQuality) { if (sSourceImage == null || sSourceImage.trim().equals("")) { System.out.println(" JimiImageUtil.convertToJPG() : 要转换的源图像文件路径不能为空!"); 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()) { System.out.println(" JimiImageUtil.convertToJPG() : 要转换的源图像文件路径不存在!"); return false; } else if (!tSourceImageFile.canRead()) { System.out.println(" JimiImageUtil.convertToJPG() : 要转换的源图像文件路径不可读!"); return false; } else if (!tSourceImageFile.isFile()) { System.out.println(" JimiImageUtil.convertToJPG() : 要转换的源图像路径不是一个有效的文件名!"); return false; } tSourceImageFile = null; try { long lRunStartTime = System.currentTimeMillis(); 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; long lRunEndTime = System.currentTimeMillis(); long lRunTime = lRunEndTime - lRunStartTime; System.out.println(" JimiImageUtil.convertToJPG() 运行时间 : " + lRunTime + " 毫秒"); } catch (JimiException je) { System.out.println(" JimiImageUtil.convertToJPG() : 转换图像格式发生异常!"); je.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } /** * 转换图像格式为 PNG * * @param sSourceImage , 其它格式的源图像文件路径 * @param sDestImage ,目标 PNG 图像文件存放路径 * @param sCompression ,压缩比, none, default, fast, max * @return */ public static boolean imageConvertToPNG(String sSourceImage, String sDestImage, String sCompression) { if (sSourceImage == null || sSourceImage.trim().equals("")) { System.out.println(" JimiImageUtil.convertToPNG() : 要转换的源图像文件路径不能为空!"); return false; } if (sDestImage == null || sDestImage.trim().equals("")) { sDestImage = sSourceImage.substring(0, sSourceImage.lastIndexOf(".")) + ".png"; } else if (!sDestImage.endsWith(".png")) { sDestImage += ".png"; } //---------------------------------------------------------------------- //检查源图像文件 File tSourceImageFile = new File(sSourceImage); if (!tSourceImageFile.exists()) { System.out.println(" JimiImageUtil.convertToPNG() : 要转换的源图像文件路径不存在!"); return false; } else if (!tSourceImageFile.canRead()) { System.out.println(" JimiImageUtil.convertToPNG() : 要转换的源图像文件路径不可读!"); return false; } else if (!tSourceImageFile.isFile()) { System.out.println(" JimiImageUtil.convertToPNG() : 要转换的源图像路径不是一个有效的文件名!"); return false; } tSourceImageFile = null; try { long lRunStartTime = System.currentTimeMillis(); PNGOptions tPNGOptions = new PNGOptions(); if (sCompression == null || sCompression.trim().equals("")) { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_DEFAULT); } else if (sCompression.equalsIgnoreCase("none")) { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_NONE); } else if (sCompression.equalsIgnoreCase("fast")) { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_FAST); } else if (sCompression.equalsIgnoreCase("max")) { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_MAX); } else { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_DEFAULT); } ImageProducer tImageProducer = Jimi.getImageProducer(sSourceImage); JimiWriter tJimiWriter = Jimi.createJimiWriter(sDestImage); tJimiWriter.setSource(tImageProducer); tJimiWriter.setOptions(tPNGOptions); tJimiWriter.putImage(sDestImage); tImageProducer = null; tJimiWriter = null; tPNGOptions = null; long lRunEndTime = System.currentTimeMillis(); long lRunTime = lRunEndTime - lRunStartTime; System.out.println(" JimiImageUtil.convertToPNG() 运行时间 : " + lRunTime + " 毫秒"); } catch (JimiException je) { System.out.println(" JimiImageUtil.convertToPNG() : 转换图像格式发生异常!"); je.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } public static void main(String[] args) throws Exception { String srcImgPath = "D:\\22\\srcImgPath\\22.png"; String sDestImage = "D:\\22\\srcImgPath\\22.jpg"; //批量图片文件格式转换 第2个参数可是省略 // ImageConverUtil.imageConvertCommon(srcImgPath, "",".jpg"); ImageConverUtil.imageConvertCommon(srcImgPath, sDestImage,".jpg"); // tif 图片转jpg png gif // String inputFilePath = "D:" + File.separator + "1" + File.separator + "1.tif"; // String outputFilePath = "D:" + File.separator + "1" + File.separator + "1.jpg"; // String fileSuffix = ".jpg"; // ImageConverUtil.imageConvertCommon(inputFilePath,outputFilePath, fileSuffix); // System.out.println("-----------------执行完成-------------"); // String inputFilePath = "D:" + File.separator + "5" + File.separator + "1.jpg"; // String outputFilePath = "D:" + File.separator + "5" + File.separator + "1.tif"; // String fileSuffix = "tif"; // ImageConverUtil.imageConvertCommon(inputFilePath,outputFilePath , fileSuffix); // System.out.println("-----------------执行完成-------------"); //png /jpg 转tif // String inputFilePath = "D:" + File.separator + "6" + File.separator + "1.jpg"; // String inputFilePath = "D:" + File.separator + "6" + File.separator + "1.png"; // String outputFilePath = "D:" + File.separator + "6" + File.separator + "1.tif"; // ImageConverUtil.imageConvertToTif(inputFilePath, outputFilePath); // System.out.println("-----------------执行完成-------------"); //png /jpg 转gif // String inputFilePath = "D:" + File.separator + "5" + File.separator + "1.png"; // String inputFilePath = "D:" + File.separator + "5" + File.separator + "1.jpg"; // String outputFilePath = "D:" + File.separator + "5" + File.separator + "1.gif"; // ImageConverUtil.imageConvertToGIF(inputFilePath, outputFilePath); // System.out.println("-----------------执行完成-------------"); // png转jpg // String inputFilePath = "D:" + File.separator + "4" + File.separator + "1.tif"; // String outputFilePath = "D:" + File.separator + "4" + File.separator + "1.jpg"; // ImageConverUtil.imageConvertToJPG(inputFilePath, outputFilePath, 100); // System.out.println("-----------------执行完成-------------"); // jpg 转png // String inputFilePath = "D:" + File.separator + "3" + File.separator + "1.jpg"; // String outputFilePath = "D:" + File.separator + "3" + File.separator + "1.png"; // ImageConverUtil.imageConvertToPNG(inputFilePath, outputFilePath, "default"); // System.out.println("-----------------执行完成-------------"); } }

    需要的jar如下:

    jai_codec-1.1.3.jar jai_core.jar jai_imageio.jar jimi-1.0.jar
    Processed: 0.013, SQL: 9