根据企业真实需求背景,java实现jpg、png、tif、gif 任意图像格式转换
方法名 | 说明 |
imageConvertCommon | 任意图像转换通用类 |
imageConvertToGIF | 图像任意格式转gif |
imageConvertToTif | 图像任意格式转tif |
imageConvertToJPG | 图像任意格式转jpg |
imageConvertToPNG | 图像任意格式转png |
说一下特殊处理的部分,就是动态传后缀名,这个地方重命名用的是带.
的,但是,在格式转换这一行是不带.
的,这里要注意,可以采用替换或者截取的方式,这里才去的是替换,都一样吧!
if (!ImageIO.write(bi, fileSuffix.replace(".",""), new File(sDestImage))) {
System.out.println(" JimiImageUtil.convertTo" + fileSuffix.toUpperCase() + "() : 转换图像格式发生异常!");
return false;
}
涉及的jar包,由于jar收拾收费的因此,大家可以去我的开源项目中,
下载需要的jar饱即可(见文章底部)
jai_imageio.jar
jimi-1.0.jar
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.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
/*******************************************************************************
*
* @direction: 图像格式转换类(转换时不需要关心源图的格式)
* @support : GIF(no compressed encoding), JPEG, TIFF, PNG, PICT, BMP, Targa,
* ICO, CUR, XBM, XPM, PCX, DCX
* @author : gblfy
******************************************************************************/
public class ImageConverUtil {
/**
* 任意图片转换
*
* @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) {
// jpg png gif tif 图片格式互转
String inputFilePath = "D:" + File.separator + "7" + File.separator + "1.jpg";
String outputFilePath = "D:" + File.separator + "7" + File.separator + "1.tif";
String fileSuffix = "tif";
ImageConverUtil.imageConvertCommon(outputFilePath, inputFilePath, 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("-----------------执行完成-------------");
}
}