java 截图代码

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;


public class ImgTool {
 private int startX;  //截图开始位置  X
     private int startY;  //截图的开始位置 Y
     private int width;  //截取多宽
     private int height;  //截取多高
  
    private String srcPath;  //源文件的位置

     private String tarPath;  //目标路径
       
     public static void main(String args[]){ 
         ImgTool imgT = new ImgTool(); 
         imgT.setStartX(100); 
         imgT.setStartY(100); 
         imgT.setHeight(200); 
         imgT.setWidth(200); 
         imgT.setSrcPath("c:/1.jpg"); 
         imgT.setTarPath("c:/2.jpg");     
         imgT.cut(); 
     } 
     /** 
      * 根据开始坐标,宽度和高度切图。 
      * 把图片读入内存缓冲区,然后再 
      * 根据具体坐标切取子图最后把子 
      * 图俺规定的格式存入指定文件 
      */
     public void cut(){ 
         try { 
             BufferedImage bufImg = ImageIO.read(new File(srcPath)); 
             bufImg = bufImg.getSubimage(startX, startY, width, height); 
             ImageIO.write(bufImg,"jpg",new File(tarPath)); 
         } catch (Exception e) { 
             e.printStackTrace(); 
         } 
     } 
       
     public int getStartX() { 
         return startX; 
     } 
     public void setStartX(int startX) { 
         this.startX = startX; 
     } 
     public int getStartY() { 
         return startY; 
     } 
     public void setStartY(int startY) { 
         this.startY = startY; 
     } 
     public int getWidth() { 
         return width; 
     } 
     public void setWidth(int width) { 
         this.width = width; 
     } 
     public int getHeight() { 
         return height; 
     } 
     public void setHeight(int height) { 
         this.height = height; 
     } 
     public String getSrcPath() { 
         return srcPath; 
     } 
     public void setSrcPath(String srcPath) { 
         this.srcPath = srcPath; 
     } 
     public String getTarPath() { 
         return tarPath; 
     } 
     public void setTarPath(String tarPath) { 
         this.tarPath = tarPath; 
     } 
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值