0

ImageView を使用して画像のサイズを変更したいのですが、画像のサイズが変更されていません。

ImageView imageView = new ImageView(image); 
imageView.setPreserveRatio(true);
imageView.setFitHeight(40);
System.out.println("imageview image width = " + imageView.getImage().getWidth());
System.out.println("imageview image height = " + imageView.getImage().getHeight());

出力は

imageview image width = 674.0
imageview image height = 888.0

ただし、幅は40にする必要があります。私のImageViewはどのシーンにも添付されておらず、添付したくありません。画像のサイズ変更にのみ使用されます。ImageView がどのシーンにもアタッチされていなくても、ImageView のイメージのサイズを強制的に変更する方法はありますか? サイズ変更に ImageView を使用している理由は、ディスクから再度読み取らずに、RAM でイメージのサイズを変更したいからです。詳細については、この質問を参照してください。

ヒントをありがとう!

4

1 に答える 1

0

サイズ変更に ImageView を使用するのは非常にハックなようです。

より良いアプローチは、Image を BufferedImage に変換し、古い方法でサイズ変更を行うことです。(JavaFx は (まだ) メモリ イメージのサイズを変更する内部的な方法を提供していません)

int width = 500; // desired size
int height = 400;
Image original = ...; // fx image

BufferedImage img = new BufferedImage(
        (int)original.getWidth(),
        (int)original.getHeight(),
        BufferedImage.TYPE_INT_ARGB);

SwingFXUtils.fromFXImage(original, img);
BufferedImage rescaled = Scalr.rescaleImage(img, width, heigth);  // the actual rescale

// convert back to FX image
WritableImage rescaledFX = new WritableImage(width, heigth);
SwingFXUtils.toFXImage(rescaled, rescaledFX);

Scalrは、ネイティブ Java で画像のサイズを変更するための優れたライブラリです明らかに、他の/より単純な再スケーリング方法を使用できますが、画質はそれほど良くありません。

于 2013-11-05T08:21:58.683 に答える