13,097
社区成员




相同的代码另一个就好好的这个就不行,就是一张图片如何在图片上吧 md 的写上去 public static BufferedImage singleWatermarkImage(String md,int width,int height) { //md="sdgnewfewgo" //定义图片宽度和高度 /* int width = 5; int height = 5;*/ Font font = new Font("微软雅黑", Font.BOLD, 1); BufferedImage img = new BufferedImage(width, height, TYPE_INT_ARGB); Graphics2D loGraphic = img.createGraphics(); //获取文本size FontMetrics loFontMetrics = loGraphic.getFontMetrics(font); int liStrWidth = loFontMetrics.stringWidth("md"); int liStrHeight = loFontMetrics.getHeight(); //文本显示样式及位置 loGraphic.setColor(Color.white); loGraphic.fillRect(0, 0, (int) width, (int) height); loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); //倾斜度设置 loGraphic.rotate(Math.toRadians(-0)); loGraphic.translate(-((int) width - liStrWidth) / 4, -((int) height - liStrHeight) / 4); loGraphic.setFont(font); loGraphic.setColor(Color.black); loGraphic.drawString(md, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); loGraphic.dispose(); return img; }
这个是好的 就是图片的计算方式不同 。求大佬解惑
public static BufferedImage tileWatermarkImage(String text, Font font, Color textColor, double height, double width, int angle) { //定义图片宽度和高度 BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB); Graphics2D loGraphic = img.createGraphics(); //获取文本size FontMetrics loFontMetrics = loGraphic.getFontMetrics(font); int liStrWidth = loFontMetrics.stringWidth(text); int liStrHeight = loFontMetrics.getHeight(); //文本显示样式及位置 loGraphic.setColor(Color.white); loGraphic.fillRect(0, 0, (int) width, (int) height); loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); //倾斜度设置 loGraphic.rotate(Math.toRadians(-angle)); loGraphic.translate(-((int) width - liStrWidth) / 4, -((int) height - liStrHeight) / 4); loGraphic.setFont(font); loGraphic.setColor(textColor); loGraphic.drawString(text, ((int) width - liStrWidth) / 4, ((int) height - liStrHeight) / 2); loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 4); loGraphic.drawString(text, ((int) width - liStrWidth) / 6, ((int) height - liStrHeight) / 50); loGraphic.dispose(); return img; }