利用java给网站图片打上文本或图片logo

这是个本地化应用,稍加修改便可做成个Action,上传图片时调用一下就能给你网站的图片打上文本或图像logo。代码如下



 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
import java.io.File;
 import java.io.FileOutputStream;
import javax.imageio.ImageIO;
 import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.io.IOException;
import java.io.FileNotFoundException;

public class PhotoWatermark{

   public static void main(String[] args) throws Exception{
        PhotoWatermark pw=new PhotoWatermark();
        //添加logo  原图片路径  logo路径 生成合成图片路径 x,y
        pw.addImageLogo("d:/1.png","d:/b.jpg","d:/x.jpg",20,20);
        
        //源文件路径 水印文字信息 生成后的路径
       pw.addTextLogo("d:/1.png","www.baidu.com","d:/logo.png",
            new Font("Arial Black",Font.PLAIN,48),20,40);        
   }
    
    /**
    *@param photopath : 原图存放的路径
     *@param logopath : logo图像存放的路径
     *@param savepath : 目标输出保存的路径
     *@param x : logo图像在合并到原图中的显示位置x座标
     *@param y : logo图像在合并到原图中的显示位置y座标
     */
    public void addImageLogo(String photopath,String logopath,String savepath,int x,int y) 
        throws IOException,FileNotFoundException{
        
        Image image=ImageIO.read(new File(photopath));
        int pwidth=image.getWidth(null);
       int pheight=image.getHeight(null);
        
       BufferedImage buffimage=new BufferedImage(pwidth,pheight,BufferedImage.TYPE_INT_BGR);
      Graphics g=buffimage.createGraphics();
       g.drawImage(image,0,0,pwidth,pheight,null);
       
       Image logo=ImageIO.read(new File(logopath));        
        int lwidth=logo.getWidth(null);
        int lheight=logo.getHeight(null);
       g.drawImage(logo,x,y,lwidth,lheight,null);
       
       g.dispose();
        FileOutputStream os=new FileOutputStream(savepath);
        JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(os);
       encoder.encode(buffimage);        
        os.close();    
    }
    
    /**
53     *@param photopath : 原图存放的路径
54     *@param logopath : 文本logo内容
55     *@param savepath : 目标输出保存的路径
56     *@param font : 文本logo的字体设置
57     *@param x : 文本logo在合并到原图中的显示位置x座标
58     *@param y : 文本logo在合并到原图中的显示位置y座标
59     */
    public void addTextLogo(String photopath,String logotext,String savepath,
        java.awt.Font font,int x,int y) throws Exception{
            
        Image image=ImageIO.read(new File(photopath));
       int pwidth=image.getWidth(null);
        int pheight=image.getHeight(null);
        
       BufferedImage buffimage=new BufferedImage(pwidth,pheight,BufferedImage.TYPE_INT_BGR);
       Graphics g=buffimage.createGraphics();
        g.drawImage(image,0,0,pwidth,pheight,null);
        
        g.setFont(font);
        g.drawString(logotext,x,y);
                
        g.dispose();
       FileOutputStream os=new FileOutputStream(savepath);
        JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(os);
        encoder.encode(buffimage);        
        os.close();    
    }
}

如果  jpeg导入失败 请看下我写的博客解决方案  java目录

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值