调整图像大小而无需再次从磁盘读取图像? [英] Resize an image without reading it again from disk?

查看:96
本文介绍了调整图像大小而无需再次从磁盘读取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据最长的一面来调整图像的大小,例如最长的一面-可以是宽度或高度-只能长100像素.

I need to resize an image based on it's longest side, e.g. longest side - which can be width or height - should be only 100 pixels long.

当前我正在使用此方法:

Currently I am using this method:

private Image resizeImageByLongestSide(File imageFile, int lengthLongestSide)
{
    String uri ="file:" + imageFile.getAbsolutePath();
    Image image = new Image(uri); // raed to determine width/height

    // read image again for resizing
    if(image.getWidth() >= image.getHeight())
        return new Image(uri, lengthLongestSide, 0, true, false);
    else
        return new Image(uri, 0, lengthLongestSide, true, false);
}

因此,首先必须从磁盘读取图像以找出哪一侧是最长的一侧,而不是从磁盘再次读取,因为调整大小似乎只能通过使用Image构造函数进行...任何提示/对此有所改善?谢谢:-)

So, first the image has to be read by disk to figure out which side is the longest side, than it has again to be read from the disk, because resizing seems only possible by using the Image constructor... Any hint/improvement on this? Thanks :-)

推荐答案

不变式接近最终解决方案,最终解决方案的功劳归于

invariant was close to the final solution, credits for the final solution go to l2p in the oracle forum:

ImageView不会更改实际的Image,无论它是否在场景中.它仅从给定图像渲染可见节点.如果希望它从其当前可见状态渲染新图像,则必须制作一个snapshot(). 所以:

ImageView does not alter the actual Image whether it is in a scene or not. It only renders a visible node from the given image. If you want it to render a new Image from its current visible state you have to make a snapshot(). So:

Image resizedImage = myImageView.snapshot(null, null); // after using myImageView.setFitHeight()/setFitWidth/( etc.

Bugfix:

当前方法失去了透明度,因为默认控件(herE:imageview)的背景是白色的.白色上的透明度(来自我们的图像)的屏幕截图显示为白色,因此我们失去了原始的透明度.解决此问题的方法:将背景本身设置为透明.

Transparency is lost with the current approach, because default control (herE: imageview) background is white. screenshot of transparency (from our image) on white gives white, so we loose the original transparency. fix for this: set the background itself to transparent.

因此,固定代码:

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT); 
return imageView.snapshot(params, null); 

地狱,这东西很棘手;-)

Hell, tricky this stuff ;-)

这篇关于调整图像大小而无需再次从磁盘读取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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