Tuesday, June 17, 2014

PixelReader, PixelWriter and WritableImage

Example to show how to copy image to WritableImage with PixelReader, PixelWriter.



package javafx8image;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
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.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX8Image extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Button btn = new Button();
        ImageView imageView = new ImageView();
        ImageView imageViewDest = new ImageView();
        Label label = new Label();
        
        imageView.setOnMouseClicked(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                imageView.setImage(null);
                imageViewDest.setImage(null);
                label.setText("");
            }
        });

        
        btn.setText("Load image");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                Image image = new Image("http://goo.gl/kYEQl");
                imageView.setImage(image);
                
                //copy image
                PixelReader pixelReader = image.getPixelReader();
                WritableImage writableImage = 
                    new WritableImage(
                        (int)image.getWidth(), 
                        (int)image.getHeight());
                PixelWriter pixelWriter = writableImage.getPixelWriter();
                
                for(int y=0; y<image.getHeight(); y++){
                    for(int x=0; x<image.getWidth(); x++){
                        Color color = pixelReader.getColor(x, y);
                        color = color.brighter();
                        pixelWriter.setColor(x, y, color);
                    }
                }
                
                imageViewDest.setImage(writableImage);
                label.setText(image.getWidth() + " x " + image.getHeight());
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(btn, imageView, imageViewDest, label);
        
        StackPane root = new StackPane();
        root.getChildren().addAll(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}


Next:
- Various effect of creating Color with brighter(), darker(), saturate(), desaturate(), grayscale() and invert().

No comments:

Post a Comment