欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaFX学习之Image

程序员文章站 2022-06-08 10:44:37
...
[b]ImageView and Image[/b]

ImageView iv = new ImageView(image);
iv.setFitWidth(200);
iv.setPreserveRatio(true);
iv.setSmooth(true);

ImageView iv2 = new ImageView(image);
iv2.setViewport(new Rectangle2D(192, 256, 300, 300));

setFitHeight(double value)
setFitWidth(double value) 设置ImageView的大小
setPreserveRatio(boolean value) setPreserveRatio(boolean)设置比率,按比率去缩小放大。当你只设置了宽度时,宽度会缩小,但是高度仍然不变,图像就畸形了。若设置为true,则高度也会按比率缩小。
setSmooth(boolean value) 若为true,则用较好的过滤算法花时间,若为false,则快速
setViewport(Rectangle2D value) 设置可视口,因为图片的大小经常大于你ImageViw的设置的大小,而你不想按比率缩放,只想显示图像的一部分,看一下Rectangle2D的参数Rectangle2D(double minX, double minY, double width, double height) minX,Y图片左上的位置,width,height宽度高度,这样就可以从图片上截取一块区域,然后显示出来。

WritableImage 可编辑的image
PixelReader 获取image像素数据
PixelWriter 写入image像素数据

Image image = new Image("http://docs.oracle.com/javafx/"
+ "javafx/images/javafx-documentation.png");
ImageView imageView = new ImageView();
imageView.setImage(image);
// Obtain PixelReader
PixelReader pixelReader = image.getPixelReader();
System.out.println("Image Width: "+image.getWidth());
System.out.println("Image Height: "+image.getHeight());
System.out.println("Pixel Format: "+pixelReader.getPixelFormat());

// Create WritableImage
WritableImage wImage = new WritableImage(
(int)image.getWidth(),
(int)image.getHeight());
PixelWriter pixelWriter = wImage.getPixelWriter();

// Determine the color of each pixel in a specified row
for(int readY=0;readY<image.getHeight();readY++){
for(int readX=0; readX<image.getWidth();readX++){
Color color = pixelReader.getColor(readX,readY);
System.out.println("\nPixel color at coordinates ("+
readX+","+readY+") "
+color.toString());
System.out.println("R = "+color.getRed());
System.out.println("G = "+color.getGreen());
System.out.println("B = "+color.getBlue());
System.out.println("Opacity = "+color.getOpacity());
System.out.println("Saturation = "+color.getSaturation());

// Now write a brighter color to the PixelWriter.
color = color.brighter();
pixelWriter.setColor(readX,readY,color);
}
}

// Display image on screen
//imageView.setImage(wImage);
StackPane root = new StackPane();
root.getChildren().add(imageView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Image Write Test");
primaryStage.setScene(scene);
primaryStage.show();


PixelFormat 定义图像像素数据的格式
WritablePixelFormat 可编辑图像像素格式

PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteRgbInstance();
pixelWriter.setPixels(int x, int y, int w, int h, PixelFormat<ByteBuffer> pixelformat, byte[] buffer, int offset, int scanlineStride)