第三十节(java-实现水印图片)

package com.tanzhou.imageDemo;

import java.awt.Color;
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 java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ImageDemo {
      /**
       * 把图片印刷到目标图片上
       * @param pressImg   水印文件
       * @param targetImg  目标文件
       * @param x      横坐标
       * @param y      纵坐标
       */
    public final static void pressImage(String pressImg,String targetImg,int x,int y){
        //目标文件
        try {
            File _file = new File(targetImg); // 获取图片
            Image src = ImageIO.read(_file); // file 文件对象转换成image对象
            int width = src.getWidth(null);
            int height = src.getHeight(null);
            System.out.println(width+"========="+height);
            
            BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics(); // Graphics 是java中处理图片的类
            g.drawImage(src,0,0,width,height,null);
            
            
            // 水印文件
            File _filebiao = new File(pressImg);
            Image src_biao = ImageIO.read(_filebiao);
            int width_biao = src_biao.getWidth(null); // 图片宽度
            int height_biao = src_biao.getHeight(null); // 图片高度
            g.drawImage(src_biao,(width-width_biao)/2,(height-height_biao)/2,width_biao,height_biao,null);
        
            g.dispose(); // 结束文件对象的处理
            
            FileOutputStream out = new FileOutputStream(targetImg);
            /**
             * JPEGCodec 的作用是创建一个和指定输出流关联的JPEGImageEncoder
             */
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
    /**
     * 
     * @param pressText   文字
     * @param targetImg   目标图片
     * @param fontName    字体名
     * @param fontStyle  字体样式
     * @param color    颜色  0-255  
     * @param fontSize  大小  12
     * @param x    横坐标
     * @param y    纵坐标
     */
    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 width = src.getWidth(null);
            int height = src.getHeight(null);
            
            BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(src,0,0,width,height,null);
            
            g.setColor(Color.RED);
            g.setFont(new Font(fontName,fontStyle,fontSize));
            g.drawString(pressText, width-fontSize-x, height-fontSize/2-y);
            g.dispose();
            
            FileOutputStream out = new FileOutputStream(targetImg);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
    }
    
    public static void main(String[] args){
        pressImage("C:\\Users\\Tony Liu\\Desktop\\image\\4.jpg","C:\\Users\\Tony Liu\\Desktop\\image\\5.jpg",120,120);
        pressText("中山学院-Java软件工程师","C:\\Users\\Tony Liu\\Desktop\\image\\5.jpg","宋体",16,120,16,250,100);
    }
}

 

posted @ 2015-02-08 09:59  哥的笑百度不到  阅读(237)  评论(0编辑  收藏  举报