Saturday, January 19, 2013

Apply SepiaTone on ImageView

javafx.scene.effect.SepiaTone is a filter that produces a sepia tone effect, similar to antique photographs. To apply SepiaTone on a ImageView, simple call setEffect(new SepiaTone()) method of ImageView.

Apply SepiaTone on ImageView
Apply SepiaTone on ImageView


Modify from last post "Set fitWidth and fitHeight properties dynamically, with scroll bar".
package javafxpixel;

import java.awt.image.BufferedImage;
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.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXPixel extends Application {
    
    Label label;
    ImageView myImageView;
    ScrollPane scrollPane;
    
    Boolean autoFix = true;
    
    Image myImage;
    
    @Override
    public void start(Stage primaryStage) {
        Button btnLoad = new Button("Load");
        btnLoad.setOnAction(btnLoadEventListener);
        Button btnAutoFix = new Button("Auto Fix");
        btnAutoFix.setOnAction(btnAutoFixEventListener);
        Button btnNoFix = new Button("No Fix");
        btnNoFix.setOnAction(btnNoFixEventListener);
         
        label = new Label();
        myImageView = new ImageView();
        scrollPane = new ScrollPane();
        scrollPane.setPrefSize(400, 400);
        scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
        scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
        scrollPane.setContent(myImageView);
        
        HBox buttonBox = new HBox();
        buttonBox.getChildren().addAll(btnLoad, btnAutoFix, btnNoFix);
 
        VBox rootBox = new VBox();
        rootBox.getChildren().addAll(buttonBox, label, scrollPane);
        
        Scene scene = new Scene(rootBox, 500, 500);
         
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
    
    EventHandler<ActionEvent> btnLoadEventListener
    = new EventHandler<ActionEvent>(){
 
        @Override
        public void handle(ActionEvent t) {
            FileChooser fileChooser = new FileChooser();
             
            //Set extension filter
            FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
            FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
            fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
              
            //Show open file dialog
            File file = fileChooser.showOpenDialog(null);

            BufferedImage bufferedImage;
            try {
                bufferedImage = ImageIO.read(file);
                myImage = SwingFXUtils.toFXImage(bufferedImage, null);

                setFix();

            } catch (IOException ex) {
                Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    
    EventHandler<ActionEvent> btnAutoFixEventListener
    = new EventHandler<ActionEvent>(){

        @Override
        public void handle(ActionEvent t) {
            autoFix = true;
            setFix();
        }
        
    };
    
    EventHandler<ActionEvent> btnNoFixEventListener
    = new EventHandler<ActionEvent>(){

        @Override
        public void handle(ActionEvent t) {
            autoFix = false;
            setFix();
        }
        
    };
    
    private void setFix(){
        if(autoFix){
            myImageView.setFitWidth(400);
            myImageView.setFitHeight(400);
        }else{
            myImageView.setFitWidth(0);
            myImageView.setFitHeight(0);
        }
        
        myImageView.setPreserveRatio(true);
        myImageView.setSmooth(true);
        myImageView.setCache(true);
        
        myImageView.setImage(myImage);
        
        scrollPane.setContent(null);
        scrollPane.setContent(myImageView);
        
        myImageView.setEffect(new SepiaTone());
    }
}


Ask for help:

The application fail caused by java.lang.NullPointerException, when load large image (ex. 4032 x 3024 JPG from DSLR). The error log list below. Please advise.

java.lang.NullPointerException
 at com.sun.scenario.effect.impl.sw.sse.SSESepiaTonePeer.filter(SSESepiaTonePeer.java:100)
 at com.sun.scenario.effect.CoreEffect.filterImageDatas(CoreEffect.java:103)
 at com.sun.scenario.effect.SepiaTone.filterImageDatas(SepiaTone.java:34)
 at com.sun.scenario.effect.FilterEffect.filter(FilterEffect.java:178)
 at com.sun.scenario.effect.impl.prism.PrEffectHelper.render(PrEffectHelper.java:156)
 at com.sun.javafx.sg.prism.NGNode$EffectFilter.render(NGNode.java:748)
 at com.sun.javafx.sg.prism.NGNode.renderEffect(NGNode.java:494)
 at com.sun.javafx.sg.prism.NGNode$CacheFilter.impl_createImageData(NGNode.java:689)
 at com.sun.javafx.sg.BaseCacheFilter.render(BaseCacheFilter.java:134)
 at com.sun.javafx.sg.prism.NGNode$CacheFilter.render(NGNode.java:660)
 at com.sun.javafx.sg.prism.NGNode.renderCached(NGNode.java:487)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:183)
 at com.sun.javafx.sg.prism.NGImageView.doRender(NGImageView.java:97)
 at com.sun.javafx.sg.prism.NGImageView.doRender(NGImageView.java:20)
 at com.sun.javafx.sg.BaseNode.render(BaseNode.java:1145)
 at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:204)
 at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:420)
 at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:429)
 at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:320)
 at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:346)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:179)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:39)
 at com.sun.javafx.sg.BaseNode.render(BaseNode.java:1145)
 at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:204)
 at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:420)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:187)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:39)
 at com.sun.javafx.sg.BaseNode.render(BaseNode.java:1145)
 at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:204)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:187)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:39)
 at com.sun.javafx.sg.BaseNode.render(BaseNode.java:1145)
 at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:204)
 at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:420)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:187)
 at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:39)
 at com.sun.javafx.sg.BaseNode.render(BaseNode.java:1145)
 at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:117)
 at com.sun.javafx.tk.quantum.AbstractPainter.paintImpl(AbstractPainter.java:175)
 at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:73)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
 at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
 at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
 at com.sun.prism.render.RenderJob.run(RenderJob.java:37)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
 at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:98)
 at java.lang.Thread.run(Thread.java:722)
 


No comments:

Post a Comment