根據企業真實需求背景,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("-----------------執行完成-------------");
}
}