Java基礎(chǔ)之簡(jiǎn)單的圖片處理
一、前言
先使用一個(gè)模板圖片,在圖片上添加圖片或者文字都可以。
二、依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<optional>true</optional>
</dependency>
三、封裝數(shù)據(jù)類
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.awt.*;
/**
* 坐標(biāo)數(shù)據(jù)
* @author tyg
* @date 2021-04-23 14:33
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PositionPO {
/** 顯示的數(shù)據(jù) */
private Object data;
/** X軸坐標(biāo) */
private float x;
/** Y軸坐標(biāo) */
private float y;
/** 寬度 */
private float w;
/** 高度 */
private float h;
/** 字體 */
private Font font;
public PositionPO(Object data, float x, float y, float w, float h) {
this.data = data;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public PositionPO(Object data, float x, float y) {
this.data = data;
this.x = x;
this.y = y;
}
public PositionPO(Object data, float x, float y, Font font) {
this.data = data;
this.x = x;
this.y = y;
this.font = font;
}
public PositionPO(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}
import com.yt.distributor.po.pdf.PositionPO;
import lombok.Data;
import java.util.List;
/**
* 邀請(qǐng)海報(bào)
* @author tyg
* @date 2021-04-24 14:52
*/
@Data
public class ImageHandlePO {
/** 文字 */
private List<PositionPO> textList;
/** 圖片 */
private List<PositionPO> imageList;
public ImageHandlePO(List<PositionPO> textList, List<PositionPO> imageList) {
this.textList = textList;
this.imageList = imageList;
}
}
四、常量類
package com.yt.distributor.constant;
import org.springframework.core.io.ClassPathResource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* 圖片常量
* @author tyg
* @date 2021-04-24 16:59
*/
public class ImageConstant {
/** 透明度 */
public static final float PELLUCIDITY = 1.0F;
/** 字體 */
public static final Font FONT = new Font("微軟雅黑", Font.BOLD, 18);
/** 邀請(qǐng)海報(bào)模板圖片源文件 */
public static File POSTER_SOURCE_FILE;
/** 圖片默認(rèn)格式 */
public static final String FORMAT = "png";
static{
try {
ClassPathResource resource = new ClassPathResource("conf/poster.jpg");
POSTER_SOURCE_FILE = resource.getFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
五、圖像處理類
import com.yt.distributor.constant.ImageConstant;
import com.yt.distributor.po.img.ImageHandlePO;
import com.yt.distributor.po.pdf.PositionPO;
import lombok.extern.log4j.Log4j2;
import net.dreamlu.mica.core.utils.$;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* 圖像合成處理
* 注:圖像處理的原點(diǎn)坐標(biāo)在:左上角,距離為像素
* @author tyg
* @date 2021-04-24 17:45
*/
@Log4j2
public class PictureSynthesis {
/** 原模板圖片文件 */
public static final Object FLAG = true;
/** 原模板圖片文件 */
public static File sourceFile;
public static void main(String[] args) throws IOException {
// 生成二維碼
BufferedImage image = QrCodeGenerator.generateQrCode("http://www.baiud.com/index.html?id=13", 192, 192);
// 圖片
List<PositionPO> imageList = new ArrayList<>();
imageList.add(new PositionPO(ImageIO.read(new URL("https://thirdwx.qlogo.cn/mmopen/vi_32/AtTHbmrMict69vB7ocDMbstibgvwxpK51bOoNkQiaemrImnicUK2L9OoF1JibHiceLwY53ibiaicJQibuEwLNFicJiaYcQHRiaw/132")), 120F, 1688F, 192F, 192F));
imageList.add(new PositionPO(image, 785F, 1632F, 192F , 192F));
// 文字
Font font = new Font("微軟雅黑", Font.PLAIN, 30);
List<PositionPO> textList = new ArrayList<>();
textList.add(new PositionPO("顏魔子辰", 120F, 1660F, font));
textList.add(new PositionPO("顏魔子辰邀請(qǐng)您", 336F, 1758F, font));
textList.add(new PositionPO("加入某某小店。", 336F, 1796F, font));
textList.add(new PositionPO("長(zhǎng)按可識(shí)別二維碼", 760F, 1880F, font));
String sourcePath = "C:\\Users\\Administrator\\Desktop\\poster.jpg";
String savePath = "C:\\Users\\Administrator\\Desktop\\poster-handle.jpg";
// 輸出水印圖片
handleImage(new ImageHandlePO(textList, imageList), new File(sourcePath), savePath);
}
/**
* 圖片處理(返回輸入流)
* @param po 處理的數(shù)據(jù)
* @author tyg
* @date 2021-04-14 15:45
* @return InputStream
*/
public static InputStream handleImage(ImageHandlePO po, File sourceFile) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
//圖片處理,導(dǎo)出數(shù)據(jù)
BufferedImage image = watermark(po);
return getInputStream(image);
}
}
/**
* 圖片處理(輸出到文件中)
* @param po 處理的數(shù)據(jù)
* @param saveFilePath 保存的路徑
* @author tyg
* @date 2017年9月6日下午12:53:11
*/
public static void handleImage(ImageHandlePO po, File sourceFile, String saveFilePath) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
// 構(gòu)建疊加層
BufferedImage buffImg = watermark(po);
// 輸出水印圖片
generateWaterFile(buffImg, saveFilePath);
}
}
/**
* 構(gòu)建疊加層
* 圖像處理的原點(diǎn)坐標(biāo)在:左上角
* @param po 處理的數(shù)據(jù)
* @throws IOException io異常
* @return BufferedImage 生成水印并返回java.awt.image.BufferedImage
*/
private static BufferedImage watermark(ImageHandlePO po) throws IOException {
// 獲取底圖
BufferedImage buffImg = ImageIO.read(sourceFile);
// 創(chuàng)建Graphics2D對(duì)象,用在底圖對(duì)象上繪圖
Graphics2D g2d = buffImg.createGraphics();
// 處理文字
if ($.isNotEmpty(po.getTextList())){
for (PositionPO pp : po.getTextList()){
g2d.setColor(Color.black);
g2d.setFont( pp.getFont() == null ? ImageConstant.FONT : pp.getFont());
g2d.drawString(pp.getData().toString(), pp.getX(), pp.getY());
}
}
// 處理圖片
if ($.isNotEmpty(po.getImageList())){
for (PositionPO pp : po.getImageList()){
BufferedImage image = (BufferedImage) pp.getData();
// 獲取層圖的寬度
int width = pp.getW() <= 0 ? image.getWidth() : (int) pp.getW();
// 獲取層圖的高度
int height = pp.getH() <= 0 ? image.getHeight() : (int) pp.getH();
// 在圖形和圖像中實(shí)現(xiàn)混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ImageConstant.PELLUCIDITY));
// 繪制
g2d.drawImage(image, (int)pp.getX(), (int)pp.getY(), width, height, null);
}
}
// 釋放圖形上下文使用的系統(tǒng)資源
g2d.dispose();
return buffImg;
}
/**
* 輸出水印圖片
* @param buffImg 圖像加水印之后的BufferedImage對(duì)象
* @param savePath 圖像加水印之后的保存路徑
* @author tyg
* @date 2021-04-24 16:19
*/
private static void generateWaterFile(BufferedImage buffImg, String savePath) {
int temp = savePath.lastIndexOf(".") + 1;
try {
ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* 獲取系統(tǒng)所支持的字體
* @author tyg
* @date 2021-04-24 16:19
*/
private static void getFonts(){
String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for(String fontName:fontNames){
System.out.println(fontName);
}
}
/**
* 獲取圖片輸入流
* @param image 圖片
* @author tyg
* @date 2021-04-14 17:14
* @return java.io.InputStream
*/
public static InputStream getInputStream(BufferedImage image){
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(image, ImageConstant.FORMAT, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
log.error("提示:",e);
}
return null;
}
}
六、效果圖
以上的數(shù)據(jù)都是按圖片的1080*1920像素來(lái)設(shè)定的,下面紅框部分是動(dòng)態(tài)生成的。

到此這篇關(guān)于Java基礎(chǔ)之簡(jiǎn)單的圖片處理的文章就介紹到這了,更多相關(guān)Java圖片處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
單點(diǎn)登錄的概念及SpringBoot實(shí)現(xiàn)單點(diǎn)登錄的操作方法
在本文中,我們將使用Spring Boot構(gòu)建一個(gè)基本的單點(diǎn)登錄系統(tǒng),我們將介紹如何使用Spring Security和JSON Web Tokens(JWTs)來(lái)實(shí)現(xiàn)單點(diǎn)登錄功能,本文假設(shè)您已經(jīng)熟悉Spring Boot和Spring Security,感興趣的朋友一起看看吧2024-10-10
Java使用過(guò)濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)
這篇文章主要介紹了Java使用過(guò)濾器防止SQL注入XSS腳本注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡(jiǎn)單,首先需要安裝maven jdk,具體安裝過(guò)程跟隨小編一起看看吧2022-03-03
如何解決executors線程池創(chuàng)建的線程不釋放的問(wèn)題
這篇文章主要介紹了如何解決executors線程池創(chuàng)建的線程不釋放的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Java經(jīng)典設(shè)計(jì)模式之策略模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之策略模式,簡(jiǎn)單說(shuō)明了策略模式的概念、原理并結(jié)合實(shí)例形式分析了java策略模式的具有用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08

