完成主舞台布局并绑定主舞台按钮事件
public class CapterScreen extends Application {
ImageView iv;
Stage primaryStage;
Stage stage;
double start_x;
double start_y;
double w;
double h;
HBox hBox;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
AnchorPane root = new AnchorPane();
Button bu = new Button("点击截屏");
iv= new ImageView();
iv.setFitHeight(400);
iv.setPreserveRatio(true);
root.getChildren().addAll(bu,iv);
AnchorPane.setTopAnchor(bu,50.0);
AnchorPane.setLeftAnchor(bu,50.0);
AnchorPane.setTopAnchor(iv,100.0);
AnchorPane.setLeftAnchor(iv,50.0);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("截屏工具");
primaryStage.setHeight(800);
primaryStage.setWidth(800);
primaryStage.show();
bu.setOnAction(event -> {
show();
});
KeyCombination keyCombination = KeyCombination.valueOf("ctrl+alt+p");
Mnemonic mc = new Mnemonic(bu,keyCombination);
scene.addMnemonic(mc);
}
public static void main(String[] args) {
Application.launch(args);
}
}
显示截图操作舞台并绑定鼠标截图事件
public void show(){
primaryStage.setIconified(true);
stage = new Stage();
AnchorPane anchorPane = new AnchorPane();
anchorPane.setStyle("-fx-background-color: #85858522");
Scene scene = new Scene(anchorPane);
scene.setFill(Paint.valueOf("#ffffff00"));
stage.setScene(scene);
stage.setFullScreenExitHint("");
stage.initStyle(StageStyle.TRANSPARENT);
stage.setFullScreen(true);
stage.show();
anchorPane.setOnMousePressed(event -> {
anchorPane.getChildren().clear();
hBox = new HBox();
hBox.setBackground(null);
hBox.setBorder(new Border(new BorderStroke(Paint.valueOf("#c03700"), BorderStrokeStyle.SOLID,
null,new BorderWidths(3))));
anchorPane.getChildren().add(hBox);
start_x = event.getSceneX();
start_y = event.getSceneY();
AnchorPane.setLeftAnchor(hBox,start_x);
AnchorPane.setTopAnchor(hBox,start_y);
});
anchorPane.setOnMouseDragged(event -> {
Label label = new Label();
label.setAlignment(Pos.CENTER);
label.setPrefHeight(30);
label.setPrefWidth(170);
anchorPane.getChildren().add(label);
AnchorPane.setLeftAnchor(label,start_x+30);
AnchorPane.setTopAnchor(label,start_y);
label.setTextFill(Paint.valueOf("#ffffff"));
label.setStyle("-fx-background-color: #000000");
w = Math.abs(event.getSceneX()-start_x);
h = Math.abs(event.getSceneY()-start_y);
hBox.setPrefWidth(w);
hBox.setPrefHeight(h);
label.setText("宽:"+w+" 高:"+h);
});
anchorPane.setOnMouseReleased(event -> {
w = Math.abs(event.getSceneX()-start_x);
h = Math.abs(event.getSceneY()-start_y);
anchorPane.setStyle("-fx-background-color: #00000000");
Button b = new Button("剪切");
hBox.setBorder(new Border(new BorderStroke(Paint.valueOf("#85858544"), BorderStrokeStyle.SOLID,
null,new BorderWidths(3))));
hBox.getChildren().add(b);
hBox.setAlignment(Pos.BOTTOM_RIGHT);
b.setOnAction(event1 -> {
stage.close();
try {
capterImg();
} catch (Exception e) {
e.printStackTrace();
}
primaryStage.setIconified(false);
});
});
scene.setOnKeyPressed(event -> {
if(event.getCode() == KeyCode.ESCAPE){
stage.close();
primaryStage.setIconified(false);
}
});
}
处理截屏与图像的保存
public void capterImg() throws Exception {
Robot robot = new Robot();
Rectangle re = new Rectangle((int)start_x,(int)start_y,(int)w,(int)h);
BufferedImage screenCapture = robot.createScreenCapture(re);
BufferedImage bufferedImage = screenCapture;
WritableImage writableImage = SwingFXUtils.toFXImage(bufferedImage, null);
iv.setImage(writableImage);
Clipboard cb = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putImage(writableImage);
cb.setContent(content);
ImageIO.write(bufferedImage,"png",new File("E:/capter.png"));
}
背景透明处理
public static BufferedImage transferAlpha(BufferedImage img) {
BufferedImage bufferedImage = null;
try {
int RGB = img.getRGB(img.getMinX(),img.getMinY());
int R = (RGB & 0xff0000) >>16;
int G= (RGB & 0xff00) >> 8;
int B = (RGB & 0xff);
bufferedImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int j1 = img.getMinY(); j1 < img.getHeight(); j1++) {
for (int j2 = img.getMinX(); j2 < img.getWidth(); j2++) {
int rgb = img.getRGB(j2, j1);
int r =(rgb & 0xff0000 ) >> 16 ;
int g= (rgb & 0xff00 ) >> 8 ;
int b= (rgb & 0xff );
if((Math.abs(r-R)<15) && (Math.abs(g-G)<15) && (Math.abs(b-B)<15)){
rgb = (1 << 24) | (rgb & 0x00ffffff);
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
}
return bufferedImage;
}
整体效果展示