上传oss 给图片添加水印

废话不说 直接上代码 亲测有效

业务层代码

 public String uploadImage(MultipartFile file,String sn){
        if(file==null|| StrUtil.isEmpty(sn)){
            return ApiResult.error("传入参数不能为空");
        }
            try (InputStream inputStream=file.getInputStream();
                 ByteArrayOutputStream os = new ByteArrayOutputStream();){

                //获取图片名称
               String fileName= file.getOriginalFilename();
                Image srcImg=ImageIO.read(inputStream);
                LocalDateTime now = LocalDateTime.now();
                // add by chenxin  start   给图片加上水印
                Font font = new Font("微软雅黑", Font.PLAIN, 100);                     //水印字体
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String waterMarkContent = sdf.format(new Date());  //水印内容
                Color color = new Color(255, 255, 255, 255);
                if(srcImg!=null){
                    int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
                    int srcImgHeight = srcImg.getHeight(null);//获取图片的高
                    // 加水印
                    BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = bufImg.createGraphics();
                    g.drawImage(srcImg, 1, 2, srcImgWidth, srcImgHeight, null);

                    g.setColor(color); //根据图片的背景设置水印颜色
                    g.setFont(font);              //设置字体
                    g.drawString(waterMarkContent, 100, 100);  //画出水印
                    g.dispose();
                    System.out.println("文件名称为:"+file.getOriginalFilename());
                    ImageIO.write(bufImg, "jpg", os);
                    bufImg.flush();
                    try (InputStream input=new ByteArrayInputStream(os.toByteArray())){

              //          String url = AliOssUtils.uploadFileToOssFileName(file,"device");
                        String url = AliOssUtils.uploadFileToOss(input, "device/"+sn, 1, fileName);
                    
                        return  url;
                    }
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//文件转化为图片
           catch (Exception e){
            log.info("上传图片失败{}",e.getMessage());
            return ApiResult.error("上传失败");
        }
            return null;

    }

ali Oss 工具类。。

public static String uploadFileToOss(InputStream inputStream,String upName,Integer water,String file) {
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(OssConstant.endpoint, OssConstant.ALIYUN_SMS_ACCESS_KEY, OssConstant.ALIYUN_SMS_SECRET_KEY);
        try {
            String fileName = upName+"/"+file;
            ossClient.putObject(OssConstant.bucketName, OssConstant.AlIYUN_SMS_File_NAME + fileName, inputStream);
            String url = OssConstant.accesspoint + OssConstant.AlIYUN_SMS_File_NAME + fileName;
            return  url;

        } catch (OSSException oe) {
            log.info("Caught an OSSException, which means your request made it to OSS, \"\n" +
                    "                    + \"but was rejected with an error response for some reason.");
            log.info("Error Message:{},ErrorCode:{},Request:{},Host ID:{}",
                    oe.getErrorMessage(), oe.getErrorCode(), oe.getRequestId(), oe.getHostId());
            log.error(oe.getErrorCode() + oe.getErrorMessage());
            return  "error";
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
            log.error(ce.getErrorCode() + ce.getErrorMessage());
            return "error";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            AliOssUtils.shutdown(ossClient);
        }
        return  "error";
    }

你可能感兴趣的:(java)