• 修改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;
    }

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

  • 相关阅读:
    python爬取哦漫画
    NLP系列(2)_用朴素贝叶斯进行文本分类(上)
    svm 笔记
    nlp学习笔记
    LR
    bp网络全解读
    最小二乘法
    学习参考
    pandas中的quantile函数
    泰坦尼克号与小费与乘客数量与 鸢尾花数据集
  • 原文地址:https://www.cnblogs.com/sjshare/p/4964389.html
Copyright © 2020-2023  润新知