比如生成证书的时候,可能模板图片里面已经有印章了,我们如何把文字添加上去后再把印章盖回去呢?
可以通过操纵像素点来完成
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class WithLayersBufferedImage {
private BufferedImage image;
private Graphics2D graphics2D;
public WithLayersBufferedImage(InputStream inputStream) throws IOException {
image = ImageIO.read(inputStream);
graphics2D = bufferedImage.createGraphics();
}
/**
* 调整尺寸.
*
*/
public void reSize(int width, int height) {
BufferedImage outputImage = new BufferedImage(width, height, image.getType());
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawImage(image, 0, 0, width, height, null);
this.image = outputImage;
this.graphics2D = graphics;
}
/**
* 写证书文字 默认黑体 加粗.
*/
public void writeFont(int x, int y, String str, int size) {
Font font = getSimHeiFont(size);
this.graphics2D.setFont(font);
this.graphics2D.setColor(Color.BLACK);
String s = new String(str.getBytes(Charset.forName("UTF-8")), Charset.forName("UTF-8"));
this.graphics2D.drawString(s, x, y);
}
/**
* 保持印章在上层.
*
* @param minX the min x 要保持图层最上层的区域坐标,区域越小需要遍历的像素点越少
* @param maxX the max x 要保持图层最上层的区域坐标,区域越小需要遍历的像素点越少
* @param minY the min y 要保持图层最上层的区域坐标,区域越小需要遍历的像素点越少
* @param maxY the max y 要保持图层最上层的区域坐标,区域越小需要遍历的像素点越少
* @param judgeRedFunc 判断方法,判断哪些像素点是要保持上层的,对于印章来说就是红色的像素,传null就行,其他的自行写判断方法
* @param consumer 实际的图片操作
*/
public void KeepSealOnUpper(int minX, int maxX, int minY, int maxY, Function<Color, Boolean> judgeRedFunc, Consumer<WithLayersBufferedImage> consumer) {
if (judgeRedFunc == null) {
//如果是红色 就存起来 这里比较粗暴的判断三色原里,红色大于200 其他二色小于100就算印章的颜色(如果自己模板图片的颜色非常相近则需要好好调节)
judgeRedFunc = color -> color.getRed() > 200 && color.getGreen() < 100 && color.getBlue() < 100;
}
//先把这区域内符合的像素点都保存起来
List<int[]> l = new ArrayList<>();
for (int w = minX; w <= maxX; w++) {
for (int h = minY; h <= maxY; h++) {
int pixel = image.getRGB(w, h);
Color color = new Color(pixel);
if (judgeRedFunc.apply(color)) {
l.add(new int[]{w, h, pixel});
}
}
}
//执行画图操作 可以写文字或画图片之类
consumer.accept(this);
//重新覆盖像素点回去
for (int[] a : l) {
int pixel = image.getRGB(a[0], a[1]);
//有变化
if (pixel != a[2]) {
//红色被人覆盖了
Color color = new Color(a[2]);
Color c = new Color(color.getRed(), color.getGreen(), color.getBlue(), 200);//设置点透明度,这样更真实
image.setRGB(a[0], a[1], c.getRGB());
}
}
}
/**
* 写出.
*
* @param format jpeg png
* @param outputStream the output stream
* @throws IOException the io exception
*/
public void output(String format, OutputStream outputStream) throws IOException {
graphics2D.dispose();
ImageIO.write(image, format, outputStream);
}
}
使用
public class Test{
public void main(String[] args){
InputStream inputStream = new ByteArrayInputStream(fileContent);
WithLayersBufferedImage ci = new WithLayersBufferedImage (inputStream);
ci.reSize(1000, 800);
ci.writeFont(200, 400,"xxxxxx", 30);
ci.writeFont(900, 300, "xxxxxx", 34);
ci.writeFont(300, 400, "xxxxxx", 50);
//设置证书日期
ci.KeepSealOnUpper(800, 1000, 700, 750, null, (s) -> {
s.writeFont(850, 730, "xxxx年xx月xx日", 30);
});
ByteArrayOutputStream os = new ByteArrayOutputStream();
ci.output(imageType, os);
}
}
同理,这稍加改造也可以用于分离图层等操作