java,根据图片去生成图片,图片合成,签名图写入图片
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; public class PasqsUtil { private static String 个人姓名 = "张三"; private static String 身份证号码 = "123123123123"; private static String 联系地址 = "123123123123"; private static String 联系电话 = "123123123123"; private static String 用户ID = "123123123123"; private static String 签署日期 = "2024-04-05"; public static void main(String[] args) throws URISyntaxException, IOException { String 文件输出地址="D:\\"; URL 模板 = new URL("http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960"); URL 出险人签名 = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"); generalImg(模板.openStream(), 文件输出地址, 个人姓名, 身份证号码, 联系地址, 联系电话, 小程序用户ID, 签署日期, 出险人签名); } public static String generalImg(InputStream 模板, String 文件输出地址, String 个人姓名, String 身份证号码, String 联系地址, String 联系电话, String 用户ID, String 签署日期, URL 出险人签名 ) throws IOException { try { // 读取原始图片 BufferedImage originalImage = ImageIO.read(模板); // 创建Graphics对象,用于在原始图片上绘制水印 Graphics2D graphics = originalImage.createGraphics(); graphics.setColor(Color.black); // 设置水印颜色 graphics.setFont(new Font("宋体", Font.PLAIN, 24)); // 设置水印字体和大小 graphics.drawString(个人姓名, 450, 425); graphics.drawString(身份证号码, 450, 445); graphics.drawString(联系地址, 450, 465); graphics.drawString(联系电话, 450, 485); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); BufferedImage 出险人签名Img = ImageIO.read(出险人签名); ColorUtil.transparentColor(出险人签名Img); graphics.drawImage(出险人签名Img, 450, 200, 320, 180,null); // 保存修改后的图片 SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd_HH时mm分ss"); String da = sf.format(new Date()); String outFileName = da + ".png"; File out = new File(文件输出地址 + outFileName); ImageIO.write(originalImage, "png", out); return outFileName; } catch (Exception e) { e.printStackTrace(); throw e; } } }
附:里面用到的一个类
import java.awt.*; import java.awt.image.BufferedImage; public class ColorUtil { public static boolean isTransparent(BufferedImage bufImg) { int height = bufImg.getHeight(); int width = bufImg.getWidth(); boolean isTransparent = false; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int pixel = bufImg.getRGB(i, j); if (pixel >> 24 == 0) { isTransparent = true; break; } } } return isTransparent; } public static void transparentColor(BufferedImage bufferedImage) { //图片本身透明,则不在做背景透明处理 if(bufferedImage==null||isTransparent(bufferedImage)){ return; } Color transparentColor = new Color(bufferedImage.getRGB(0, 0)); // 遍历图片的每个像素 for (int x = 0; x < bufferedImage.getWidth(); x++) { for (int y = 0; y < bufferedImage.getHeight(); y++) { // 获取当前像素的颜色 Color currentColor = new Color(bufferedImage.getRGB(x, y)); // 如果当前像素的颜色与要设为透明的颜色足够接近 if (currentColor.equals(transparentColor)) { // 设置该像素为透明 bufferedImage.setRGB(x, y, new Color(0, 0, 0, 0).getRGB()); } } } } }
服务器可能出现乱码:
那是因为服务器支持的字体,答题如下:
Dialog,DialogInput,Lucida Bright,Lucida Sans,Lucida Sans Typewriter,Monospaced,SansSerif,Serif,
不包含 宋体
得下载个支持宋体的字体ttf文件丢上去识别
Font font = null; try { font = Font.createFont(Font.TRUETYPE_FONT, new File("/opt/ziti_songti.ttf")).deriveFont(24f); } catch (FontFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(font); //...
graphics.setFont(font); // 设置水印字体和大小,docker里不支持【宋体】这个编码
https://blog.csdn.net/weixin_66994393/article/details/135933388

浙公网安备 33010602011771号