您的问题太多,无法在StackOverflow上得到解答.我建议你先阅读
official Oracle documentation about JavaFX.
但是,由于这是一个有趣的话题,这里是代码中的答案.
您需要考虑以下几点:
>使用ImageView作为容器
>如果图像较大,请使用ScrollPane
>提供选择机制
>裁剪图像本身
>将图像保存到文件,提供文件选择器对话框
这就是它.在下面的示例中,使用鼠标左键进行选择,使用鼠标右键选择裁剪上下文菜单,然后在选择边界处获取ImageView节点的快照,然后将图像保存到文件中.
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
/**
* Load image,provide rectangle for rubberband selection. Press right mouse button for "crop" context menu which then crops the image at the selection rectangle and saves it as jpg.
*/
public class ImageCropWithRubberBand exten

这篇博客介绍如何利用JavaFX实现图像裁剪功能。通过创建一个ImageView容器,配合ScrollPane处理大图,并添加选择机制,允许用户用鼠标选择裁剪区域。在选择后,程序会截取选区并保存为jpg文件。
最低0.47元/天 解锁文章
1240

被折叠的 条评论
为什么被折叠?



