1、多個(gè)圖片合成
2、裁剪圖片
3、圖片壓縮
4、制作圓角
5、重調(diào)圖片尺寸
6、獲取圖片尺寸
7、圖片縮放
8、導(dǎo)入網(wǎng)絡(luò)圖片到緩沖區(qū)
/** * @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.java.util.JavaUtil; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * 純JAVA實(shí)現(xiàn)的圖片處理工具類 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a target=_blank href="http://m.eyofj.com">張軍個(gè)人網(wǎng)站</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); /** * 導(dǎo)入網(wǎng)絡(luò)圖片到緩沖區(qū) * * @param imgUrl * 網(wǎng)絡(luò)路徑 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return 圖片緩沖區(qū) */ public static BufferedImage loadImageUrl(String imgUrl) { try { // 獲取網(wǎng)絡(luò)地址 URL url = new URL(imgUrl); // 返回網(wǎng)絡(luò)圖片緩沖區(qū) return ImageIO.read(url); } catch (Exception e) { logger.error("導(dǎo)入網(wǎng)絡(luò)圖片到緩沖區(qū)", e); } return null; } /** * 圖片縮放 * * @see #resize(String, String, int, int) * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @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()) { // 創(chuàng)建目標(biāo)文件目錄 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 * 目標(biāo)文件路徑 * @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()) { // 創(chuàng)建目標(biāo)文件目錄 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); // 獲取緩沖區(qū) 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); } } /** * 重調(diào)圖片尺寸 * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @param width * 新的寬度,小于1則忽略,按原圖比例縮放 * @param height * 新的高度,小于1則忽略,按原圖比例縮放 * @param maxWidth * 最大寬度,限制目標(biāo)圖片寬度,小于1則忽略此設(shè)置 * @param maxHeight * 最大高度,限制目標(biāo)圖片高度,小于1則忽略此設(shè)置 * @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 { // 獲取文件輸入流 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); } // 獲取源圖片緩沖區(qū) BufferedImage img = ImageIO.read(input); // 源圖片寬度 double tempWidth = img.getWidth(null); // 源圖片高度 double tempHeight = img.getHeight(null); // 目標(biāo)圖片寬度 int toWidth = -1; // 目標(biāo)圖片高度 int toHeight = -1; // 源圖片寬高比例 double rate = tempWidth / tempHeight; if (width > 0 && height > 0) { // 如果輸出的寬高大于0,比例設(shè)置為寬高比 rate = ((double) width) / ((double) height); // 設(shè)置目標(biāo)圖片寬高為輸出的寬高 toWidth = width; toHeight = height; } else if (width > 0) { // 如果輸出的寬大于0 // 設(shè)置目標(biāo)圖片寬為輸出的寬 toWidth = width; // 設(shè)置目標(biāo)圖片高為輸出的寬比上源圖片寬高比 toHeight = (int) (toWidth / rate); } else if (height > 0) { // 如果輸出的高大于0 // 設(shè)置目標(biāo)圖片高為輸出的高 toHeight = height; // 設(shè)置目標(biāo)圖片寬為輸出的高比上源圖片寬高比 toWidth = (int) (toHeight * rate); } else { // 否則設(shè)置源圖片寬 toWidth = ((Number) tempWidth).intValue(); // 否則設(shè)置源圖片高 toHeight = ((Number) tempHeight).intValue(); } if (maxWidth > 0 && toWidth > maxWidth) { // 如果最大寬高大于 0并且輸出的寬大于最大寬度 // 設(shè)置輸出的寬為最大寬度 toWidth = maxWidth; // 設(shè)置輸出的高為輸出的寬比上源圖片寬高比 toHeight = (int) (toWidth / rate); } if (maxHeight > 0 && toHeight > maxHeight) { // 如果最大高高大于 0并且輸出的高大于最大高度 // 設(shè)置輸出的高為最大高度 toHeight = maxHeight; // 設(shè)置輸出的寬為輸出的高比上源圖片寬高比 toWidth = (int) (toHeight * rate); } // 判斷透明色 boolean hasNotAlpha = img.getColorModel().hasAlpha(); // 獲取透明色 int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; // 創(chuàng)建一個(gè)帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_ARGB // 創(chuàng)建一個(gè)不帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_RGB // 源圖片緩沖區(qū) BufferedImage tag = new BufferedImage(toWidth, toHeight, typeInt); // Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優(yōu)先級(jí)比速度高 生成的圖片質(zhì)量比較好 但速度慢 tag.getGraphics().drawImage(img.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH), 0, 0, null); // 獲取源文件擴(kuò)展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 輸出源圖片緩沖區(qū)到輸出的文件中 ImageIO.write(tag, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 重調(diào)圖片尺寸(此方法圖片更清楚) * * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @param width * 新的寬度,小于1則忽略,按原圖比例縮放 * @param height * 新的高度,小于1則忽略,按原圖比例縮放 * @param maxWidth * 新的最大寬度,限制目標(biāo)圖片寬度,按原圖比例縮放 * @param maxHeight * 新的最大高度,限制目標(biāo)圖片高度,按原圖比例縮放 * @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); } /** * 重調(diào)圖片尺寸(此方法圖片更清楚) * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @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); } /** * 重調(diào)圖片尺寸(此方法圖片更清楚) * * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @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 * 目標(biāo)文件 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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)); // 獲取目標(biāo)圖片輸入流 output = new BufferedOutputStream(new FileOutputStream(destFile)); // 獲取源圖片緩沖區(qū) BufferedImage srcImg = ImageIO.read(input); // 獲取源圖片寬度 int srcWidth = srcImg.getWidth(); // 獲取源圖片高度 int srcHeight = srcImg.getHeight(); // 計(jì)算臨時(shí)x軸 int tempX = Math.min(srcWidth - 1, x); // 計(jì)算臨時(shí)y軸 int tempY = Math.min(srcHeight - 1, y); // 輸出的臨時(shí)寬度 int tempWidth = width; if (tempX + width > srcWidth) { // 臨時(shí)的x軸+輸出的寬度大于源文件寬度 // 臨時(shí)寬度設(shè)置源圖片的寬度-臨時(shí)x軸與1相比的最大值 tempWidth = Math.max(1, srcWidth - tempX); } // 輸出的臨時(shí)高度 int tempHeight = height; if (tempY + height > srcHeight) { // 臨時(shí)的y軸+輸出的高度大于源文件高度 // 臨時(shí)高度設(shè)置源圖片的高度-臨時(shí)y軸與1相比的最大值 tempHeight = Math.max(1, srcHeight - tempY); } // 通過源圖片的緩沖區(qū)獲取目標(biāo)文件緩沖區(qū) BufferedImage dest = srcImg.getSubimage(tempX, tempY, tempWidth, tempHeight); // 判斷透明色 boolean hasNotAlpha = srcImg.getColorModel().hasAlpha(); // 獲取透明色 int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; // 創(chuàng)建一個(gè)帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_ARGB // 創(chuàng)建一個(gè)不帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_RGB // 源圖片緩沖區(qū) BufferedImage tag = new BufferedImage(width, height, typeInt); // 開始裁剪 tag.getGraphics().drawImage(dest, 0, 0, null); // 獲取源文件擴(kuò)展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 輸出源圖片緩沖區(qū)到輸出的文件中 ImageIO.write(tag, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 裁剪圖片 * * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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 * 目標(biāo)文件 * @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)); // 獲取目標(biāo)圖片輸入流 output = new BufferedOutputStream(new FileOutputStream(destFile)); // 獲取源圖片緩沖區(qū) BufferedImage srcImg = ImageIO.read(input); // 獲取源圖片寬度 int srcWidth = srcImg.getWidth(); // 獲取源圖片高度 int srcHeight = srcImg.getHeight(); cornerRadius = cornerRadius < 1 ? srcWidth / 4 : cornerRadius; // 創(chuàng)建一個(gè)帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_ARGB // 創(chuàng)建一個(gè)不帶透明色的BufferedImage對(duì)象:BufferedImage.TYPE_INT_RGB // 源圖片緩沖區(qū) BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_ARGB); // Graphics2D ,Graphics 類,提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 // 它是用于在 Java(tm) 平臺(tái)上呈現(xiàn)二維形狀、文本和圖像的基礎(chǔ)類。驗(yàn)證碼生成可以用到此類。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴(kuò)展了 Graphics 類, // 提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 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)值,以便底層的圖形可以顯示出來。通常我們會(huì)創(chuàng)建一個(gè)java.awt.AlphaComposite對(duì)象,然后傳入 setComposite()方法的實(shí)現(xiàn)。 g2.setComposite(AlphaComposite.Src); // 其他渲染hints適用在不同的環(huán)境下。如縮放圖片時(shí),為KEY_INTERPOLATION使用 VALUE_INTERPOLATION_BILINEAR。請(qǐng)查閱本類的Javadoc,詳細(xì)了解各個(gè)選項(xiàng)所適用的環(huán)境。 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 設(shè)置顏色 g2.setColor(Color.WHITE); // 開始制作 // 前兩個(gè)指定矩形的左上角的坐標(biāo) // 第3,4個(gè)指定矩形的寬度和高度 // 最后兩個(gè)指定圓角弧形的寬度和高度 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)值,以便底層的圖形可以顯示出來。通常我們會(huì)創(chuàng)建一個(gè)java.awt.AlphaComposite對(duì)象,然后傳入 setComposite()方法的實(shí)現(xiàn)。 g2.setComposite(AlphaComposite.SrcAtop); // 開始畫圖片 g2.drawImage(srcImg, 0, 0, null); // 釋放資源 g2.dispose(); // 輸出源圖片緩沖區(qū)到輸出的文件中(圓角只能是png) ImageIO.write(tag, "png", output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } } /** * 制作圓角 * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @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); } /** * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本) * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @param font * 字體 * @param fontColor * 字體顏色 * @param fontSize * 字體大小 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @param onlyLine * 是否一行輸出文本,默認(rèn)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)); // 獲取源圖片緩沖區(qū) BufferedImage srcImg = ImageIO.read(input); // 獲取源圖片寬度 int srcWidth = srcImg.getWidth(); // 獲取源圖片高度 int srcHeight = srcImg.getHeight(); // Graphics2D ,Graphics 類,提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 // 它是用于在 Java(tm) 平臺(tái)上呈現(xiàn)二維形狀、文本和圖像的基礎(chǔ)類。驗(yàn)證碼生成可以用到此類。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴(kuò)展了 Graphics 類, // 提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 Graphics2D g = srcImg.createGraphics(); // 設(shè)置背景為白色 g.setBackground(Color.WHITE); if (fontColor == null) { // 設(shè)置字體顏色為藍(lán)色 fontColor = Color.BLUE; } // 設(shè)置字體顏色為藍(lán)色 g.setColor(fontColor); // ont是JAVA中的字體類,PLAIN是Font類中的靜態(tài)常量( static final ) , // 表示是:普通樣式常量。其他可用樣式為:BOLD :粗體樣式常量 ,ITALIC: 斜體樣式常量.如可以如下初始化對(duì)象: // Font textFont = new Font("宋體" , Font.BOLD , 23);該字體表示23磅粗體的宋體字。 if (font == null) { // 設(shè)置字體 font = new Font("微軟雅黑", Font.PLAIN, fontSize); } // 設(shè)置字體 g.setFont(font); // 字體大小 // 計(jì)算臨時(shí)x軸 int tempX = x; // 計(jì)算臨時(shí)y軸 int tempY = y; // 驗(yàn)證輸出位置的縱坐標(biāo)和橫坐標(biāo) if (x >= srcHeight || y >= srcWidth) { // 臨時(shí)x軸為源圖片高度-文字大小+2 tempX = srcHeight - fontSize + 2; // 臨時(shí)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;// 重新計(jì)算文本輸出位置 } } 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;// 重新計(jì)算文本輸出位置 } } } 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(); // 獲取源文件擴(kuò)展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 輸出源圖片緩沖區(qū)到輸出的文件中 ImageIO.write(srcImg, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本) * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @param fontColor * 字體顏色 * @param fontSize * 字體大小 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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); } /** * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本) * * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @param fontColor * 字體顏色 * @param fontSize * 字體大小 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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); } /** * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本) * * @param srcFile * 源文件 * @param destFile * 目標(biāo)文件 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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); } /** * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本) * * @param src * 源文件路徑 * @param dest * 目標(biāo)文件路徑 * @param x * x坐標(biāo) * @param y * y坐標(biāo) * @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 * 目標(biāo)文件 * @param x * x坐標(biāo),默認(rèn)合并到左上角,當(dāng)x<0時(shí),合并到右下角 * @param y * y坐標(biāo),默認(rèn)合并到左上角,當(dāng)y<0時(shí),合并到右下角 * @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圖片緩沖區(qū) BufferedImage fromImg = ImageIO.read(frominput); // 獲取to圖片緩沖區(qū) 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 類,提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 // 它是用于在 Java(tm) 平臺(tái)上呈現(xiàn)二維形狀、文本和圖像的基礎(chǔ)類。驗(yàn)證碼生成可以用到此類。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 類擴(kuò)展了 Graphics 類, // 提供了對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。 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文件擴(kuò)展名 String toExtName = FilenameUtils.getExtension(toFile.getName()); // 輸出to圖片緩沖區(qū)到輸出的文件中 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 * 目標(biāo)文件 * @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 * 目標(biāo)文件路徑 * @param x * x坐標(biāo),默認(rèn)合并到左上角,當(dāng)x<0時(shí),合并到右下角 * @param y * y坐標(biāo),默認(rèn)合并到左上角,當(dāng)y<0時(shí),合并到右下角 * @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 * 目標(biāo)文件路徑 * @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); } }
本文為張軍原創(chuàng)文章,轉(zhuǎn)載無需和我聯(lián)系,但請(qǐng)注明來自張軍的軍軍小站,個(gè)人博客http://m.eyofj.com
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
