zoukankan      html  css  js  c++  java
  • java给图片加上水印

    有时候我们需要给照片上传的时候,加一些水印。

    public String watermarkMulText(InputStream is, String filename, String realUploadPath) {
            String logoFileName = System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            OutputStream os = null;
    
            try {
                Image image2 = ImageIO.read(is);
                int width = image2.getWidth(null);
                int height = image2.getHeight(null);
    
    //            1、创建缓存图片对象
                BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    
    //            2、        创建JAVA绘图工具对象
                Graphics2D graphics2D = bufferedImage.createGraphics();
    
    //            3、使用绘图工具对象,将原图绘制到缓存图片对象
                graphics2D.drawImage(image2,0,0,width,height,null);
    
    //            4、使用绘图工具,将水印(文字/图片)绘制到缓存图片对象
                graphics2D.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
                graphics2D.setColor(FONT_COLOR);
    
                //S1-获取文字水印的宽、高
                int width1 = FONT_SIZE*getTextLength(MARK_TEXT);
                int height1 = FONT_SIZE;
    
    
                graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));//设置水印图层
    
                graphics2D.rotate(Math.toRadians(45),bufferedImage.getHeight()/2,bufferedImage.getHeight()/2);//图层以中心为基点,转30度的弧度。
    
                int x = -width/2;
                int y = -height/2;
    
                while (x<width*1.5){
                    y = -height/2;
                    while (y<height*1.5){
                        graphics2D.drawString(MARK_TEXT,x,y);
                        y+=height1+MARGIN_ALL;
                    }
                    x+=width1+MARGIN_ALL;
                }
    
    
                graphics2D.dispose();
    
    //            5、        创建图像编码工具类
                String des = realUploadPath+File.separator+ logoFileName;
                os = new FileOutputStream(des);
    
                JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(os);
    
    //            6、使用图片编码工具类,输出缓存图像到模板图片文件
                jpegImageEncoder.encode(bufferedImage);
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(os!=null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    
            return realUploadPath+File.separator+logoFileName;
        }

    以上修改于网上的一段代码,忘记保留出处了。

    效果如下:

    String --> InputStream

    ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
    InputStream --> String
    String inputStream2String(InputStream is){
    	BufferedReader in = new BufferedReader(new InputStreamReader(is));
       StringBuffer buffer = new StringBuffer();
       String line = "";
       while ((line = in.readLine()) != null){
         buffer.append(line);
       }
       return buffer.toString();
    }
    

    File --> InputStream

    InputStream in = new FileInputStream(file);
    

    InputStream --> File

    public void inputstreamtofile(InputStream ins,File file){
       OutputStream os = new FileOutputStream(file);
       int bytesRead = 0;
       byte[] buffer = new byte[8192];
       while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
          os.write(buffer, 0, bytesRead);
       }
       os.close();
       ins.close();

    BufferedImage --> inputStream
    BufferedImage --> String

    public static InputStream encode(String content, String logoPath, boolean needCompress) throws Exception {
            BufferedImage image = QrCodeUtil.createImage(content, logoPath, true);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, FORMAT, outputStream);
            //String streamEncoded = Base64Util.encode(outputStream.toByteArray());
            InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            return inputStream;
      }

    转:https://blog.csdn.net/weixin_39248420/article/details/89949880

    道法自然
  • 相关阅读:
    jquery的图片异步加载
    thinkphp3.1的验证码
    android的edittext设置输入限制,只能输入数字
    android,安卓get请求的提交以及我遇到的异常
    android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传
    php正则表达式函数
    php对浮点数小数取整,php除法取整数
    php数组全排列,元素所有组合
    javascript数组全排列,数组元素所有组合
    spring 配置中相关属性的含义:
  • 原文地址:https://www.cnblogs.com/jiduoduo/p/14813508.html
Copyright © 2011-2022 走看看