Java中给图片添加文字水印,字体大小问题

Java中给图片添加文字水印,字体大小问题

问题描述:

在给图片添加文字水印的时候怎样根据图片得知在setFont(new Font(typeface,style,fontSize));fontSize该设置多少来根据图片自适应。fontSize应该怎样计算出最佳值。

网上找一个计算的公式吧。
如果图片size种类少,可以自己做一个列表,哪个size对应哪个字体

package ouc.sei.test.servlet;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import org.apache.log4j.Logger;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtils {
public static Logger logger = Logger.getLogger(ImageUtils.class);

public   static   void  main(String[] args)  {
 File sourceimage=new File("c:/image/") ;
 File flist[]=sourceimage.listFiles();
 for(int i=0;i<flist.length;i++){
  System.out.println("flist[i] is "+flist[i].getName());
  adjustpic("c:/image/"+flist[i].getName(),"c:/adjimage/ddddd"+i+".jpg",500,333);
  pressImage("C:/33.PNG","c:/adjimage/ddddd"+i+".jpg",20,20);
  //pressText("ybc","c:/adjimage/ddddd"+i+".jpg","宋体",800,234,12,20,20);
 }
 }
public final static void adjustpic(String originalpic,String adjustedpic,int imgwidth,int imgheight){
 String filecreate=adjustedpic.substring(0,adjustedpic.lastIndexOf("/")+1);//获取最后一个“/”的位置,并把取得它之前的文件路径
 try{
  File adjfile=new File(filecreate);
  if(!adjfile.exists()){
   adjfile.mkdir();
  }
 }catch(Exception e){
  System.out.println("新建文件夹操作出错");  
  e.printStackTrace();    
 }
 try   {
        File _file  =   new  File(originalpic);
        Image src  =  ImageIO.read(_file);
         int  wideth  =  src.getWidth( null );
         int  height  =  src.getHeight( null );
         boolean flag=true;
         if (wideth >= height)
         {
             flag = true;
             logger.info("图片长度Width大于或者等于高度Height");
             System.out.println("图片长度Width大于或者等于高度Height");
         }
         else
         {
             flag = false;
             logger.info("图片长度Width小于高度Height");
             System.out.println("图片长度Width小于高度Height");
         }
         Float f = new Float(0);
         if(flag){
          f = ((new Float(height) * imgwidth) / new Float(wideth));
             logger.info("新生成的图片的高度height是:" + f);
             System.out.println("新的页面的高度是:"+f);
             int newHeight = f.intValue() + 1;

        BufferedImage image  =   new  BufferedImage(imgwidth, newHeight,
                BufferedImage.TYPE_INT_RGB);
        Graphics g  =  image.createGraphics();
        g.drawImage(src,  0 ,  0 , imgwidth, newHeight,  null );
        g.dispose();
        FileOutputStream out  =   new  FileOutputStream(adjustedpic);
        JPEGImageEncoder encoder  =  JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);
        out.close();
         }
         else{
          f = ((new Float(wideth) * imgheight) / new Float(height));
             logger.info("新生成的图片的宽度width是:" + f);
             System.out.println("新的图片的宽度:"+f);
             int newWidth = f.intValue() + 1;
             BufferedImage image  =   new  BufferedImage(newWidth, imgheight,
                     BufferedImage.TYPE_INT_RGB);
             Graphics g  =  image.createGraphics();
             g.drawImage(src,  0 ,  0 , newWidth, imgheight,  null );
        g.dispose();
        FileOutputStream out  =   new  FileOutputStream(adjustedpic);
        JPEGImageEncoder encoder  =  JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);
        out.close();
         }
    }   catch  (Exception e)  {
        e.printStackTrace();
    }
}

public   final   static   void  pressImage(String pressImg, String targetImg,  int  x,  int  y)  {
    try   {
       File _file  =   new  File(targetImg);
       Image src  =  ImageIO.read(_file);
        int  wideth  =  src.getWidth( null );
        int  height  =  src.getHeight( null );
       BufferedImage image  =   new  BufferedImage(wideth, height,
               BufferedImage.TYPE_INT_RGB);
       Graphics g  =  image.createGraphics();
       g.drawImage(src,  0 ,  0 , wideth, height,  null );
        //  水印文件
       File _filebiao  =   new  File(pressImg);
       Image src_biao  =  ImageIO.read(_filebiao);
        int  wideth_biao  =  src_biao.getWidth( null );
        int  height_biao  =  src_biao.getHeight( null );
       g.drawImage(src_biao, wideth  -  wideth_biao  -  x, height  -  height_biao  - y, wideth_biao,
               height_biao,  null );
       g.dispose();
       FileOutputStream out  =   new  FileOutputStream(targetImg);
       JPEGImageEncoder encoder  =  JPEGCodec.createJPEGEncoder(out);
       encoder.encode(image);
       out.close();
   }   catch  (Exception e)  {
       e.printStackTrace();
   }

}

  public   static   void  pressText(String pressText, String targetImg, String fontName, int  fontStyle,  int  color,  int  fontSize,  int  x,  int  y)  {
      try   {
         File _file  =   new  File(targetImg);
         Image src  =  ImageIO.read(_file);
          int  wideth  =  src.getWidth( null );
          int  height  =  src.getHeight( null );
         BufferedImage image  =   new  BufferedImage(wideth, height,
                 BufferedImage.TYPE_INT_RGB);
         Graphics g  =  image.createGraphics();
         g.drawImage(src,  0 ,  0 , wideth, height,  null );
                     g.setColor(Color.RED);
         g.setFont( new  Font(fontName, fontStyle, fontSize));


         g.drawString(pressText, wideth  -  fontSize  -  x, height  -  fontSize / 2   -  y);
         g.dispose();
         FileOutputStream out  =   new  FileOutputStream(targetImg);
         JPEGImageEncoder encoder  =  JPEGCodec.createJPEGEncoder(out);
         encoder.encode(image);
         out.close();
     }   catch  (Exception e)  {
         System.out.println(e);
     }
 }

}

thumbnailator

Thumbnailator是一个用来生成图像缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。