java处理图片 添加文字或图片水印并可生成缩略图

ImageHelper.java

复制代码
package com.test1433;

import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorModel;
    import java.awt.image.WritableRaster;
    import java.io.File;
    import java.io.FileOutputStream;
    import javax.imageio.ImageIO;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    public class ImageHelper {
     
/**
 * 生成缩略图 <br/>保存:ImageIO.write(BufferedImage, imgType[jpg/png/...], File);
 *
 * @param source
 *            原图片
 * @param width
 *            缩略图宽
 * @param height
 *            缩略图高
 * @param b
 *            是否等比缩放
 * */
public static BufferedImage Thumb(BufferedImage source, int width,
        int height, boolean b) {
// targetW,targetH分别表示目标长和宽
                int type = source.getType();
                BufferedImage target = null;
                double sx = (double) width / source.getWidth();
                double sy = (double) height / source.getHeight();
 
                if (b) {
                        if (sx > sy) {
                                sx = sy;
                                width = (int) (sx * source.getWidth());
                        } else {
                                sy = sx;
                                height = (int) (sy * source.getHeight());
                        }
                }
                 
                if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(width,
                        height);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
                } else
        target = new BufferedImage(width, height, type);
                Graphics2D g = target.createGraphics();
// smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                        RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
        }
 
        /**
 * 图片水印
 *
 * @param imgPath
 *            待处理图片
 * @param markPath
 *            水印图片
 * @param x
 *            水印位于图片左上角的 x 坐标值
 * @param y
 *            水印位于图片左上角的 y 坐标值
 * @param alpha
 *            水印透明度 0.1f ~ 1.0f
 * */
public static void waterMark(String imgPath, String markPath, int x, int y,
                float alpha) {
        try {
        // 加载待处理图片文件
             Image img = ImageIO.read(new File(imgPath));
 
             BufferedImage image = new BufferedImage(img.getWidth(null), img
                                        .getHeight(null), BufferedImage.TYPE_INT_RGB);
             Graphics2D g = image.createGraphics();
             g.drawImage(img, 0, 0, null);
 
             // 加载水印图片文件
             Image src_biao = ImageIO.read(new File(markPath));
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                     alpha));
             g.drawImage(src_biao, x, y, null);
                        g.dispose();
         // 保存处理后的文件
        FileOutputStream out = new FileOutputStream(imgPath);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);
        out.close();
        } catch (Exception e) {
                e.printStackTrace();
        }
}
 
        /**
 * 文字水印
 *
 * @param imgPath
 *            待处理图片
 * @param text
 *            水印文字
 * @param font
 *            水印字体信息
 * @param color
 *            水印字体颜色
 * @param x
 *            水印位于图片左上角的 x 坐标值
 * @param y
 *            水印位于图片左上角的 y 坐标值
 * @param alpha
 *            水印透明度 0.1f ~ 1.0f
 */
 
public static void textMark(String imgPath, String text, Font font,
                Color color, int x, int y, float alpha) {
        try {
            Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;
         
            Image img = ImageIO.read(new File(imgPath));
         
            BufferedImage image = new BufferedImage(img.getWidth(null), img
                                                .getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
         
           g.drawImage(img, 0, 0, null);
           g.setColor(color);
           g.setFont(Dfont);
           g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                                                alpha));
           g.drawString(text, x, y);
           g.dispose();
           FileOutputStream out = new FileOutputStream(imgPath);
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
           encoder.encode(image);
           out.close();
          } catch (Exception e) {
                System.out.println(e);
          }
        }
    }
复制代码

测试代码

复制代码
package com.test1433;

import java.awt.Color;
import java.awt.Font;

public class TestPic {
    
    public static void main(String[] args) {
        Font font = new Font("宋体",50,60);
        Color color = new Color(0,0,0);
        ImageHelper.textMark("c://2012.jpg","http://www.baidu.com",font,color,20,40,1f);
    }
 
}
复制代码

 

posted on   癫狂编程  阅读(339)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2017-10-25 如何禁止复制电脑文件到U盘、禁止U盘拷贝文件
2017-10-25 软件破解入门(暴力破解CrackMe)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示