源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java实现的图片裁剪功能示例

  • 时间:2020-05-17 13:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java实现的图片裁剪功能示例
本文实例讲述了java实现的图片裁剪功能。分享给大家供大家参考,具体如下: PicCut.java:
package Tsets;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class PicCut {
  public void cut(int x,int y,int width,int height,String srcpath,String subpath) throws IOException {//裁剪方法
    FileInputStream is = null;
    ImageInputStream iis = null;
    try {
      is = new FileInputStream(srcpath); //读取原始图片
      Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); //ImageReader声称能够解码指定格式
      ImageReader reader = it.next();
      iis = ImageIO.createImageInputStream(is); //获取图片流
      reader.setInput(iis, true); //将iis标记为true(只向前搜索)意味着包含在输入源中的图像将只按顺序读取
      ImageReadParam param = reader.getDefaultReadParam(); //指定如何在输入时从 Java Image I/O框架的上下文中的流转换一幅图像或一组图像
      Rectangle rect = new Rectangle(x, y, width, height); //定义空间中的一个区域
      param.setSourceRegion(rect); //提供一个 BufferedImage,将其用作解码像素数据的目标。
      BufferedImage bi = reader.read(0, param); //读取索引imageIndex指定的对象
      ImageIO.write(bi, "jpg", new File(subpath)); //保存新图片
    } finally {
      if (is != null)
        is.close();
      if (iis != null)
        iis.close();
    }
  }
  public static void main(String[] args) throws Exception {
      PicCut pc = new PicCut();
    pc.cut(20, 20, 100, 100,"D:\\1.jpg","D:\\11.jpg");
    System.out.println("ok");
  }
}

[b]PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:[/b] [b]在线图片转换BASE64工具: [/b][url=http://tools.jb51.net/transcoding/img2base64]http://tools.jb51.net/transcoding/img2base64[/url] [b]ICO图标在线生成工具: [/b][url=http://tools.jb51.net/aideddesign/ico_img]http://tools.jb51.net/aideddesign/ico_img[/url] [b]在线Email邮箱图标制作工具: [/b][url=http://tools.jb51.net/email/emaillogo]http://tools.jb51.net/email/emaillogo[/url] [b]在线图片格式转换(jpg/bmp/gif/png)工具: [/b][url=http://tools.jb51.net/aideddesign/picext]http://tools.jb51.net/aideddesign/picext[/url] 更多java相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/915.htm]Java图片操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/869.htm]java日期与时间操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/830.htm]Java操作DOM节点技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/687.htm]Java文件与目录操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/632.htm]Java数据结构与算法教程[/url]》。 希望本文所述对大家java程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部