用Java实现给图片添加文字

程序背景

为实现干部培训平台证书。

在网络中的很多地方都有水印的存在,比如微信公众号上面的图片,微博,以及这个CSDN博客上面的图片……所以突发奇想,看看自己能否写一个可以给图片添加水印的工具类。

程序代码

package image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

/**
 * @use 利用Java代码给图片加水印
 */
public class WaterMarkUtils {

    /**
     * @param srcImgPath 源图片路径
     * @param tarImgPath 保存的图片路径
     * @param waterMarkContent 水印内容
     * @param markContentColor 水印颜色
     * @param font 水印字体
     */
    public void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent,Color markContentColor,Font font) {

        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font);              //设置字体

            //设置水印的坐标
            int x = srcImgWidth - 2*getWatermarkLength(waterMarkContent, g);
            int y = srcImgHeight - 2*getWatermarkLength(waterMarkContent, g);
            g.drawString(waterMarkContent, x, y);  //画出水印
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();  

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
    public static void main(String[] args) {
        Font font = new Font("微软雅黑", Font.PLAIN, 35);                     //水印字体
        String srcImgPath="H:/安静时写写/写写博客/Java实现给图片添加水印/s.jpg"; //源图片地址
        String tarImgPath="H:/安静时写写/写写博客/Java实现给图片添加水印/t.jpg"; //待存储的地址
        String waterMarkContent="图片来源:http://blog.csdn.net/zjq_1314520";  //水印内容
        Color color=new Color(255,255,255,128);                               //水印图片色彩以及透明度
        new WaterMarkUtils().addWaterMark(srcImgPath, tarImgPath, color, waterMarkContent,font);

    }
}

最后效果

原文地址:https://www.cnblogs.com/remember-forget/p/8134823.html

时间: 2024-08-24 23:25:41

用Java实现给图片添加文字的相关文章

php 图片添加文字水印 以及 图片合成(微信快码传播)

1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPath)); $font = 'msyhl.ttc';//字体 $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB $fontSize = 20; //字体大小 $circleSize = 60; //旋转角度 $left = 50; //左边距

php给图片添加文字水印方法汇总

在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_ex

php图片添加文字水印方法汇总

方法一: <?php header("content-type:text/html;charset=utf-8"); //指定图片路径 $src = "img/a.png"; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_extension($info[2],false); // echo $type; // exit; //动态的把图片导入内存中 $fun = &qu

php给图片添加文字水印

PHP对图片的操作用到GD库,这里我们介绍如何给图片添加文字水印. 大致分为四步: 1.打开图片 2.操作图片 3.输出图片 4.销毁图片 下面我们上代码来具体讲解每步的实现过程: <?php /*打开图片*/ //1.配置图片路径 $src = "bg.jpg"; //2.获取图片信息 $info = getimagesize($src); //3.通过编号获取图像类型 $type = image_type_to_extension($info[2],false); //4.在

C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域

需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小单位与后端没什么关系,所以不能直接传字体大小,也就是后端要根据矩形区域以及文字内容来自己推算用什么样的字体大小合适. 简单说就是知道文字的矩形区域,以及文字内容,要让文字内容根据矩形区域大小调整到适合的字体大小能比较合适地填满这个区域. 分析&思路 Graphics 类有个 MeasureStrin

如何给gif图片添加文字?GIF图片添加文字教程

我们经常会在网上下载一下GIF动态图片,有时候我们想给我们下载的GIF动态图片添加文字,这时候该怎么操作,下面小编就来分享一下给GIF图片添加文字的教程给大家,供大家参考和学习,希望大家都是能够满意的. GIF格式可以存多幅彩色图像,如果把存于一个文件中的多幅图像数据逐幅读出并显示到屏幕上,就可构成一种最简单的动画. 迅捷GIF制作工具添加文字就是将我们添加进来GIF图片逐帧分解成若干张图片,然后我们批量的在这些图片上添加文字. 迅捷GIF制作工具http://www.xunjieshipin.

图片添加文字水印 和图片水印

<?php /** * @desc 图片处理类 */ class Pic{ private $info; private $res; public $thumb_pic; public function __construct($picPath){ //获取图片信息 $this->info = getimagesize($picPath); //获取图片名 $this->info['type'] = image_type_to_extension($this->info[2],fa

PIL图片添加文字水印

python PIL图像处理中添加文字水印 代码如下: 运行效果图: 原文地址:https://www.cnblogs.com/VYao/p/9753502.html

给图片添加文字水印

public void ImageWaterMarkText(string filename, ImageFormat format) { Image originalImage = Image.FromFile(filename); Graphics g = Graphics.FromImage(originalImage); Font drawFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel); /