Saturday, December 29, 2012

Save WritableImage to file

To save WritableImage to file, e can use the code:


                    File file = new File("test.png");
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(
renderedImage,
"png",
file);


Where we render the image by calling SwingFXUtils.fromFXImage() and write to file by calling ImageIO.write().

Complete example:

Modify the code in last post "Create and adjust Color using hue, saturation, brightness". Add a button to save the image once button clicked.

package testjavafx;

import java.awt.image.BufferedImage;
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.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.Slider;
import javafx.scene.control.SliderBuilder;
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.layout.HBox;
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 TestJavaFX extends Application {

PixelReader pixelReader;
int width, height;

WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;

Slider sliderHue, sliderSaturation, sliderBrightness;
double adjHue, adjSaturation, adjBrightness;

@Override
public void start(Stage primaryStage) {

Image image = new Image("http://goo.gl/kYEQl");
ImageView imageView = new ImageView();
imageView.setImage(image);

pixelReader = image.getPixelReader();
width = (int)image.getWidth();
height = (int)image.getHeight();

//Copy from source to destination pixel by pixel
writableImage = new WritableImage(width, height);
pixelWriter = writableImage.getPixelWriter();

writableImageHue = new WritableImage(width, height);
pixelWriterHue = writableImageHue.getPixelWriter();

writableImageSaturation = new WritableImage(width, height);
pixelWriterSaturation = writableImageSaturation.getPixelWriter();

writableImageBrightness = new WritableImage(width, height);
pixelWriterBrightness = writableImageBrightness.getPixelWriter();

destImageView = new ImageView();
imageViewHue = new ImageView();
imageViewSaturation = new ImageView();
imageViewBrightness = new ImageView();

HBox hBox_Image = new HBox();
hBox_Image.getChildren().addAll(imageView, destImageView);

//Control box for Hue
sliderHue = SliderBuilder.create()
.prefWidth(300)
.min(-360)
.max(360)
.majorTickUnit(30)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.build();
sliderHue.valueProperty().addListener(sliderChangeListener);
HBox hBox_Hue = new HBox();
hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);

//Control box for Saturation
sliderSaturation = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.build();
sliderSaturation.valueProperty().addListener(sliderChangeListener);
HBox hBox_Saturation = new HBox();
hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);

//Control box for Blue
sliderBrightness = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.build();
sliderBrightness.valueProperty().addListener(sliderChangeListener);
HBox hBox_Brightness = new HBox();
hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);

Button buttonSave = new Button("Save");
buttonSave.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
try {
File file = new File("test.png");
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(
renderedImage,
"png",
file);
} catch (IOException ex) {
Logger.getLogger(TestJavaFX.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(hBox_Image,
hBox_Hue, hBox_Saturation, hBox_Brightness, buttonSave);

StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 350, 400);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();

updateImage();
}

ChangeListener<Number> sliderChangeListener
= new ChangeListener<Number>(){

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
updateImage();
}
};

private void updateImage(){
adjHue = sliderHue.valueProperty().doubleValue();
adjSaturation = sliderSaturation.valueProperty().doubleValue();
adjBrightness = sliderBrightness.valueProperty().doubleValue();

for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
Color color = pixelReader.getColor(x, y);
pixelWriter.setColor(x, y, color);

double hue = color.getHue() + adjHue;
if(hue > 360.0){
hue = hue - 360;
}else if(hue < 0.0){
hue = hue + 360.0;
}

double saturation = color.getSaturation() + adjSaturation;
if(saturation > 1.0){
saturation = 1.0;
}else if(saturation < 0.0){
saturation = 0.0;
}

double brightness = color.getBrightness() + adjBrightness;
if(brightness > 1.0){
brightness = 1.0;
}else if(brightness < 0.0){
brightness = 0.0;
}

double opacity = color.getOpacity();

pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));

Color newColor = Color.hsb(hue, saturation, brightness, opacity);
pixelWriter.setColor(x, y, newColor);

}
}

imageViewHue.setImage(writableImageHue);
imageViewSaturation.setImage(writableImageSaturation);
imageViewBrightness.setImage(writableImageBrightness);
destImageView.setImage(writableImage);
}

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



No comments:

Post a Comment