JavaFX 中的有效图像裁剪 [英] Effective image cropping in JavaFX

查看:30
本文介绍了JavaFX 中的有效图像裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JavaFX 编写拼图游戏,部分原因是有人问我,部分原因是我想尝试一下 JavaFX.但是,我在实际裁剪图像时遇到了困难.

I'm trying to write a Jigsaw puzzle using JavaFX partly because someone asked me and partly because I want to give JavaFX a go. However, I am having difficulty with the actual cropping of the image.

这个想法是让用户提供图像和程序将图像切割成更小的部分.很简单吧?我的问题是:我能找到的剪切图像的唯一方法是制作图像对象的副本并更改副本的可见部分,这是一个示例:

The idea is for the user to supply the image and the program to cut the image into smaller pieces. Simple, right? My problem is this: The only way I could find to cut the image is to make a copy of the image object and change the visible part of the copy, here's an example:

ImageView imgageView = new ImageView(); // Creates a new ImageView; this will be a single puzzle piece.
imgageView.setImage(originalImage); // Use the original image as the "base."
Rectangle2D rectangle = new Rectangle2D(0, 0, 50, 50); // Crop the image from (0,0) to (50, 50).

只是为了澄清最后一行,这里是 中的相关部分API:

Just to clarify the last line, here's the related piece in the API:

public Rectangle2D(double minX,
           double minY,
           double width,
           double height)
Creates a new instance of Rectangle2D.
Parameters:
minX - The x coordinate of the upper-left corner of the Rectangle2D
minY - The y coordinate of the upper-left corner of the Rectangle2D
width - The width of the Rectangle2D
height - The height of the Rectangle2D

现在如果我将图片切成四块或九块就可以了(这个游戏是为幼儿设计的),但是如果我想把图片切成一个 1200 块的拼图怎么办?这不会导致极其昂贵的操作吗?不仅是裁剪本身,还要在内存中保留大量的图像副本.看,如果我理解正确的话,每一块都将由整个原始图像组成,其中很大一部分将保持隐藏"状态.

Now this is fine if I'm cutting the picture into four or nine pieces (the game is meant for young children), but what if I want to cut the picture into a nice 1200 piece puzzle? Will this not result in an extremely expensive operation? Not only the cropping itself, but to keep that many copies of the image in memory. See, if I'm understanding this correctly each piece will consist of the entire original image with a large portion of it that will remain "hidden."

我只是误解了这个功能吗?如果没有,一定有更好的方法来做到这一点,对吗?

Am I simply misunderstanding the function? If not, there must be a better way to do this, right?

推荐答案

使用 PixelReader 和 WritableImage 应该会有所帮助.

Using PixelReader and WritableImage should help.

以下从旧图像在 position (x,y)size (width, height)

the following cuts a new image from an old one at position (x,y) and size (width, height)

PixelReader reader = oldImage.getPixelReader();
WritableImage newImage = new WritableImage(reader, x, y, width, height);

这篇关于JavaFX 中的有效图像裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆