java 图片操作_java对图片进行操作,仅仅是小demo

packagecom.cy.thumb;importjava.awt.Rectangle;importjava.awt.image.BufferedImage;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Iterator;importjavax.imageio.ImageIO;importjavax.imageio.ImageReadParam;importjavax.imageio.ImageReader;importjavax.imageio.stream.ImageInputStream;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;public classThumb {public static void main(String[] args) throwsIOException {//把图片image.png 长宽等比缩小5倍。

reduceImage("E:/image.png", "E:/image1.png", 5);//把图片image.png 长宽各设置为100

reduceImage("E:/image.png", "E:/image2.png", 100, 100);

}/*** 对图片进行剪裁 返回字节数组

*@paramis 图片输入流

*@paramwidth 裁剪图片的宽

*@paramheight 裁剪图片的高

*@paramimageFormat 输出图片的格式 "jpeg jpg等"

*@return

*/

public static byte[] clipImage(InputStream is,int width, intheight, String imageFormat){

ByteArrayOutputStream bos= newByteArrayOutputStream();try{//构造Image对象

BufferedImage src =javax.imageio.ImageIO.read(is);//缩小边长

BufferedImage tag = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//绘制 缩小 后的图片

tag.getGraphics().drawImage(src, 0, 0, width, height, null);

ImageIO.write(tag, imageFormat, bos);

}catch(IOException e) {

e.printStackTrace();

}returnbos.toByteArray();

}/*** 重置图片大小

*@paramsrcImagePath 读取图片路径

*@paramtoImagePath 写入图片路径

*@paramwidth 重新设置图片的宽

*@paramheight 重新设置图片的高

*@throwsIOException*/

public static void reduceImage(String srcImagePath,String toImagePath,int width, int height) throwsIOException{

FileOutputStream out= null;try{//读入文件

File file = newFile(srcImagePath);//构造Image对象

BufferedImage src =javax.imageio.ImageIO.read(file);//缩小边长

BufferedImage tag = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//绘制 缩小 后的图片

tag.getGraphics().drawImage(src, 0, 0, width, height, null);

out= newFileOutputStream(toImagePath);

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

}catch(Exception e){

e.printStackTrace();

}finally{if(out != null){

out.close();

}

}

}/*** 按倍率缩小图片

*@paramsrcImagePath 读取图片路径

*@paramtoImagePath 写入图片路径

*@paramratio 缩小比率 宽、高一起等比率缩小

*@throwsIOException*/

public static void reduceImage(String srcImagePath,String toImagePath,int ratio) throwsIOException{

FileOutputStream out= null;try{//读入文件

File file = newFile(srcImagePath);//构造Image对象

BufferedImage src =javax.imageio.ImageIO.read(file);int width =src.getWidth();int height =src.getHeight();//缩小边长

BufferedImage tag = new BufferedImage(width / ratio, height /ratio, BufferedImage.TYPE_INT_RGB);//绘制 缩小 后的图片

tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);

out= newFileOutputStream(toImagePath);

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

}catch(Exception e){

e.printStackTrace();

}finally{if(out != null){

out.close();

}

}

}/*** 对图片裁剪,并把裁剪新图片保存

*@paramsrcPath 读取源图片路径

*@paramtoPath 写入图片路径

*@paramx 剪切起始点x坐标

*@paramy 剪切起始点y坐标

*@paramwidth 剪切宽度

*@paramheight 剪切高度

*@paramreadImageFormat 读取图片格式

*@paramwriteImageFormat 写入图片格式*/

public static void cropImage(String srcPath, String toPath, int x,int y,int width,intheight, String readImageFormat,String writeImageFormat){

FileInputStream fis= null;

ImageInputStream iis=null;try{//读取图片文件

fis = newFileInputStream(srcPath);

Iterator readers =ImageIO.getImageReadersByFormatName(readImageFormat);

ImageReader reader=readers.next();//获取图片流

iis =ImageIO.createImageInputStream(fis);

reader.setInput(iis,true);

ImageReadParam param=reader.getDefaultReadParam();//定义一个矩形

Rectangle rect = newRectangle(x, y, width, height);//提供一个 BufferedImage,将其用作解码像素数据的目标。

param.setSourceRegion(rect);

BufferedImage bi= reader.read(0, param);//保存新图片

ImageIO.write(bi, writeImageFormat, newFile(toPath));

}catch(Exception e){

e.printStackTrace();

}finally{try{if(fis!=null){

fis.close();

}if(iis!=null){

iis.close();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值