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
;
public class ImageConverUtil {
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();
}
}
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;
}
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;
}
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;
}
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;
}
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";
ImageConverUtil
.imageConvertCommon(srcImgPath
, sDestImage
,".jpg");
}
}
需要的jar如下:
jai_codec
-1.1.3.jar
jai_core
.jar
jai_imageio
.jar
jimi
-1.0.jar