图片的水印与缩略图

//只需修改main方法里的图片路径就可以调试或运行了

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.text.AttributedString;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;

public class ImageHandle {

 public ImageHandle() {

 }

 /**
  * 给图片添加水印(只支持jpeg/jpg格式)
  *
  * @param filePath
  *            需要添加水印的图片的路径
  * @param markContent
  *            水印的文字
  * @param markContentColor
  *            水印文字的颜色
  * @param qualNum
  *            图片质量
  * @param fontType
  *            字体
  * @param fontsize
  *            字体大小
  * @return boolean
  */
 public boolean createMark(String filePath, String markContent,
   Color markContentColor, float qualNum, String fontType, int fontSize) {
  
  ImageIcon imgIcon = new ImageIcon(filePath);
  Image theImg = imgIcon.getImage();
  int width = theImg.getWidth(null);
  int height = theImg.getHeight(null);
  
  BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  Graphics2D g = bimage.createGraphics();
  g.setColor(markContentColor);
  g.setBackground(Color.white);
  g.drawImage(theImg, 0, 0, null);
  
  AttributedString ats = new AttributedString(markContent);
  Font f = new Font(fontType, Font.BOLD, fontSize);
  ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
  AttributedCharacterIterator iter = ats.getIterator();

  g.drawString(iter,0,f.getSize()); // 添加水印的文字和设置水印文字出现的位置(左上角)
  g.dispose();

  try {
   FileOutputStream out = new FileOutputStream(filePath);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
   param.setQuality(qualNum, true);
   encoder.encode(bimage, param);
   out.close();
  } catch (Exception e) {
   return false;
  }
  return true;
 }

 /**
  * 生成图片的缩略图
  *
  * @param filePath
  *            图片的路径
  * @param width
  *            希望生成的缩略图的宽度
  * @param height
  *            希望生成的缩略图的高度
  * @return Icon
  */
 public Icon createFixedBoundIcon(String filePath, int width, int height)
   throws Exception {

  File F = new File(filePath);
  double Ratio = 0.0;  // 缩放比例

  if (!F.isFile())
   throw new Exception(F
     + "  is not image file error in getFixedBoundIcon! ");
  
  Icon icon = new ImageIcon(filePath);
  BufferedImage Bi = ImageIO.read(F);
  
  if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
   if (Bi.getHeight() > Bi.getWidth()) {
    Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
   } else {
    Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
   }
   int lastLength = filePath.lastIndexOf(".");
   String subFilePath = filePath.substring(0, lastLength);

   String fileName=this.GetFileName(subFilePath);//图片的文件名
   String fileTypes=filePath.substring(lastLength+1); //图片的类型
   File zoomFile = new File(subFilePath+"_" +fileName+"."+fileTypes);//图片的完整路径
   Image Itemp = Bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
   AffineTransformOp op = new AffineTransformOp(AffineTransform
     .getScaleInstance(Ratio, Ratio), null);
   Itemp = op.filter(Bi, null);
   
   try {
    ImageIO.write((BufferedImage) Itemp,"jpg", zoomFile);
    icon = new ImageIcon(zoomFile.getPath());  //设定输出路径
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
  return icon;
 }
 
 /**
  * 获取文件路径中的文件名
  *
  * @param filePath
  *            文件的路径
  * @return String
  */
 public String GetFileName(String filepath) {
  
  String returnstr = "*.*";
  int length = filepath.trim().length();

  filepath = filepath.replace('//', '/');
  if (length > 0) {
   int i = filepath.lastIndexOf("/");
   if (i >= 0) {
    filepath = filepath.substring(i + 1);
    returnstr = filepath;
   }
  }
  return returnstr;
 }
 
 
 public   static   void  main(String[] args)
    {
  ImageHandle wm = new ImageHandle();
        try{
         wm.createMark("G://q1.jpg","注册商标",Color.BLUE,5f,"宋体",12);
         System.out.println("为图片添加了水印!");
         wm.createFixedBoundIcon("G://q3.gif",128,128);
         System.out.println("为图片生成了缩略图!");
        }catch(Exception e){
         e.printStackTrace();
        }
   }
 
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值