这是我前两天写的生成图片的缩略图和给图片打上logo的java代码没有怎么整理就放上来了希望能够为大家在工作中帮点忙同时写的有什么不对的地方希望大家指点指点
import javaawtGraphicsD;
import javaawtgeomAffineTransform;
import javaawtimageAffineTransformOp;
import javaawtimageBufferedImage;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaawtColor;
//生成缩略图和给图上打上公司logo
public static void main(String[] args) throws IOException {
//源文件必须是有文件
String pathS=D:/tdeng/My Pictures/showPhotogif;
//目标文件
String pathD=D:/temp/showPhotominigif;
//拷贝文件
copyfile(pathSpathD);
//
//生成缩小mimi图
File stadimgfile = new File(pathD);
//图片缓存
BufferedImage img = ImageIOread(stadimgfile);
//得到图片的宽和高
double width = imggetWidth();
double height = imggetHeight();
int miniwidth = ;//缩略图宽度
int miniheight = ;//缩略图高度
double ratew = miniwidth / width;
double rateh = miniheight / height;
//获得适合的缩放比率即以在规定缩略尺寸中完整显示图片内容的同时又保证最大的缩放比率
double rate = Mathmin(ratew rateh);
rate = (Mathrint((rate * ) + )) / ;
BufferedImage imgmini = new javaawtimageBufferedImage(miniwidth miniheightBufferedImageTYPE_USHORT__RGB);
GraphicsD gmini = imgminicreateGraphics();
gminisetBackground(ColorWHITE);
gminiclearRect( miniwidth miniheight);
AffineTransform trans = new AffineTransform();
transscale(rate rate);
AffineTransformOp op = new AffineTransformOp(trans AffineTransformOpTYPE_BILINEAR);
gminidrawImage(img op (int) (miniwidth (width * rate)) / (int) (miniheight (height * rate)) / );
ImageIOwrite(imgmini jpg stadimgfile);
//
//mimi图加水印
BufferedImage img = ImageIOread(stadimgfile);
int mimi_width = imggetWidth();
int mimi_height = imggetHeight();
BufferedImage imgmimi = new javaawtimageBufferedImage(mimi_width mimi_height
BufferedImageTYPE_USHORT__RGB);
//logo文件的位置必须是真是的
String logoFile = D:/tdeng/My Pictures/logogif;
BufferedImage watermark = ImageIOread(new File(logoFile));
GraphicsD gmimi = imgmimicreateGraphics();
gmimidrawImage(img null );
gmimidrawImage(watermark null mimi_width watermarkgetWidth() mimi_height watermarkgetHeight());
ImageIOwrite(imgmimi jpg stadimgfile);
}
// 使用FileInputStream和FileOuStream
public static void copyfile(String pathSrcString pathDest) throws IOException
{
FileInputStream fi=new FileInputStream(pathSrc);
FileOutputStream fo=new FileOutputStream(pathDest);
byte data[]=new byte[fiavailable()];
firead(data);
fowrite(data);
ficlose();
foclose();
}