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

查看:1101
本文介绍了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.

以下内容从位置(x,y)大小的旧图像中剪切新图像(宽度,高度)

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

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

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