/**
* @Description TestImageUtil.java
* @author 張軍
* @date 2017年6月16日 下午6:10:01
* @version V1.0
*/
package zj.image;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Collection;
import javax.imageio.ImageIO;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import zj.check.util.CheckUtil;
import zj.common.exception.ServiceException;
import zj.io.util.FileUtil;
import zj.java.util.JavaUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 純JAVA實現的圖片處理工具類
* @see 1、多個圖片合成
* @see 2、裁剪圖片
* @see 3、圖片壓縮
* @see 4、制作圓角
* @see 5、重調圖片尺寸
* @see 6、獲取圖片尺寸
* @see 7、圖片縮放
* @see 8、導入網絡圖片到緩沖區
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*/
public class ImageUtil {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(ImageUtil.class);
/**
* 導入網絡圖片到緩沖區
*
* @param imgUrl
* 網絡路徑
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 圖片緩沖區
*/
public static BufferedImage loadImageUrl(String imgUrl) {
try {
// 獲取網絡地址
URL url = new URL(imgUrl);
// 返回網絡圖片緩沖區
return ImageIO.read(url);
} catch (Exception e) {
logger.error("導入網絡圖片到緩沖區", e);
}
return null;
}
/**
* 圖片縮放
*
* @see #resize(String, String, int, int)
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param width
* 寬度
* @param height
* 高度
*/
@Deprecated
public static void zoomImage(String src, String dest, int width, int height) {
try {
double wr = 0, hr = 0;
File srcFile = new File(src);
File destFile = new File(dest);
File targetDir = destFile.getParentFile();
if (targetDir != null && !targetDir.exists()) {
// 創建目標文件目錄
targetDir.mkdirs();
}
BufferedImage bufImg = ImageIO.read(srcFile);
Image Itemp = bufImg.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
wr = width * 1.0 / bufImg.getWidth();
hr = height * 1.0 / bufImg.getHeight();
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
Itemp = ato.filter(bufImg, null);
ImageIO.write((BufferedImage) Itemp, dest.substring(dest.lastIndexOf(".") + 1), destFile);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 壓縮圖片
*
* @see #resize(String, String, int, int)
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param width
* 寬度
* @param height
* 高度
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
@Deprecated
public static void imageScale(String src, String dest, int width, int height) {
BufferedOutputStream output = null;
try {
File srcFile = new File(src);
File destFile = new File(dest);
File targetDir = destFile.getParentFile();
if (targetDir != null && !targetDir.exists()) {
// 創建目標文件目錄
targetDir.mkdirs();
}
Image image = javax.imageio.ImageIO.read(srcFile);
image = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
// Make a BufferedImage from the Image.
BufferedImage mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();
g2.drawImage(image, 0, 0, width, height, Color.WHITE, null);
g2.dispose();
float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, -0.125f, -0.125f, -0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, kernelData2);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
mBufferedImage = cOp.filter(mBufferedImage, null);
output = new BufferedOutputStream(new FileOutputStream(destFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(mBufferedImage);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(output);
}
}
/**
* 獲取圖片尺寸(寬,高)
*
* @param filePath
* 文件路徑
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return [width,height]
*/
public static int[] getSizeInfo(String filePath) {
try {
// 獲取圖片文件
File file = new File(filePath);
// 獲取緩沖區
Image image = ImageIO.read(file);
// 返回寬度
int width = image.getWidth(null);
// 返回高度
int height = image.getHeight(null);
return new int[] { width, height };
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 重調圖片尺寸
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param width
* 新的寬度,小于1則忽略,按原圖比例縮放
* @param height
* 新的高度,小于1則忽略,按原圖比例縮放
* @param maxWidth
* 最大寬度,限制目標圖片寬度,小于1則忽略此設置
* @param maxHeight
* 最大高度,限制目標圖片高度,小于1則忽略此設置
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void resize(File srcFile, File destFile, int width, int height, int maxWidth, int maxHeight) {
InputStream input = null;
OutputStream output = null;
try {
FileUtil.fileMkdir(destFile);
// 獲取文件輸入流
input = new BufferedInputStream(new FileInputStream(srcFile));
// 獲取文件輸出流
output = new BufferedOutputStream(new FileOutputStream(destFile));
if (width < 1 && height < 1 && maxWidth < 1 && maxHeight < 1) {
// 如果全部小于1尺寸
IOUtils.copy(input, output);
}
// 獲取源圖片緩沖區
BufferedImage img = ImageIO.read(input);
// 源圖片寬度
double tempWidth = img.getWidth(null);
// 源圖片高度
double tempHeight = img.getHeight(null);
// 目標圖片寬度
int toWidth = -1;
// 目標圖片高度
int toHeight = -1;
// 源圖片寬高比例
double rate = tempWidth / tempHeight;
if (width > 0 && height > 0) {
// 如果輸出的寬高大于0,比例設置為寬高比
rate = ((double) width) / ((double) height);
// 設置目標圖片寬高為輸出的寬高
toWidth = width;
toHeight = height;
} else if (width > 0) {
// 如果輸出的寬大于0
// 設置目標圖片寬為輸出的寬
toWidth = width;
// 設置目標圖片高為輸出的寬比上源圖片寬高比
toHeight = (int) (toWidth / rate);
} else if (height > 0) {
// 如果輸出的高大于0
// 設置目標圖片高為輸出的高
toHeight = height;
// 設置目標圖片寬為輸出的高比上源圖片寬高比
toWidth = (int) (toHeight * rate);
} else {
// 否則設置源圖片寬
toWidth = ((Number) tempWidth).intValue();
// 否則設置源圖片高
toHeight = ((Number) tempHeight).intValue();
}
if (maxWidth > 0 && toWidth > maxWidth) {
// 如果最大寬高大于 0并且輸出的寬大于最大寬度
// 設置輸出的寬為最大寬度
toWidth = maxWidth;
// 設置輸出的高為輸出的寬比上源圖片寬高比
toHeight = (int) (toWidth / rate);
}
if (maxHeight > 0 && toHeight > maxHeight) {
// 如果最大高高大于 0并且輸出的高大于最大高度
// 設置輸出的高為最大高度
toHeight = maxHeight;
// 設置輸出的寬為輸出的高比上源圖片寬高比
toWidth = (int) (toHeight * rate);
}
// 判斷透明色
boolean hasNotAlpha = img.getColorModel().hasAlpha();
// 獲取透明色
int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
// 創建一個帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_ARGB
// 創建一個不帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_RGB
// 源圖片緩沖區
BufferedImage tag = new BufferedImage(toWidth, toHeight, typeInt);
// Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢
tag.getGraphics().drawImage(img.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH), 0, 0, null);
// 獲取源文件擴展名
String srcExtName = FilenameUtils.getExtension(srcFile.getName());
// 輸出源圖片緩沖區到輸出的文件中
ImageIO.write(tag, srcExtName, output);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
/**
* 重調圖片尺寸(此方法圖片更清楚)
*
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param width
* 新的寬度,小于1則忽略,按原圖比例縮放
* @param height
* 新的高度,小于1則忽略,按原圖比例縮放
* @param maxWidth
* 新的最大寬度,限制目標圖片寬度,按原圖比例縮放
* @param maxHeight
* 新的最大高度,限制目標圖片高度,按原圖比例縮放
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void resize(String src, String dest, int width, int height, int maxWidth, int maxHeight) {
resize(new File(src), new File(dest), width, height, maxWidth, maxHeight);
}
/**
* 重調圖片尺寸(此方法圖片更清楚)
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param width
* 新的寬度,小于1則忽略,按原圖比例縮放
* @param height
* 新的高度,小于1則忽略,按原圖比例縮放
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void resize(File srcFile, File destFile, int width, int height) {
resize(srcFile, destFile, width, height, -1, -1);
}
/**
* 重調圖片尺寸(此方法圖片更清楚)
*
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param width
* 新的寬度,小于1則忽略,按原圖比例縮放
* @param height
* 新的高度,小于1則忽略,按原圖比例縮放
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void resize(String src, String dest, int width, int height) {
resize(new File(src), new File(dest), width, height);
}
/**
* 裁剪圖片
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param x
* x坐標
* @param y
* y坐標
* @param width
* 裁剪寬度
* @param height
* 裁剪高度
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void crop(File srcFile, File destFile, int x, int y, int width, int height) {
OutputStream output = null;
InputStream input = null;
try {
// 獲取源圖片輸入流
input = new BufferedInputStream(new FileInputStream(srcFile));
// 獲取目標圖片輸入流
output = new BufferedOutputStream(new FileOutputStream(destFile));
// 獲取源圖片緩沖區
BufferedImage srcImg = ImageIO.read(input);
// 獲取源圖片寬度
int srcWidth = srcImg.getWidth();
// 獲取源圖片高度
int srcHeight = srcImg.getHeight();
// 計算臨時x軸
int tempX = Math.min(srcWidth - 1, x);
// 計算臨時y軸
int tempY = Math.min(srcHeight - 1, y);
// 輸出的臨時寬度
int tempWidth = width;
if (tempX + width > srcWidth) {
// 臨時的x軸+輸出的寬度大于源文件寬度
// 臨時寬度設置源圖片的寬度-臨時x軸與1相比的最大值
tempWidth = Math.max(1, srcWidth - tempX);
}
// 輸出的臨時高度
int tempHeight = height;
if (tempY + height > srcHeight) {
// 臨時的y軸+輸出的高度大于源文件高度
// 臨時高度設置源圖片的高度-臨時y軸與1相比的最大值
tempHeight = Math.max(1, srcHeight - tempY);
}
// 通過源圖片的緩沖區獲取目標文件緩沖區
BufferedImage dest = srcImg.getSubimage(tempX, tempY, tempWidth, tempHeight);
// 判斷透明色
boolean hasNotAlpha = srcImg.getColorModel().hasAlpha();
// 獲取透明色
int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
// 創建一個帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_ARGB
// 創建一個不帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_RGB
// 源圖片緩沖區
BufferedImage tag = new BufferedImage(width, height, typeInt);
// 開始裁剪
tag.getGraphics().drawImage(dest, 0, 0, null);
// 獲取源文件擴展名
String srcExtName = FilenameUtils.getExtension(srcFile.getName());
// 輸出源圖片緩沖區到輸出的文件中
ImageIO.write(tag, srcExtName, output);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
/**
* 裁剪圖片
*
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param x
* x坐標
* @param y
* y坐標
* @param width
* 裁剪寬度
* @param height
* 裁剪高度
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void crop(String src, String dest, int x, int y, int width, int height) throws Exception {
crop(new File(src), new File(dest), x, y, width, height);
}
/**
* 制作圓角
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param cornerRadius
* 角度
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void makeRoundedCorner(File srcFile, File destFile, int cornerRadius) {
InputStream input = null;
OutputStream output = null;
try {
// 獲取源圖片輸入流
input = new BufferedInputStream(new FileInputStream(srcFile));
// 獲取目標圖片輸入流
output = new BufferedOutputStream(new FileOutputStream(destFile));
// 獲取源圖片緩沖區
BufferedImage srcImg = ImageIO.read(input);
// 獲取源圖片寬度
int srcWidth = srcImg.getWidth();
// 獲取源圖片高度
int srcHeight = srcImg.getHeight();
cornerRadius = cornerRadius < 1 ? srcWidth / 4 : cornerRadius;
// 創建一個帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_ARGB
// 創建一個不帶透明色的BufferedImage對象:BufferedImage.TYPE_INT_RGB
// 源圖片緩沖區
BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_ARGB);
// Graphics2D ,Graphics 類,提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
// 它是用于在 Java(tm) 平臺上呈現二維形狀、文本和圖像的基礎類。驗證碼生成可以用到此類。
// public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴展了 Graphics 類,
// 提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
Graphics2D g2 = tag.createGraphics();
// This is what we want, but it only does hard-clipping, i.e.
// aliasing
// g2.setClip(new RoundRectangle2D ...)
// so instead fake soft-clipping by first drawing the desired clip
// shape
// in fully opaque white with antialiasing enabled...
// Java 2D允許分配透明(alpha)值,以便底層的圖形可以顯示出來。通常我們會創建一個java.awt.AlphaComposite對象,然后傳入 setComposite()方法的實現。
g2.setComposite(AlphaComposite.Src);
// 其他渲染hints適用在不同的環境下。如縮放圖片時,為KEY_INTERPOLATION使用 VALUE_INTERPOLATION_BILINEAR。請查閱本類的Javadoc,詳細了解各個選項所適用的環境。
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 設置顏色
g2.setColor(Color.WHITE);
// 開始制作
// 前兩個指定矩形的左上角的坐標
// 第3,4個指定矩形的寬度和高度
// 最后兩個指定圓角弧形的寬度和高度
g2.fill(new RoundRectangle2D.Float(0, 0, srcWidth, srcHeight, cornerRadius, cornerRadius));
// ... then compositing the image on top,
// using the white shape from above as alpha source
// Java 2D允許分配透明(alpha)值,以便底層的圖形可以顯示出來。通常我們會創建一個java.awt.AlphaComposite對象,然后傳入 setComposite()方法的實現。
g2.setComposite(AlphaComposite.SrcAtop);
// 開始畫圖片
g2.drawImage(srcImg, 0, 0, null);
// 釋放資源
g2.dispose();
// 輸出源圖片緩沖區到輸出的文件中(圓角只能是png)
ImageIO.write(tag, "png", output);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
}
/**
* 制作圓角
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param cornerRadius
* 角度
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void makeRoundedCorner(String src, String dest, int cornerRadius) {
makeRoundedCorner(new File(src), new File(dest), cornerRadius);
}
/**
* 修改圖片,返回修改后的圖片緩沖區(只輸出一行文本)
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param font
* 字體
* @param fontColor
* 字體顏色
* @param fontSize
* 字體大小
* @param x
* x坐標
* @param y
* y坐標
* @param onlyLine
* 是否一行輸出文本,默認false:多行輸出
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
@SuppressWarnings("unchecked")
public static void writeImageString(File srcFile, File destFile, Font font, Color fontColor, int fontSize, Object content, int x, int y, boolean onlyLine) {
InputStream input = null;
OutputStream output = null;
try {
// 獲取源圖片輸入流
input = new BufferedInputStream(new FileInputStream(srcFile));
// 獲取文件輸出流
output = new BufferedOutputStream(new FileOutputStream(destFile));
// 獲取源圖片緩沖區
BufferedImage srcImg = ImageIO.read(input);
// 獲取源圖片寬度
int srcWidth = srcImg.getWidth();
// 獲取源圖片高度
int srcHeight = srcImg.getHeight();
// Graphics2D ,Graphics 類,提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
// 它是用于在 Java(tm) 平臺上呈現二維形狀、文本和圖像的基礎類。驗證碼生成可以用到此類。
// public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴展了 Graphics 類,
// 提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
Graphics2D g = srcImg.createGraphics();
// 設置背景為白色
g.setBackground(Color.WHITE);
if (fontColor == null) {
// 設置字體顏色為藍色
fontColor = Color.BLUE;
}
// 設置字體顏色為藍色
g.setColor(fontColor);
// ont是JAVA中的字體類,PLAIN是Font類中的靜態常量( static final ) ,
// 表示是:普通樣式常量。其他可用樣式為:BOLD :粗體樣式常量 ,ITALIC: 斜體樣式常量.如可以如下初始化對象:
// Font textFont = new Font("宋體" , Font.BOLD , 23);該字體表示23磅粗體的宋體字。
if (font == null) {
// 設置字體
font = new Font("微軟雅黑", Font.PLAIN, fontSize);
}
// 設置字體
g.setFont(font);
// 字體大小
// 計算臨時x軸
int tempX = x;
// 計算臨時y軸
int tempY = y;
// 驗證輸出位置的縱坐標和橫坐標
if (x >= srcHeight || y >= srcWidth) {
// 臨時x軸為源圖片高度-文字大小+2
tempX = srcHeight - fontSize + 2;
// 臨時y軸為源圖片寬度
tempY = srcWidth;
}
if (content instanceof Collection) {
Collection<String> contentColl = (Collection<String>) content;
int contentLength = contentColl.size();
if (onlyLine) {
boolean first = true;
for (String scontent : contentColl) {
if (first) {
if (x < 0) {
// 寫到右下角
tempX = srcWidth - scontent.length() * contentLength - fontSize - 50;
}
if (y < 0) {
// 寫到右下角
tempY = srcHeight - fontSize - 10;
}
first = false;
}
g.drawString(scontent, tempX, tempY);
tempX += scontent.length() * fontSize / 2 + 5;// 重新計算文本輸出位置
}
} else {
boolean first = true;
for (String scontent : contentColl) {
if (first) {
if (x < 0) {
// 寫到右下角
tempX = srcWidth - scontent.length() - fontSize - 50;
}
if (y < 0) {
// 寫到右下角
tempY = srcHeight - fontSize * contentLength - 10;
}
first = false;
}
g.drawString(scontent, tempX, tempY);
tempY += fontSize + 2;// 重新計算文本輸出位置
}
}
} else {
String scontent = JavaUtil.objToStr(content);
if (CheckUtil.isNotNull(scontent)) {
if (x < 0) {
// 寫到右下角
tempX = srcWidth - scontent.length() - fontSize - 30;
}
if (y < 0) {
// 寫到右下角
tempY = srcHeight - fontSize - 10;
}
// 在圖片上寫文字
g.drawString(scontent, tempX, tempY);
}
}
// 釋放資源
g.dispose();
// 獲取源文件擴展名
String srcExtName = FilenameUtils.getExtension(srcFile.getName());
// 輸出源圖片緩沖區到輸出的文件中
ImageIO.write(srcImg, srcExtName, output);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
/**
* 修改圖片,返回修改后的圖片緩沖區(只輸出一行文本)
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param fontColor
* 字體顏色
* @param fontSize
* 字體大小
* @param x
* x坐標
* @param y
* y坐標
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageString(File srcFile, File destFile, Color fontColor, int fontSize, Object content, int x, int y) {
writeImageString(srcFile, destFile, null, fontColor, fontSize, content, x, y, false);
}
/**
* 修改圖片,返回修改后的圖片緩沖區(只輸出一行文本)
*
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param fontColor
* 字體顏色
* @param fontSize
* 字體大小
* @param x
* x坐標
* @param y
* y坐標
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageString(String src, String dest, Color fontColor, int fontSize, Object content, int x, int y) {
writeImageString(new File(src), new File(dest), fontColor, fontSize, content, x, y);
}
/**
* 修改圖片,返回修改后的圖片緩沖區(只輸出一行文本)
*
* @param srcFile
* 源文件
* @param destFile
* 目標文件
* @param x
* x坐標
* @param y
* y坐標
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageString(File srcFile, File destFile, Object content, int x, int y) {
writeImageString(srcFile, destFile, null, 20, content, x, y);
}
/**
* 修改圖片,返回修改后的圖片緩沖區(只輸出一行文本)
*
* @param src
* 源文件路徑
* @param dest
* 目標文件路徑
* @param x
* x坐標
* @param y
* y坐標
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageString(String src, String dest, Object content, int x, int y) {
writeImageString(new File(src), new File(dest), content, x, y);
}
/**
* 將兩張圖片合并后輸出圖片
*
* @param fromFile
* 合并來自文件
* @param toFile
* 合并到文件
* @param destFile
* 目標文件
* @param x
* x坐標,默認合并到左上角,當x<0時,合并到右下角
* @param y
* y坐標,默認合并到左上角,當y<0時,合并到右下角
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageMerge(File fromFile, File toFile, File destFile, int x, int y) {
InputStream frominput = null;
InputStream toinput = null;
OutputStream output = null;
try {
// 獲取源圖片輸入流from
frominput = new BufferedInputStream(new FileInputStream(fromFile));
// 獲取源圖片輸入流to
toinput = new BufferedInputStream(new FileInputStream(toFile));
// 獲取文件輸出流
output = new BufferedOutputStream(new FileOutputStream(destFile));
// 獲取from圖片緩沖區
BufferedImage fromImg = ImageIO.read(frominput);
// 獲取to圖片緩沖區
BufferedImage toImg = ImageIO.read(toinput);
// 獲取from圖片寬度
int fromWidth = fromImg.getWidth();
// 獲取from圖片高度
int fromHeight = fromImg.getHeight();
// 獲取to圖片寬度
int toWidth = toImg.getWidth();
// 獲取to圖片高度
int toHeight = toImg.getHeight();
// Graphics2D ,Graphics 類,提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
// 它是用于在 Java(tm) 平臺上呈現二維形狀、文本和圖像的基礎類。驗證碼生成可以用到此類。
// public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴展了 Graphics 類,
// 提供了對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制。
Graphics2D g = toImg.createGraphics();
// 合并圖片
if (x < 0) {
// 合并到右下角
x = toWidth - fromWidth;
}
if (y < 0) {
// 合并到右下角
y = toHeight - fromHeight;
}
g.drawImage(fromImg, x, y, fromWidth, fromHeight, null);
// 釋放資源
g.dispose();
// 獲取to文件擴展名
String toExtName = FilenameUtils.getExtension(toFile.getName());
// 輸出to圖片緩沖區到輸出的文件中
ImageIO.write(toImg, toExtName, output);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
IOUtils.closeQuietly(frominput);
IOUtils.closeQuietly(toinput);
IOUtils.closeQuietly(output);
}
}
/**
* 將兩張圖片合并后輸出圖片
*
* @param fromFile
* 合并來自文件
* @param toFile
* 合并到文件
* @param destFile
* 目標文件
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageMerge(File fromFile, File toFile, File destFile) {
writeImageMerge(fromFile, toFile, destFile, 0, 0);
}
/**
* 將兩張圖片合并后輸出圖片
*
* @param from
* 合并來自文件路徑
* @param to
* 合并到文件路徑
* @param dest
* 目標文件路徑
* @param x
* x坐標,默認合并到左上角,當x<0時,合并到右下角
* @param y
* y坐標,默認合并到左上角,當y<0時,合并到右下角
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageMerge(String from, String to, String dest, int x, int y) {
writeImageMerge(new File(from), new File(to), new File(dest), x, y);
}
/**
* 將兩張圖片合并后輸出圖片
*
* @param from
* 合并來自文件路徑
* @param to
* 合并到文件路徑
* @param dest
* 目標文件路徑
* @author 張軍
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public static void writeImageMerge(String from, String to, String dest) {
writeImageMerge(new File(from), new File(to), new File(dest), 0, 0);
}
}
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.dlhighland.cn
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

