`
flashdream8
  • 浏览: 667127 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 图片涂抹功能

阅读更多

 

今天遇到一个问题,用户上传图片的时候,有些敏感信息 需要在图片上进行编辑一下(将敏感信息进行涂抹)

 

上传图片 在图片上记录用户涂抹的 坐标 (相对应左上角) 绘制 矩形框框

 

  
        String waterString = "_water";
        /**
         * 本功能实现了 在用户上传图片的时候,将敏感信息进行涂抹,并且在上传到图片服务器上 
         * @param objName  图片名称
         * @param url  图片 url 
         * @param waters  水印坐标   x,y,w,h|x,y,w,h (w 代表矩形框框的宽度  h 代表高度)
         * @return
         * @throws Exception
         */
    @RequestMapping(value = "/uploadimg")
    @ResponseBody
    public String upload2waterImg(String objName,String url , String waters) throws Exception {
        String picUrl = "error";
        InputStream iStream =  null; 
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream bis = null;
        try {
            if(!StringUtils.isEmpty(url)&&!StringUtils.isEmpty(waters)){
                URL imgUrl = new URL(url);
                iStream = imgUrl.openStream();
                if(iStream!=null){
                    Image image = ImageIO.read(iStream);
                    int width_img = image.getWidth(null);
                    int height_img = image.getHeight(null);
                    BufferedImage bufferedImage = new BufferedImage(width_img, height_img, BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = bufferedImage.createGraphics();
                    g.drawImage(image, 0, 0, width_img, height_img, null);
                    String[] water = StringUtils.split(waters, "\\|");
                    if(water!=null && water.length>0){
                        for(int i=0;i<water.length;i++){
                            String[] xy = StringUtils.split(water[i],",");
                            if(xy!=null && xy.length==4){
                                int x = Integer.parseInt(xy[0]);
                                int y = Integer.parseInt(xy[1]);
                                int w = Integer.parseInt(xy[2]);
                                int h = Integer.parseInt(xy[3]);
                                Rectangle2D.Float r1= new Rectangle2D.Float(x,y,w,h);//定义直角矩形
                                g.setColor(new Color(0x999999));   // 涂抹颜色
                                g.fill(r1);  //以填充方式绘制直角矩形
                            }
                        }
                        g.dispose(); 
                    }
                    picUrl = objName+waterString;
                    bos = new ByteArrayOutputStream();
                    ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, bos);
                    bis = new ByteArrayInputStream(bos.toByteArray());
                    s3ServiceUtil.saveObject(picUrl, bis);
                }
            }
        } catch (Exception e) {
            picUrl = "error";
          
            log.error("/upload upResult = "+false+"/xxx",e);
        } finally {
            try {
                if (iStream != null) {
                    iStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null != bos) {
                bos.close();
            }
            if (null != bis) {
                bis.close();
            }

            
        }
        return picUrl;
    }
    

 

  • 大小: 43.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics