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;
    }

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

  • 相关阅读:
    关于Class.forName(“com.mysql.jdbc.Driver”)
    Vector既然继承了AbstractList为啥还要实现List接口
    推荐两个支持Java的云主机空间
    关于“Return empty arrays or collections, not nulls”的思考
    "win7回收站已损坏"解决方法
    ibatis中使用like模糊查询
    当你升级到ubuntu12.04之后
    转一篇:如何快速的修改参考文献
    Java Annotations初探
    用eclipse开发android,xmllayout文件不自动提示,Java代码可以自动提示
  • 原文地址:https://www.cnblogs.com/sjshare/p/4964389.html
Copyright © 2011-2022 走看看