Java基础之简单的图片处理
短信预约 -IT技能 免费直播动态提醒
一、前言
先使用一个模板图片,在图片上添加图片或者文字都可以。
二、依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<optional>true</optional>
</dependency>
三、封装数据类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.awt.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PositionPO {
private Object data;
private float x;
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;
@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;
public class ImageConstant {
public static final float PELLUCIDITY = 1.0F;
public static final Font FONT = new Font("微软雅黑", Font.BOLD, 18);
public static File POSTER_SOURCE_FILE;
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;
@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("颜魔子辰邀请您", 336F, 1758F, font));
textList.add(new PositionPO("加入某某小店。", 336F, 1796F, font));
textList.add(new PositionPO("长按可识别二维码", 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);
}
public static InputStream handleImage(ImageHandlePO po, File sourceFile) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
//图片处理,导出数据
BufferedImage image = watermark(po);
return getInputStream(image);
}
}
public static void handleImage(ImageHandlePO po, File sourceFile, String saveFilePath) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
// 构建叠加层
BufferedImage buffImg = watermark(po);
// 输出水印图片
generateWaterFile(buffImg, saveFilePath);
}
}
private static BufferedImage watermark(ImageHandlePO po) throws IOException {
// 获取底图
BufferedImage buffImg = ImageIO.read(sourceFile);
// 创建Graphics2D对象,用在底图对象上绘图
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();
// 在图形和图像中实现混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.class="lazy" data-src_ATOP, ImageConstant.PELLUCIDITY));
// 绘制
g2d.drawImage(image, (int)pp.getX(), (int)pp.getY(), width, height, null);
}
}
// 释放图形上下文使用的系统资源
g2d.dispose();
return buffImg;
}
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();
}
}
private static void getFonts(){
String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for(String fontName:fontNames){
System.out.println(fontName);
}
}
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;
}
}
六、效果图
以上的数据都是按图片的1080*1920像素来设定的,下面红框部分是动态生成的。
到此这篇关于Java基础之简单的图片处理的文章就介绍到这了,更多相关Java图片处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341