`

给图片加水印

    博客分类:
  • Java
SUN 
阅读更多

// 将 s 进行 BASE64 编码
 public static String getBASE64(String s) {
  if (s == null)
   return null;
  return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
 }

 // 将 BASE64 编码的字符串 s 进行解码
 public static String getFromBASE64(String s) {
  if (s == null)
   return null;
  BASE64Decoder decoder = new BASE64Decoder();
  try {
   byte[] b = decoder.decodeBuffer(s);
   return new String(b);
  } catch (Exception e) {
   return null;
  }
 }

 
 
 public static int getLength(String text) {      
        int length = 0;      
        for (int i = 0; i < text.length(); i++) {      
            if (new String(text.charAt(i) + "").getBytes().length > 1) {      
                length += 2;      
            } else {      
                length += 1;      
            }      
        }      
        return length / 2;      
    }      

 //参数说明  水印图片 目标图片 位置 透明度
  public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {      
         try {      
             File img = new File(targetImg);  
             System.out.println(img.length());
             Image src = ImageIO.read(img);      
             int wideth = src.getWidth(null);      
             int height = src.getHeight(null);   
             System.out.println(wideth+"---"+height);
             BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);      
             Graphics2D g = image.createGraphics();      
             g.drawImage(src, 0, 0, wideth, height, null);      
             // 水印文件      
             Image src_biao = ImageIO.read(new File(pressImg));      
             int wideth_biao = src_biao.getWidth(null);      
             int height_biao = src_biao.getHeight(null); 
             System.out.println(wideth_biao+"###"+height_biao);
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));      
             g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);      
             // 水印文件结束      
             g.dispose();      
             ImageIO.write((BufferedImage) image, "jpg", img);      
         } catch (Exception e) {      
             e.printStackTrace();      
         }      
     }      
 
  public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {  
         try {  
             File img = new File(targetImg);  
             Image src = ImageIO.read(img);  
             int width = src.getWidth(null);  
             int height = src.getHeight(null);  
             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
             Graphics2D g = image.createGraphics();  
             g.drawImage(src, 0, 0, width, height, null);  
             g.setColor(color);  
             g.setFont(new Font(fontName, fontStyle, fontSize));  
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));  
             g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);  
             g.dispose();  
             ImageIO.write((BufferedImage) image, "jpg", img);  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
     }  

 

public static void main(String[] args) throws IOException {
  pressImage("D://My Documents//My Pictures//v.jpg", "D://My Documents//My Pictures//6.jpg", 0, 0, 0.5f);
  //pressText("我是文字水印", "D://My Documents//My Pictures//vladstudio_learningtofly_1152x864.jpg", "黑体", 36, Color.red, 80, 0, 0, 0.1f);            

 }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics