java實現(xiàn)在一張大圖片上添加小圖及文字
更新時間:2021年11月12日 11:17:19 作者:Mr_LGZ
這篇文章主要介紹了java實現(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);
//通過JPEG圖象流創(chuàng)建JPEG數(shù)據(jù)流解碼器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解碼當(dāng)前JPEG數(shù)據(jù)流,返回BufferedImage對象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
//得到畫筆對象
Graphics g = buffImg.getGraphics();
//小圖片的路徑
ImageIcon imgIcon = new ImageIcon(smallImgPath);
//得到Image對象。
Image img = imgIcon.getImage();
//將小圖片繪到大圖片上,5,300 .表示你的小圖片在大圖片上的位置。
g.drawImage(img, sx, sy, null);
//設(shè)置顏色。
g.setColor(Color.WHITE);
//最后一個參數(shù)用來設(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) .第一個是你設(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();
}
}
實現(xiàn)給圖片添加水印
某些應(yīng)用場景下需要對圖片加上水印防止盜用,例如微博用戶圖片。Java中實現(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();
}
}
}
}
實現(xiàn)效果如下:
原圖:

加文字水?。?/p>

加圖片水?。?/p>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Intellij IDEA 斷點不可用報錯 No executable 
這篇文章主要介紹了Intellij IDEA 斷點不可用報錯 No executable code found問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
SpringBoot整合mybatis-generator插件流程詳細(xì)講解
這篇文章主要介紹了SpringBoot整合mybatis-generator插件流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
SpringBoot配置shiro安全框架的實現(xiàn)
這篇文章主要介紹了SpringBoot配置shiro安全框架的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
java連接mysql數(shù)據(jù)庫亂碼的解決方法
這篇文章主要介紹通過java連接mysql數(shù)據(jù)庫的時候,頁面出現(xiàn)亂碼,這里簡單分享下解決方法, 需要的朋友可以參考下2013-05-05

