上传图片打水印的问题

是结合在jspsmart.upload下使用的,这个是imgbean的代码

package com.pic;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import org.w3c.dom.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.*;

public class ImgBean_i{
public void ImgBean_i(){

}

public void ImgYin(String Imagename_biao,String ImgName){
try{
File _file = new File(ImgName);
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(Imagename_biao);
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-110,height-110,wideth_biao,height_biao,null);
///
 g.dispose();
FileOutputStream out=new FileOutputStream(ImgName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
}


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//WaterMark.java
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

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

public class WaterMark
{
    public void imgMark(String strOriginalFileName,String strWaterMarkFileName)
    {
        try
        {
            //源文件
            File fileOriginal = new File(strOriginalFileName);
            Image imageOriginal = ImageIO.read(fileOriginal);
            int widthOriginal = imageOriginal.getWidth(null);
            int heightOriginal = imageOriginal.getHeight(null);
            System.out.println("widthOriginal:" + widthOriginal + "/theightOriginal:" + heightOriginal);

            BufferedImage bufImage = new BufferedImage(widthOriginal,heightOriginal,BufferedImage.TYPE_INT_RGB);
            Graphics g = bufImage.createGraphics();
            g.drawImage(imageOriginal,0,0,widthOriginal,heightOriginal,null);

            //水印文件
            File fileWaterMark = new File(strWaterMarkFileName);
            Image imageWaterMark = ImageIO.read(fileWaterMark);
            int widthWaterMark = imageWaterMark.getWidth(null);
            int heightWaterMark = imageWaterMark.getHeight(null);
            System.out.println("widthWaterMark:" + widthWaterMark + "/theightWaterMark:" + heightWaterMark);

            //水印文件在源文件的右下角
            g.drawImage(imageWaterMark,widthOriginal - widthWaterMark,heightOriginal - heightWaterMark,widthWaterMark,heightWaterMark,null);
            g.dispose();

            FileOutputStream fos = new FileOutputStream(strOriginalFileName);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
            encoder.encode(bufImage);
            fos.flush();
            fos.close();
            fos = null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        WaterMark waterMark = new WaterMark();
        if(args.length == 2)
        {
            waterMark.imgMark(args[0],args[1]);
        }
        else
        {
            System.out.println("Java WaterMark fileOriginal fileWaterMark");
        }
    }
}

你可能感兴趣的:(上传图片打水印的问题)