JavaFX - 图像
-
简述
您可以使用包中 JavaFX 提供的类加载和修改图像 javafx.scene.image. JavaFX 支持图像格式,如Bmp, Gif, Jpeg, Png.本章教您如何将图像加载到 JavaFX、如何在多个视图中投影图像以及如何更改图像的像素。 -
加载图像
您可以通过实例化名为的类在 JavaFX 中加载图像 Image 包的 javafx.scene.image.对于类的构造函数,您必须传递以下任一参数 --
一个 InputStream 要加载的图像的对象,或者,
-
保存图像 URL 的字符串变量。
//Passing FileInputStream object as a parameter FileInputStream inputstream = new FileInputStream("C:\\images\\image.jpg"); Image image = new Image(inputstream); //Loading image from URL //Image image = new Image(new FileInputStream("url for the image));
加载图像后,您可以通过实例化图像来设置图像的视图 ImageView 类并将图像传递给其构造函数,如下所示 -ImageView imageView = new ImageView(image);
例子
以下是演示如何在 JavaFX 中加载图像并设置视图的示例。将此代码保存在名称为的文件中 ImageExample.java.import java.io.FileInputStream; import java.io.FileNotFoundException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class ImageExample extends Application { @Override public void start(Stage stage) throws FileNotFoundException { //Creating an image Image image = new Image(new FileInputStream("path of the image")); //Setting the image view ImageView imageView = new ImageView(image); //Setting the position of the image imageView.setX(50); imageView.setY(25); //setting the fit height and width of the image view imageView.setFitHeight(455); imageView.setFitWidth(500); //Setting the preserve ratio of the image view imageView.setPreserveRatio(true); //Creating a Group object Group root = new Group(imageView); //Creating a scene object Scene scene = new Scene(root, 600, 500); //Setting title to the Stage stage.setTitle("Loading an image"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]) { launch(args); } }
使用以下命令从命令提示符编译并执行保存的 java 文件。Javac ImageExample.java java ImageExample
执行时,上述程序会生成一个 JavaFX 窗口,如下所示 - -
-
图像的多个视图
您还可以为同一场景中的图像设置多个视图。以下程序是一个示例,演示如何在 JavaFX 中为场景中的图像设置各种视图。将此代码保存在名称为的文件中 MultipleViews.java.import java.io.FileInputStream; import java.io.FileNotFoundException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class MultipleViews extends Application { @Override public void start(Stage stage) throws FileNotFoundException { //Creating an image Image image = new Image(new FileInputStream("file path")); //Setting the image view 1 ImageView imageView1 = new ImageView(image); //Setting the position of the image imageView1.setX(50); imageView1.setY(25); //setting the fit height and width of the image view imageView1.setFitHeight(300); imageView1.setFitWidth(250); //Setting the preserve ratio of the image view imageView1.setPreserveRatio(true); //Setting the image view 2 ImageView imageView2 = new ImageView(image); //Setting the position of the image imageView2.setX(350); imageView2.setY(25); //setting the fit height and width of the image view imageView2.setFitHeight(150); imageView2.setFitWidth(250); //Setting the preserve ratio of the image view imageView2.setPreserveRatio(true); //Setting the image view 3 ImageView imageView3 = new ImageView(image); //Setting the position of the image imageView3.setX(350); imageView3.setY(200); //setting the fit height and width of the image view imageView3.setFitHeight(100); imageView3.setFitWidth(100); //Setting the preserve ratio of the image view imageView3.setPreserveRatio(true); //Creating a Group object Group root = new Group(imageView1, imageView2, imageView3); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle("Multiple views of an image"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]) { launch(args); } }
使用以下命令从命令提示符编译并执行保存的 java 文件。Javac MultipleViews.java java MultipleViews
执行时,上述程序会生成一个 JavaFX 窗口,如下所示 - -
写入像素
JavaFX 提供名为的类 PixelReader 和 PixelWriter类来读取和写入图像的像素。这WritableImage 类用于创建可写图像。以下是演示如何读取和写入图像像素的示例。在这里,我们正在读取图像的颜色值并使其变暗。将此代码保存在名称为的文件中 WritingPixelsExample.java.import java.io.FileInputStream; import java.io.FileNotFoundException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.image.PixelReader; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import javafx.scene.paint.Color; import javafx.stage.Stage; public class WritingPixelsExample extends Application { @Override public void start(Stage stage) throws FileNotFoundException { //Creating an image Image image = new Image(new FileInputStream("C:\\images\\logo.jpg")); int width = (int)image.getWidth(); int height = (int)image.getHeight(); //Creating a writable image WritableImage wImage = new WritableImage(width, height); //Reading color from the loaded image PixelReader pixelReader = image.getPixelReader(); //getting the pixel writer PixelWriter writer = wImage.getPixelWriter(); //Reading the color of the image for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { //Retrieving the color of the pixel of the loaded image Color color = pixelReader.getColor(x, y); //Setting the color to the writable image writer.setColor(x, y, color.darker()); } } //Setting the view for the writable image ImageView imageView = new ImageView(wImage); //Creating a Group object Group root = new Group(imageView); //Creating a scene object Scene scene = new Scene(root, 600, 500); //Setting title to the Stage stage.setTitle("Writing pixels "); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]) { launch(args); } }
使用以下命令从命令提示符编译并执行保存的 java 文件。Javac WritingPixelsExample.java java WritingPixelsExample
执行时,上述程序会生成一个 JavaFX 窗口,如下所示 -