mportjava.io.*;
importjava.awt.*;
importjava.awt.image.*;
importjava.awt.Graphics;
importjava.awt.color.ColorSpace;
importjavax.imageio.ImageIO;

publicclassChangeImageSize
{
/**
*缩放图像
*@paramsrcImageFile源图像文件地址
*@paramresult缩放后的图像地址
*@paramscale缩放比例
*@paramflag缩放选择:true放大;false缩小;
*/
publicstaticvoidscale(StringsrcImageFile,Stringresult,intscale,booleanflag)
{
try
{
BufferedImagesrc=ImageIO.read(newFile(srcImageFile));//读入文件
intwidth=src.getWidth();//得到源图宽
intheight=src.getHeight();//得到源图长
if(flag)
{
//放大
width=width*scale;
height=height*scale;
}
else
{
//缩小
width=width/scale;
height=height/scale;
}
Imageimage=src.getScaledInstance(width,height,Image.SCALE_DEFAULT);
BufferedImagetag=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(image,0,0,null);//绘制缩小后的图
g.dispose();
ImageIO.write(tag,"JPEG",newFile(result));//输出到文件流
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
/**
*图像切割
*@paramsrcImageFile源图像地址
*@paramdescDir切片目标文件夹
*@paramdestWidth目标切片宽度
*@paramdestHeight目标切片高度
*/
publicstaticvoidcut(StringsrcImageFile,StringdescDir,intdestWidth,intdestHeight)
{
try
{
Imageimg;
ImageFiltercropFilter;
//读取源图像
BufferedImagebi=ImageIO.read(newFile(srcImageFile));
intsrcWidth=bi.getHeight();//源图宽度
intsrcHeight=bi.getWidth();//源图高度
if(srcWidth>destWidth&&srcHeight>destHeight)
{
Imageimage=bi.getScaledInstance(srcWidth,srcHeight,Image.SCALE_DEFAULT);
destWidth=200;//切片宽度
destHeight=150;//切片高度
intcols=0;//切片横向数量
introws=0;//切片纵向数量
//计算切片的横向和纵向数量
if(srcWidth%destWidth==0)
{
cols=srcWidth/destWidth;
}
else
{
cols=(int)Math.floor(srcWidth/destWidth)+1;
}
if(srcHeight%destHeight==0)
{
rows=srcHeight/destHeight;
}
else
{
rows=(int)Math.floor(srcHeight/destHeight)+1;
}
//循环建立切片
//改进的想法:是否可用多线程加快切割速度
for(inti=0;i<rows;i++)
{
for(intj=0;j<cols;j++)
{
//四个参数分别为图像起点坐标和宽高
//即:CropImageFilter(intx,inty,intwidth,intheight)
cropFilter=newCropImageFilter(j*200,i*150,destWidth,destHeight);
img=Toolkit.getDefaultToolkit(), .createImage(
newFilteredImageSource(image.getSource(),cropFilter));
BufferedImagetag=newBufferedImage(destWidth,destHeight,BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(img,0,0,null);//绘制缩小后的图
g.dispose();
//输出为文件
ImageIO.write(tag,"JPEG",newFile(descDir+"pre_map_"+i+"_"+j+".jpg"));
}
}
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
/**
*图像类型转换GIF->JPGGIF->PNGPNG->JPGPNG->GIF(X)
*/
publicstaticvoidconvert(Stringsource,Stringresult)
{
try
{
Filef=newFile(source);
f.canRead();
f.canWrite();
BufferedImagesrc=ImageIO.read(f);
ImageIO.write(src,"JPG",newFile(result));
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*彩色转为黑白
*@paramsource
*@paramresult
*/
publicstaticvoidgray(Stringsource,Stringresult)
{
try
{
BufferedImagesrc=ImageIO.read(newFile(source));
ColorSpacecs=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOpop=newColorConvertOp(cs,null);
src=op.filter(src,null);
ImageIO.write(src,"JPEG",newFile(result));
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)
{
scale("D:\100CASIO\CIMG0001.JPG","C:\DocumentsandSettings\ibm\桌面\image.jpg",10,false);
}







{
/**
*缩放图像
*@paramsrcImageFile源图像文件地址
*@paramresult缩放后的图像地址
*@paramscale缩放比例
*@paramflag缩放选择:true放大;false缩小;
*/
publicstaticvoidscale(StringsrcImageFile,Stringresult,intscale,booleanflag)
{
try
{
BufferedImagesrc=ImageIO.read(newFile(srcImageFile));//读入文件
intwidth=src.getWidth();//得到源图宽
intheight=src.getHeight();//得到源图长
if(flag)
{
//放大
width=width*scale;
height=height*scale;
}
else
{
//缩小
width=width/scale;
height=height/scale;
}
Imageimage=src.getScaledInstance(width,height,Image.SCALE_DEFAULT);
BufferedImagetag=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(image,0,0,null);//绘制缩小后的图
g.dispose();
ImageIO.write(tag,"JPEG",newFile(result));//输出到文件流
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
/**
*图像切割
*@paramsrcImageFile源图像地址
*@paramdescDir切片目标文件夹
*@paramdestWidth目标切片宽度
*@paramdestHeight目标切片高度
*/
publicstaticvoidcut(StringsrcImageFile,StringdescDir,intdestWidth,intdestHeight)
{
try
{
Imageimg;
ImageFiltercropFilter;
//读取源图像
BufferedImagebi=ImageIO.read(newFile(srcImageFile));
intsrcWidth=bi.getHeight();//源图宽度
intsrcHeight=bi.getWidth();//源图高度
if(srcWidth>destWidth&&srcHeight>destHeight)
{
Imageimage=bi.getScaledInstance(srcWidth,srcHeight,Image.SCALE_DEFAULT);
destWidth=200;//切片宽度
destHeight=150;//切片高度
intcols=0;//切片横向数量
introws=0;//切片纵向数量
//计算切片的横向和纵向数量
if(srcWidth%destWidth==0)
{
cols=srcWidth/destWidth;
}
else
{
cols=(int)Math.floor(srcWidth/destWidth)+1;
}
if(srcHeight%destHeight==0)
{
rows=srcHeight/destHeight;
}
else
{
rows=(int)Math.floor(srcHeight/destHeight)+1;
}
//循环建立切片
//改进的想法:是否可用多线程加快切割速度
for(inti=0;i<rows;i++)
{
for(intj=0;j<cols;j++)
{
//四个参数分别为图像起点坐标和宽高
//即:CropImageFilter(intx,inty,intwidth,intheight)
cropFilter=newCropImageFilter(j*200,i*150,destWidth,destHeight);
img=Toolkit.getDefaultToolkit(), .createImage(
newFilteredImageSource(image.getSource(),cropFilter));
BufferedImagetag=newBufferedImage(destWidth,destHeight,BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(img,0,0,null);//绘制缩小后的图
g.dispose();
//输出为文件
ImageIO.write(tag,"JPEG",newFile(descDir+"pre_map_"+i+"_"+j+".jpg"));
}
}
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
/**
*图像类型转换GIF->JPGGIF->PNGPNG->JPGPNG->GIF(X)
*/
publicstaticvoidconvert(Stringsource,Stringresult)
{
try
{
Filef=newFile(source);
f.canRead();
f.canWrite();
BufferedImagesrc=ImageIO.read(f);
ImageIO.write(src,"JPG",newFile(result));
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*彩色转为黑白
*@paramsource
*@paramresult
*/
publicstaticvoidgray(Stringsource,Stringresult)
{
try
{
BufferedImagesrc=ImageIO.read(newFile(source));
ColorSpacecs=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOpop=newColorConvertOp(cs,null);
src=op.filter(src,null);
ImageIO.write(src,"JPEG",newFile(result));
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)
{
scale("D:\100CASIO\CIMG0001.JPG","C:\DocumentsandSettings\ibm\桌面\image.jpg",10,false);
}
}
来至:http://www.diybl.com/course/3_program/java/javashl/20071126/87694.html