图片印图片2.0

上一篇是固定了坐标位置,而这种方式的限制性是很大的,当所上传的图片像素不固定时,很可能所定位的签字位置已经无法找到,所以有了2.0的改进版,即:根据像素大小动态寻找所要印的目标图片位置。

注意:在过程中遇见一个问题,(手机竖着拍一张图)所上传的图片有低概率宽是大于高的,这种情况下图片是会横过来,所以在这种情况需要把签字图片也横过来(即旋转-90度)。




import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author zhuqing
 * @Date 2021/9/10 15:09
 */
public class PicUtil {

    /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     *
     * @param iconPaths  水印图片路径
     * @param srcImgPath 源图片路径
     * @param targerPath 目标图片路径
     * @param degree     水印图片旋转角度
     * @param markAlpha
     */
    public static void markImageByIcon(List<String> iconPaths, String srcImgPath,
                                       String targerPath, Integer degree, float markAlpha) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            int srcWidth = srcImg.getWidth(null);
            int srcHeight = srcImg.getHeight(null);

            // 1、得到画笔对象
            Graphics2D g = buffImg.createGraphics();

            // 2、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(
                    srcImg.getScaledInstance(srcImg.getWidth(null),
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            if (srcWidth > srcHeight){
                degree = -90;
            }
            // 3、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            // 6、水印图片的位置
            int size = iconPaths.size();
            int x;
            int y;
            String watermarkPos = "bottomLeft";
            for (String s : iconPaths) {
                ImageIcon imgIcon = new ImageIcon(s);
                Image img = imgIcon.getImage();
                int watermarkImageWidth = img.getWidth(null);
                int watermarkImageHeight = img.getHeight(null);
                double dmarkWidth = srcWidth * 0.25;// 设置水印的宽度为图片宽度的0.25倍
                double dmarkHeight = dmarkWidth * ((double) watermarkImageHeight / (double) watermarkImageWidth);//强转为double计算宽高比例
                int imarkWidth = (int) dmarkWidth;
                int imarkHeight = (int) dmarkHeight;
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, markAlpha));
                //右下
                if (size == 1){
                    x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
                    y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
                    g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                }
                //左下 右下
                if (size == 2){
                    if (Objects.equals(watermarkPos, "bottomLeft")) {
                        x = degree == null ? 0 : 0+500;
                        y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
                        g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                        watermarkPos = "bottomRight";
                    }else if (Objects.equals(watermarkPos, "bottomRight")) {
                        x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
                        y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
                        g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                    }
                }
                //左中右
                if (size == 3){
                    if (Objects.equals(watermarkPos, "bottomLeft")) {
                        x = degree == null ? 0 : 0+500;
                        y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
                        g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                        watermarkPos = "bottomCenter";
                    } else if (Objects.equals(watermarkPos, "bottomCenter")) {
                        x = (srcWidth - imarkWidth) / 2;
                        y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
                        g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                        watermarkPos = "bottomRight";
                    } else if (Objects.equals(watermarkPos, "bottomRight")) {
                        x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
                        y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
                        g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
                    }
                }
                //左右 左右(两行)
                if (size == 4){

                }
                //左边一竖排
                if (size == 5){

                }
            }
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 7、释放资源
            g.dispose();
            // 8、生成图片
            os = new FileOutputStream(targerPath);
            ImageIO.write(buffImg, "JPG", os);
            System.out.println("图片完成添加水印图片");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}


在此记录。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为达到最佳效果,推荐使用九网互联的ASP.net空间,支持ASP.net版本1.1和2.0在线切换、在线脚本映射、ASP.net具体错误信息在线查看。功能很全asp.net图片组件源码2.0史上最牛的asp.net图片组件.详细介绍请看下面说明和实例;本人学习.net的时间也不长,才一年多,现写一个公开测试,有什么不好的地方请提出和我联系,QQ群:1640606;再将其优化升级等.//参数说明:asp.net 2.0 C#开发(2008-03-22)命名空间:using PanRoor实例化:PanRoorImageWaterMark wm = new PanRoorImageWaterMark();具体试用请根据本实例,由于效果处理试用了指针,请在web.config中加入下面项:本版本在原来的基础上进行优化升级。2008年3月22日更新如下:1.优化效果处理速度,增加了马赛克效果处理.2.优化了程序代码的执行效率.3.修改了上传时原图没有删除的情况4.修改了进行效果处理时删除原图提示正在试用的bug5.修改了原来效果图像单独存放,改为缩略图和水图一起生效.6.修改了文字阴影模糊透明度单独配置.7.修改了占用内存大的情况.现在耗用系统资源很小.8.错误捕获以弹出框的形式.9.增加了一些相应的参数配置,使其更人性化而简单.10.修改了可以支持多种图片格式.11.增加了图片裁剪功能.12.修复了经广大网友测试时反映的多处bug等.13.修复了支持任意位置和角度的旋转一些细节.由于开发到现在,测试中可能还会遇到一些问题,暂时不考虑开源,请大家原谅,性能稳定了才会开源.欢迎广大网友测试试用,遇到什么问题和我联系,我会全力以赴的将其完善.本组件的配置参数还是比较全,.有时间我做个演示给大家,有我这些参数足够用了.配置任何一项都可以做到而且要比wsimage更灵活.参数说明:GetToWaterMarkImage(pictype, wmpicwidth, wmpicheight, mininfo, minwidth, minheight, effect, mosaic, mosX, mosY, mosWidth,mosHeight, imgcut, cutX, cutY, cutWidth, cutHeight)1.pictype:水类型2.wmpicwidth:生成水图片的宽3.wmpicheight:生成水图片的高4.mininfo:bool类型,是否启用生成缩略图功能5.minwidth:缩略图的宽6.minheight:缩略图的高7.effect:设置效果处理方式8.mosaic:马赛克开关9.mosX:马赛克开始位置X10.mosY:马赛克开始位置Y11.mosWidth:马赛克范围宽12.mosHeight:马赛克范围高13.imgcut:图片裁剪开关14.cutX:裁剪的开始位置X15.cutY:裁剪的开始位置Y16.cutWidth:裁剪范围宽17.cutHeight:裁剪范围高Config_XML.aspx为水配置页面uploadimages.aspx为上传图片页面该组件如需要用于商业用途请与作者联系作者:zhtvadvanced,发布者:zhtvadvanced
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值