java實(shí)現(xiàn)在一張大圖片上添加小圖及文字
在一張大圖上添加小圖及文字
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageDecoder; import com.sun.image.codec.jpeg.JPEGImageEncoder; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.text.ParseException; public class ImgUtil { public static void main(String[] args) throws ParseException { String bigImg = "C:\\Users\\langz\\Desktop\\ffff\\big.jpg"; String smallImg = "C:\\Users\\langz\\Desktop\\ffff\\small.jpg"; String content = "好久不見,你還好嗎"; String outPath = "C:\\Users\\langz\\Desktop\\ffff\\" + System.currentTimeMillis() + ".jpg"; try { bigImgAddSmallImgAndText(bigImg, smallImg, 500, 500, null, 200, 200, outPath); } catch (IOException e) { e.printStackTrace(); } } /*** * 在一張大圖張?zhí)砑有D和文字 * @param bigImgPath 大圖的路徑 * @param smallImgPath 小圖的路徑 * @param sx 小圖在大圖上x抽位置 * @param sy 小圖在大圖上y抽位置 * @param content 文字內(nèi)容 * @param cx 文字在大圖上y抽位置 * @param cy 文字在大圖上y抽位置 * @param outPathWithFileName 結(jié)果輸出路徑 */ public static void bigImgAddSmallImgAndText(String bigImgPath , String smallImgPath, int sx, int sy , String content, int cx, int cy , String outPathWithFileName) throws IOException { //主圖片的路徑 InputStream is = new FileInputStream(bigImgPath); //通過(guò)JPEG圖象流創(chuàng)建JPEG數(shù)據(jù)流解碼器 JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is); //解碼當(dāng)前JPEG數(shù)據(jù)流,返回BufferedImage對(duì)象 BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage(); //得到畫筆對(duì)象 Graphics g = buffImg.getGraphics(); //小圖片的路徑 ImageIcon imgIcon = new ImageIcon(smallImgPath); //得到Image對(duì)象。 Image img = imgIcon.getImage(); //將小圖片繪到大圖片上,5,300 .表示你的小圖片在大圖片上的位置。 g.drawImage(img, sx, sy, null); //設(shè)置顏色。 g.setColor(Color.WHITE); //最后一個(gè)參數(shù)用來(lái)設(shè)置字體的大小 if (content != null) { Font f = new Font("宋體", Font.PLAIN, 25); Color mycolor = Color.red;//new Color(0, 0, 255); g.setColor(mycolor); g.setFont(f); g.drawString(content, cx, cy); //表示這段文字在圖片上的位置(cx,cy) .第一個(gè)是你設(shè)置的內(nèi)容。 } g.dispose(); OutputStream os = new FileOutputStream(outPathWithFileName); //創(chuàng)鍵編碼器,用于編碼內(nèi)存中的圖象數(shù)據(jù)。 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); en.encode(buffImg); is.close(); os.close(); } }
實(shí)現(xiàn)給圖片添加水印
某些應(yīng)用場(chǎng)景下需要對(duì)圖片加上水印防止盜用,例如微博用戶圖片。Java中實(shí)現(xiàn)添加水印需要用到BufferedImage、Graphics2D 和ImageIO類。
1. 添加文字水印
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.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; /** * 添加文字水印 * * @author Ricky Fung */ public class TextMarkProcessor { /** * @param args */ public static void main(String[] args) { new TextMarkProcessor().testTextMark(); } public void testTextMark() { File srcImgFile = new File("D:/test/desktop.png"); String logoText = "[ 天使的翅膀 ]"; File outputRotateImageFile = new File("D:/test/desktop_text_mark.jpg"); createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 0); } public void createWaterMarkByText(File srcImgFile, String logoText, File outputImageFile, double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImgFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = buffImg.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (degree>0) { graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } graphics.setColor(Color.RED); graphics.setFont(new Font("宋體", Font.BOLD, 36)); float alpha = 0.8f; graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); graphics.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2); graphics.dispose(); os = new FileOutputStream(outputImageFile); // 生成圖片 ImageIO.write(buffImg, "JPG", os); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } }
2. 添加圖片水印
import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * 添加圖片水印 * * @author Ricky Fung */ public class PictureMarkProcessor { /** * @param args */ public static void main(String[] args) { new PictureMarkProcessor().testPictureMark(); } public void testPictureMark() { File srcImageFile = new File("D:/test/desktop.png"); File logoImageFile = new File("D:/test/tools.png"); File outputRoateImageFile = new File("D:/test/desktop_pic_mark.jpg"); createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 0); } public void createWaterMarkByIcon(File srcImageFile, File logoImageFile, File outputImageFile, double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImageFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = buffImg.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile)); Image logoImg = logoImgIcon.getImage(); //旋轉(zhuǎn) if (degree>0) { graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getWidth() / 2); } float alpha = 0.8f; // 透明度 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); //水印 的位置 graphics.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null); graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); graphics.dispose(); os = new FileOutputStream(outputImageFile); // 生成圖片 ImageIO.write(buffImg, "JPG", os); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } }
實(shí)現(xiàn)效果如下:
原圖:
加文字水?。?/p>
加圖片水?。?/p>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot的優(yōu)點(diǎn)及項(xiàng)目創(chuàng)建步驟詳解
這篇文章主要介紹了Spring?Boot的優(yōu)點(diǎn)及項(xiàng)目創(chuàng)建步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Java對(duì)日期Date類進(jìn)行加減運(yùn)算、年份加減月份加減、時(shí)間差等等
這篇文章主要介紹了Java對(duì)日期Date類進(jìn)行加減運(yùn)算、年份加減月份加減、時(shí)間差等等,在網(wǎng)上查閱資料,加上自己總結(jié)的一些關(guān)于Date類的工具類2017-01-01JAVA連接到SQLserver的步驟方法以及遇到的問(wèn)題
java相對(duì)于其他語(yǔ)言(例如c,c++等)連接數(shù)據(jù)庫(kù)要方便得多,下面這篇文章主要給大家介紹了關(guān)于JAVA連接到SQLserver的步驟方法及遇到的問(wèn)題,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06自定義類加載器的父類為何是AppClassLoader說(shuō)明
這篇文章主要介紹了自定義類加載器的父類為何是AppClassLoader說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11java根據(jù)模板導(dǎo)出PDF的詳細(xì)實(shí)現(xiàn)過(guò)程
前段時(shí)間因?yàn)橄嚓P(guān)業(yè)務(wù)需求需要后臺(tái)生成pdf文件,所以下面這篇文章主要給大家介紹了關(guān)于java根據(jù)模板導(dǎo)出PDF的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02Java面試Socket編程常用參數(shù)設(shè)置源碼問(wèn)題分析
這篇文章主要為大家介紹了Java編程中關(guān)于Socket結(jié)構(gòu)分析,常用參數(shù)設(shè)置源碼示例以及面試中的問(wèn)題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗(yàn)詳解
這篇文章主要介紹了Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗(yàn)詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼
本文主要介紹了基于Springboot實(shí)現(xiàn)JWT認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11