JAVA截取图片后保存到本地

public static void cutImage(InputStream input, OutputStream out,String type, int x, int y, int width, int height)throws IOException {

ImageInputStream imageStream = null;

try {

String imageType = (null == type || "".equals(type)) ? "jpg" : type;Iterator readers = ImageIO

.getImageReadersByFormatName(imageType);

ImageReader reader = readers.next();

imageStream = ImageIO.createImageInputStream(input);

reader.setInput(imageStream, true);

ImageReadParam param = reader.getDefaultReadParam();

Rectangle rect = new Rectangle(x, y, width, height);

param.setSourceRegion(rect);

BufferedImage bi = reader.read(0, param);

ImageIO.write(bi, imageType, out);

} finally {

imageStream.close();

input.close();

out.close();

}

}

使用示例:


//locUrl为大图路径

String[] targetRect=info.getTargetRect().split("/");

//小图保存本地

ImageUtils.cutImage(new FileInputStream(locUrl),new FileOutputStream(smallLoc),"jpg", Integer.parseInt(targetRect[0]),

Integer.parseInt(targetRect[1]), Integer.parseInt(targetRect[2]), Integer.parseInt(targetRect[3]));

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,522评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,161评论 19 139
  • 清晨起来,玻璃窗上濛濛的水汽让视野变得模糊,伸出手将它抹去。 外面的世界实在冷清,没有行人,没有车,连风都没有。 ...
    琅笑笑阅读 1,603评论 0 0
  • 阿晓加英文阅读 1,387评论 0 0
  • 只有活在未来的人才有真正的耐心,换言之,一个人的耐心有多大,只要看他活在多久之后的未来。----李笑来 这句话是出...
    金科状元阅读 1,697评论 0 2

友情链接更多精彩内容