zoukankan      html  css  js  c++  java
  • 修改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;
    }

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

  • 相关阅读:
    Java 实例
    为什么很多程序员工作时都戴耳机?
    HTTP状态码大全
    Eclipse怎么切换工作空间
    maven POM.xml内的标签大全详解
    利用html5的FormData对象实现多图上传
    后台定时器注解方式
    js多定时器
    解决ios上微信无法捕获返回键按钮事件的问题
    上传文件,获取表单数据和文件流
  • 原文地址:https://www.cnblogs.com/sjshare/p/4964389.html
Copyright © 2011-2022 走看看