修改ueditor1_4_3编辑器jsp版使上传图片支持水印

主要思路:ueditor编辑器上传图片以request请求发送到后台,后台接收通过流的形式进行处理,我们只要在后台拦截到图片文件并进行加水印处理就能够实现该功能。

一、 下载ueditor1_4_3编辑器jsp版,使其能够正常工作;

二、 修改源码

  主要修改StorageManager.java文件

      image

  1) 添加将上传文件和水印文件合成带水印图片的代码

 /**
     * 将上传文件和水印文件合成带水印图片
     */
    public static void setWaterMark(File targetFile, String rootPath, String path) throws IOException {
        //源文件
        Image src = ImageIO.read(targetFile);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        g.drawImage(src, 0, 0, width, height, null);

        // 水印文件
        String FILENAME = rootPath + “ueditor/image/waterMark.png”;
        
        //FILENAME为url地址时,如:http://www.baidu.com/abc.png
//        URL url = new URL(FILENAME);
//        InputStream pressIs = url.openStream();
        
        //FILENAME为本地路径时,如:D:/abc.png
        InputStream pressIs = new FileInputStream(FILENAME);
        Image src_biao = ImageIO.read(pressIs);
        int width_biao = src_biao.getWidth(null);
        int height_biao = src_biao.getHeight(null);
        g.drawImage(src_biao, width - width_biao, height - height_biao, width_biao, height_biao, null);

        g.dispose();
        FileOutputStream out = new FileOutputStream(path);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);
        out.close();
    }

  2) 修改saveTmpFile方法

private static State saveTmpFile(File tmpFile, String rootPath, String path, Long maxSize) {
    State state = null;
    File targetFile = new File(path);

    if (targetFile.canWrite()) {
        return new BaseState(false, AppInfo.PERMISSION_DENIED);
    }
    try {
        FileUtils.moveFile(tmpFile, targetFile);
    } catch (IOException e) {
        return new BaseState(false, AppInfo.IO_ERROR);
    }

    //判断是否为图片文件
    if (maxSize == 2048000) {
        try {
            //加水印
            setWaterMark(targetFile, rootPath, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    state = new BaseState(true);
    state.putInfo("size", targetFile.length());
    state.putInfo("title", targetFile.getName());

    return state;
}

三、 重启上传图片后直接带水印。

转载于:https://www.cnblogs.com/sjshare/p/4964389.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值