package javafximage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.SnapshotParameters; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.effect.DropShadow; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javax.imageio.ImageIO; /** * * @web http://java-buddy.blogspot.com */ public class JavaFXImage extends Application { @Override public void start(Stage primaryStage) { Image image = new Image("http://goo.gl/kYEQl"); ImageView imageView = new ImageView(); imageView.setImage(image); imageView.setEffect(new DropShadow(20, Color.BLACK)); ImageView retrievedImage = new ImageView(); Label labelPath = new Label(); Button btnSnapShot = new Button("Take SnapShot"); btnSnapShot.setOnAction((ActionEvent event) -> { File savedFile = takeSnapShot(imageView); retrieveImage(savedFile, retrievedImage, labelPath); }); Button btnSaveImage = new Button("Save"); btnSaveImage.setOnAction((ActionEvent event) -> { File savedFile = saveImage(imageView); retrieveImage(savedFile, retrievedImage, labelPath); }); VBox vBox = new VBox(); vBox.setSpacing(5); vBox.setPadding(new Insets(5, 5, 5, 5)); vBox.getChildren().addAll(imageView, btnSnapShot, btnSaveImage, retrievedImage, labelPath); StackPane root = new StackPane(); root.getChildren().add(vBox); Scene scene = new Scene(root, 400, 350); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } //Take SnapShot and save private File takeSnapShot(Node node){ WritableImage writableImage = node.snapshot(new SnapshotParameters(), null); File file = new File("snapshot.png"); try { ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", file); System.out.println("snapshot saved: " + file.getAbsolutePath()); return file; } catch (IOException ex) { Logger.getLogger(JavaFXImage.class.getName()).log(Level.SEVERE, null, ex); return null; } } //Save Image of ImageView private File saveImage(ImageView iv){ Image img = iv.getImage(); File file = new File("savedImage.png"); RenderedImage renderedImage = SwingFXUtils.fromFXImage(img, null); try { ImageIO.write(renderedImage, "png", file); System.out.println("Image saved: " + file.getAbsolutePath()); return file; } catch (IOException ex) { Logger.getLogger(JavaFXImage.class.getName()).log(Level.SEVERE, null, ex); return null; } } //Retrieve saved image private void retrieveImage(File file, ImageView imageView, Label label){ if(file != null){ Image image = new Image(file.toURI().toString()); imageView.setImage(image); label.setText(file.getName() + "\n" + image.getWidth() + " x " + image.getHeight()); }else{ label.setText(""); imageView.setImage(null); } } }
Friday, January 9, 2015
Apply DropShadow effect on ImageView
The example code show how to apply DropShadow effect on ImageView, by calling imageView.setEffect(new DropShadow()). Also save the image using node.snapshot() and ImageView.getImage(), to check how it affect the image.
No comments:
Post a Comment