java實現截屏程序

yb25 9年前發布 | 2K 次閱讀 Java

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImgTool { private int startX; private int startY; 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("農場.jpg");
    imgT.setTarPath("d:/1.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;
}

}</pre>

 本文由用戶 yb25 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!