- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Invert the color of an image using JavaFX?
JavaFX provides two interface namely PixelReader and PixelWriter in the javafx.scene.image. Using the methods provided by them you can read and write the contents of an image such as pixels, color values, etc.
You can get the objects of these interfaces using the getPixelReader() and getPixelWriter() methods respectively.
To invert an image −
Create an InputStream object by passing the URL(String) of the required image.
Instantiate the Image class bypassing the above-created input stream object as a parameter.
Get the PixelReader and PixelWriter objects of the loaded image using the respective methods.
Read each color value of the image using the getColor() method of the ImageReader class.
Invert each color value using the invert() method of the javafx.scene.paint.Color class.
Set the inverted color values to the image using the setColor() method of the ImageWriter class.
Finally, display the resultant image using the ImageView.
Example
Following JavaFX program reads the pixels of an image, inverts the color values and displays the result −
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; 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.paint.Color; import javafx.stage.Stage; public class ReadingWritingPixels extends Application { public void start(Stage stage) throws IOException { //creating the image object InputStream stream = new FileInputStream("D:\images\elephant.jpg"); Image image = new Image(stream); //Reading the pixels PixelReader reader = image.getPixelReader(); //Writing the read pixels int w = (int)image.getWidth(); int h = (int)image.getHeight(); WritableImage wImage = new WritableImage(w, h); PixelWriter writer = wImage.getPixelWriter(); //Reading the color of the image for(int y = 0; y < h; y++) { for(int x = 0; x < w; x++) { //Retrieving the color of the pixel of the loaded image Color color = reader.getColor(x, y); //Setting the color to the writable image writer.setColor(x, y, color.invert()); } } //Creating the image view ImageView imageView = new ImageView(); //Setting image to the image view imageView.setImage(wImage); //Setting the image view parameters imageView.setX(10); imageView.setY(10); imageView.setFitWidth(575); imageView.setPreserveRatio(true); //Setting the Scene object Group root = new Group(imageView); Scene scene = new Scene(root, 595, 370, Color.BEIGE); stage.setTitle("Reading/Writing Pixels"); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } }
Output
- Related Articles
- How to invert the colors of an image in Node Jimp?
- How to add an image as label using JavaFX?
- JavaFX example to decrease the brightness of an image using OpenCV.
- How to invert a binary image in OpenCV using C++?
- How to change the color spaces of an image using Java OpenCV library?
- PyTorch – How to invert the colors of an image randomly with a given probability?
- How to display an image in JavaFX?
- How to change the aspect ratio of an image in JavaFX?
- Altering the brightness and contrast of an image using JavaFX and OpenCV
- OpenCV JavaFX application to alter the sharpness of an image
- How to add scroll bar to an image in JavaFX?
- How to add context menu to an image in JavaFX?
- How to add an image to a button (action) in JavaFX?
- How to color the plotted area of a JavaFX xy-chart?
- How to create an Arc using JavaFX?
