shaw-肖班 2016-08-18 09:16 采纳率: 0%
浏览 3971

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; } }
  • 写回答

3条回答 默认 最新

  • shaw-肖班 2016-08-18 09:20
    关注

    中间那块代码
    @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();
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号