java 上传图片 生成缩略图_上传图片同时生成缩略图

packagecn.conris.sys.utils;import java.io.*;import java.awt.*;import java.awt.image.*;importjava.awt.Graphics;importjava.awt.color.ColorSpace;importjavax.imageio.ImageIO;public classChangeImageSize {/** */

/*** 缩放图像

*

*@paramsrcImageFile

* 源图像文件地址

*@paramresult

* 缩放后的图像地址

*@paramscale

* 缩放比例

*@paramflag

* 缩放选择:true 放大; false 缩小;*/

public static void scale(String srcImageFile, String result, booleanflag) {try{

BufferedImage src= ImageIO.read(new File(srcImageFile)); //读入文件

int width = src.getWidth(); //得到源图宽

int height = src.getHeight(); //得到源图长

double scale = 1;if (width >height) {

scale= height / 64;

}else{

scale= width / 64;

}if(flag) {//放大

width = (int) (width *scale);

height= (int) (height *scale);

}else{//缩小

width = (int) (width /scale);

height= (int) (height /scale);

}

Image image=src.getScaledInstance(width, height,

Image.SCALE_DEFAULT);

BufferedImage tag= newBufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

Graphics g=tag.getGraphics();

g.drawImage(image,0, 0, null); //绘制缩小后的图

g.dispose();

ImageIO.write(tag,"JPEG", new File(result));//输出到文件流

BufferedImage src2 = ImageIO.read(new File(result)); //读入文件

int x = 0;int y = 0;if (src2.getWidth() >src2.getHeight()) {

x= (src2.getWidth() - 64) / 2; //得到源图宽

y = 0; //得到源图长

} else{

y= (src2.getHeight() - 64) / 2; //得到源图宽

x = 0; //得到源图长

}

convert(result, result);

OperateImage opimg= new OperateImage(result, result, x, y, 64, 64);

opimg.cut();

}catch(IOException e) {

e.printStackTrace();

}

}/** */

/*** 图像切割

*

*@paramsrcImageFile

* 源图像地址

*@paramdescDir

* 切片目标文件夹

*@paramdestWidth

* 目标切片宽度

*@paramdestHeight

* 目标切片高度*/

public static void cut(String srcImageFile, String descDir, intdestWidth,intdestHeight) {try{

Image img;

ImageFilter cropFilter;//读取源图像

BufferedImage bi = ImageIO.read(newFile(srcImageFile));int srcWidth = bi.getHeight(); //源图宽度

int srcHeight = bi.getWidth(); //源图高度

if (srcWidth > destWidth && srcHeight >destHeight) {

Image image=bi.getScaledInstance(srcWidth, srcHeight,

Image.SCALE_DEFAULT);

destWidth= 200; //切片宽度

destHeight = 150; //切片高度

int cols = 0; //切片横向数量

int rows = 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 (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {//四个参数分别为图像起点坐标和宽高//即: CropImageFilter(int x,int y,int width,int height)

cropFilter = new CropImageFilter(j * 200, i * 150,

destWidth, destHeight);

img=Toolkit.getDefaultToolkit().createImage(newFilteredImageSource(image.getSource(),

cropFilter));

BufferedImage tag= newBufferedImage(destWidth,

destHeight, BufferedImage.TYPE_INT_RGB);

Graphics g=tag.getGraphics();

g.drawImage(img,0, 0, null); //绘制缩小后的图

g.dispose();//输出为文件

ImageIO.write(tag, "JPEG", newFile(descDir+ "pre_map_" + i + "_" + j + ".jpg"));

}

}

}

}catch(Exception e) {

e.printStackTrace();

}

}/** */

/*** 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)*/

public static voidconvert(String source, String result) {try{

File f= newFile(source);

f.canRead();

f.canWrite();

BufferedImage src=ImageIO.read(f);

ImageIO.write(src,"JPG", newFile(result));

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/** */

/*** 彩色转为黑白

*

*@paramsource

*@paramresult*/

public static voidgray(String source, String result) {try{

BufferedImage src= ImageIO.read(newFile(source));

ColorSpace cs=ColorSpace.getInstance(ColorSpace.CS_GRAY);

ColorConvertOp op= new ColorConvertOp(cs, null);

src= op.filter(src, null);

ImageIO.write(src,"JPEG", newFile(result));

}catch(IOException e) {

e.printStackTrace();

}

}/** */

/***@paramargs*/

public static voidmain(String[] args) {

scale("d://2.jpg", "d://3.jpg", false);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值