加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java中调整图片的大小并给图片加水印(或是文字)

(2011-04-01 14:38:59)
标签:

it

分类: java

java中调整图片的大小并给图片加水印(或是文字)

前几天给图片加水印一张一张的处理,相当的麻烦,相当的累,最近有时间写了一个动态实现这种效果的java类

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);
         }
     }

 }

0

阅读 收藏 喜欢 打印举报/Report
后一篇:初识触发器
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有