【问题标题】:Resize an image without reading it again from disk?调整图像大小而不从磁盘再次读取它?
【发布时间】:2013-02-01 09:28:21
【问题描述】:

我需要根据图像的最长边调整图像大小,例如最长边 - 可以是宽度或高度 - 应该只有 100 像素长。

目前我正在使用这种方法:

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 构造函数来实现...任何提示/对此有何改进?谢谢:-)

【问题讨论】:

    标签: image javafx-2 image-manipulation


    【解决方案1】:

    您只需要ImageView

    在内存中加载一次图像,然后在需要时对图像视图进行操作。

    示例代码:

    //load image to memory
    final static Image MY_IMAGE = new Image(path of image file);
    //create different imageviews pointing to image in memory and do manipulations
    ImageView myImage = new ImageView(MY_IMAGE);
    myImage.setFitHeight(value);
    myImage.setFitWidth(value);
    

    【讨论】:

    • 感谢您的回复,不幸的是我的 ImageView 拒绝调整图像大小,我认为原因是它没有附加到任何场景。详情请看这篇帖子:stackoverflow.com/questions/14924995/…
    • 看来我误解了你的问题(我以为你正在调整图像大小并将其显示在场景中),对 imageview 进行的操作不会影响真实图像。它与是否附加到场景无关。跨度>
    【解决方案2】:

    不变量接近最终解决方案,最终解决方案的学分转到l2p in the oracle forum

    ImageView 不会改变实际的图像,无论它是否在场景中。它只渲染给定图像中的可见节点。如果你想让它从当前可见状态渲染一个新图像,你必须创建一个快照()。 所以:

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

    错误修正:

    当前方法失去了透明度,因为默认控件(此处:imageview)背景为白色。白色的透明度截图(来自我们的图像)给出了白色,所以我们失去了原来的透明度。解决此问题:将背景本身设置为透明。

    所以,固定代码:

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

    见鬼,这东西很棘手;-)

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多