java Graphics绘制水印效果如何使得字体倾斜。谢谢大神

在图片上面利用Graphics中的drawstring如何使得字体倾斜,各位大神帮帮忙哟。
即,把文本信息输入到图片上,形成水印的效果。

package com.mmspush.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

  • @see 处理图片的工具类
  • @version 1.0
  • */
    public class ImageTool implements Serializable {

    public static void main(String[] args) {
    for(int i=1;i<10;i++){//0,8,156,120
    bean.fontImg("D:\caixin\20160804_分公司_"+i+".png","E:\mmsutil\caixin\20160804_分公司_"+i+".png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(17,57,101,120), 100, 130);// 把字符印到图片上

    }
    bean.fontImg("D:\caixin\20160804_收入_1.png","E:\mmsutil\caixin\20160804_收入_1.png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(0,8,156,120), 100, 120);// 把字符印到图片上
    bean.fontImg("D:\caixin\20160804_客户_2.png","E:\mmsutil\caixin\20160804_客户_2.png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(0,8,156,120), 100, 120);// 把字符印到图片上
    bean.fontImg("D:\caixin\20160804_4G发展_3.png","E:\mmsutil\caixin\20160804_4G发展_3.png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(0,8,156,120), 100, 120);// 把字符印到图片上
    bean.fontImg("D:\caixin\20160804_流量_4.png","E:\mmsutil\caixin\20160804_流量_4.png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(0,8,156,120), 100, 120);// 把字符印到图片上
    bean.fontImg("D:\caixin\20160804_宽带发展_5.png","E:\mmsutil\caixin\20160804_宽带发展_5.png","13805007960,8888888", new Font("宋体", Font.PLAIN, 18), new Color(0,8,156,120), 100, 120);// 把字符印到图片上
    }
    private static final long serialVersionUID = 1L;

    /**

    • @see 把字符印到图片上
    • @param imgPath 图片路径(物理路径)
    • @param text 准备的字符
    • @param font 字体样式
    • @param color 字体颜色
    • @param left 图片的x坐标 (印的位置)
    • @param top 图片的y坐标 (印的位置)
    • @return */ @SuppressWarnings("restriction") public void fontImg(String imgPath,String newPath, String text, Font font, Color color, int left, int top) { try { char[] chrCharArray; BufferedImage src = ImageIO.read(new File(imgPath)); int width = src.getWidth(); int height = src.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graph = image.createGraphics(); graph.drawImage(src, 0, 0, width, height, null); graph.setColor(color); graph.setFont(font); chrCharArray=text.toCharArray(); top=height/2+40; left=width/3; for(int i=0;i<chrCharArray.length;i++){ graph.drawString(""+chrCharArray[i]+"".toString(), left, top); left+=8; top-=3; } graph.dispose(); FileOutputStream out = new FileOutputStream(newPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); image.flush(); src.flush(); } catch (Exception e) { e.printStackTrace(); } }

    private static ImageTool bean = new ImageTool();

    /**

    • @see 以单例模式创建,获得对象实例
    • @return Image */ public static ImageTool getBean() { return bean; } }

2个回答

中间那块代码
@SuppressWarnings("restriction")
public void fontImg(String imgPath,String newPath, String text, Font font, Color color, int left, int top) {
try {
char[] chrCharArray;
BufferedImage src = ImageIO.read(new File(imgPath));
int width = src.getWidth();
int height = src.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graph = image.createGraphics();
graph.drawImage(src, 0, 0, width, height, null);
graph.setColor(color); graph.setFont(font);
chrCharArray=text.toCharArray();
top=height/2+40;
left=width/3;
for(int i=0;i<chrCharArray.length;i++)
{
graph.drawString(""+chrCharArray[i]+"".toString(), left, top);
left+=8; top-=3;
}
graph.dispose();
FileOutputStream out = new FileOutputStream(newPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
image.flush();
src.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

了解Python的实现,Java这方面的处理不是很了解。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐