java在图片上添加文字

摘要:
业务需求要在图片上添加水印。

业务需求要在图片上添加水印。下面粘出代码供自己和大家分享

packagecom.pro.drawTextOnImg;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.FontMetrics;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.geom.Rectangle2D;
importjava.awt.image.BufferedImage;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.ImageIcon;
importorg.apache.commons.lang3.StringUtils;
public classTestImg {
    public static voidmain(String[] args) {
        String filePath = "d:\inimg03.png";
        String outPath = "d:\2.jpg";
        drawTextInImg(filePath, outPath, new FontText("中国", 1, "#CC2BAC", 40, "黑体"));
    }
    public static voiddrawTextInImg(String filePath,String outPath, FontText text) {
        ImageIcon imgIcon = newImageIcon(filePath);
        Image img =imgIcon.getImage();
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bimage = newBufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g =bimage.createGraphics();
        g.setColor(getColor(text.getWm_text_color()));
        g.setBackground(Color.white);
        g.drawImage(img, 0, 0, null);
        Font font = null;
        if(StringUtils.isEmpty(text.getWm_text_font())
                && text.getWm_text_size() != null) {
            font = newFont(text.getWm_text_font(), Font.BOLD,
                    text.getWm_text_size());
        } else{
            font = new Font(null, Font.BOLD, 15);
        }
        g.setFont(font);
        FontMetrics metrics = newFontMetrics(font){};
        Rectangle2D bounds = metrics.getStringBounds(text.getText(), null);
        int textWidth = (int) bounds.getWidth();
        int textHeight = (int) bounds.getHeight();
        int left = 0;
        int top =textHeight;
        //九宫格控制位置
        if(text.getWm_text_pos()==2){
            left = width/2;
        }
        if(text.getWm_text_pos()==3){
            left = width -textWidth;
        }
        if(text.getWm_text_pos()==4){
            top = height/2;
        }
        if(text.getWm_text_pos()==5){
            left = width/2;
            top = height/2;
        }
        if(text.getWm_text_pos()==6){
            left = width -textWidth;
            top = height/2;
        }
        if(text.getWm_text_pos()==7){
            top = height -textHeight;
        }
        if(text.getWm_text_pos()==8){
            left = width/2;
            top = height -textHeight;
        }
        if(text.getWm_text_pos()==9){
            left = width -textWidth;
            top = height -textHeight;
        }
        g.drawString(text.getText(), left, top);
        g.dispose();
        try{
            FileOutputStream out = newFileOutputStream(outPath);
            ImageIO.write(bimage, "JPEG", out);
            out.close();
        } catch(FileNotFoundException e) {
            //TODO Auto-generated catch block
e.printStackTrace();
        } catch(IOException e) {
            //TODO Auto-generated catch block
e.printStackTrace();
        }
    }
    //color #2395439
    public staticColor getColor(String color) {
        if (color.charAt(0) == '#') {
            color = color.substring(1);
        }
        if (color.length() != 6) {
            return null;
        }
        try{
            int r = Integer.parseInt(color.substring(0, 2), 16);
            int g = Integer.parseInt(color.substring(2, 4), 16);
            int b = Integer.parseInt(color.substring(4), 16);
            return newColor(r, g, b);
        } catch(NumberFormatException nfe) {
            return null;
        }
    }
}

下面是FontText里面主要是水印的基本信息

packagecom.pro.drawTextOnImg;
public classFontText {
    privateString text;
    private intwm_text_pos;
    privateString wm_text_color;
    privateInteger wm_text_size;
    private String wm_text_font;//字体  “黑体,Arial”
    publicString getText() {
        returntext;
    }
    public voidsetText(String text) {
        this.text =text;
    }
    public intgetWm_text_pos() {
        returnwm_text_pos;
    }
    public void setWm_text_pos(intwm_text_pos) {
        this.wm_text_pos =wm_text_pos;
    }
    publicString getWm_text_color() {
        returnwm_text_color;
    }
    public voidsetWm_text_color(String wm_text_color) {
        this.wm_text_color =wm_text_color;
    }
    publicInteger getWm_text_size() {
        returnwm_text_size;
    }
    public voidsetWm_text_size(Integer wm_text_size) {
        this.wm_text_size =wm_text_size;
    }
    publicString getWm_text_font() {
        returnwm_text_font;
    }
    public voidsetWm_text_font(String wm_text_font) {
        this.wm_text_font =wm_text_font;
    }
    public FontText(String text, intwm_text_pos, String wm_text_color,
            Integer wm_text_size, String wm_text_font) {
        super();
        this.text =text;
        this.wm_text_pos =wm_text_pos;
        this.wm_text_color =wm_text_color;
        this.wm_text_size =wm_text_size;
        this.wm_text_font =wm_text_font;
    }
    publicFontText(){}
}

免责声明:文章转载自《java在图片上添加文字》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C# 从Excel表中导入数据函数到DataSetVMware虚拟机中配置静态IP的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python学习杂记_1_PyCharm使用的一些收获

一. 界面及字体的调整 装好PyCharm默认的界面是白色的,编辑区域和Console区域的字体也比较小。我个人比较喜欢界面是黑底的,主要关注区域上的字体,大一些,看着清楚一些。调整办法是这样滴~! 点击 File->Settings 打开Settings窗口,所有设置皆在此,结构也非常清晰,找呗…… 1. 黑背景   这个和选择的主题有关,Sett...

Java 8 (10) CompletableFuture:组合式异步编程

https://www.cnblogs.com/baidawei/p/9447737.html   随着多核处理器的出现,提升应用程序的处理速度最有效的方式就是可以编写出发挥多核能力的软件,我们已经可以通过切分大型的任务,让每个子任务并行运行,使用线程的方式,分支/合并框架(java 7) 和并行流(java 8)来实现。 现在很多大型的互联网公司都对外...

Java: 获取当前执行位置的文件名/类名/方法名/行号

在 JAVA 程序有时需要获取当前代码位置, 于是就利用 Thread.currentThread().getStackTrace() 写了下面这个工具类, 用来获取当前执行位置处代码的文件名/类名/方法名/行号. 当然通过 new Throwable().getStackTrace() 也能得到同样信息, 在处理异常时用这种方法还行, 否则需要 new...

读取Excel文件利器LinqToExcel,比NOPI好用

由于项目要用到读excel文件存入数据库中,前一段时间也找到了NOPI试了一下,觉得还好,但是读取xlsx就报错  之后本人在google上找到了LinqToExcel 先上代码  private void LinqToExcel(string fileName)         {             var excelFile = new Exc...

常用php操作redis命令整理(三)LIST类型

LIST 头元素和尾元素:头元素指的是列表左端/前端第一个元素,尾元素指的是列表右端/后端第一个元素。举个例子,列表list包含三个元素:x, y, z,其中x是头元素,而z则是尾元素。空列表:指不包含任何元素的列表,Redis将不存在的key也视为空列表。 LPUSH 将一个或多个值value插入到列表key的表头。如果key不存在,一个空列表会被创建并...

[爬虫]采用Go语言爬取天猫商品页面

最近工作中有一个需求,需要爬取天猫商品的信息,整个需求的过程如下: 修改后端广告交易平台的代码,从阿里上传的素材中解析url,该url格式如下: https://handycam.alicdn.com/slideshow/26/7ef5aed1e3c39843e8feac816a436ecf.mp4?content=%7B%22items%22%3A%5B...