WritableImage is of lower quality and larger in size than BufferedImage?
Kevin Rushforth
kevin.rushforth at oracle.com
Sat Aug 16 21:03:50 UTC 2014
Canvas does not support LCD antialiasing on text, which may be the
difference you are seeing. See
https://javafx-jira.kenai.com/browse/RT-23822 which is targeted for JDK
9, but we are looking at bringing it into an 8-update release (possibly
8u40).
-- Kevin
Anirvan Sarkar wrote:
> I first created a BufferedImage using the Graphics2D API and then a similar
> WritableImage using GraphicsContext and Canvas.snapshot(null, null) method.
> The WritableImage is of lower quality and larger in size than BufferedImage.
>
> Is this a JavaFX bug or I am missing some RenderingHints for
> GraphicsContext?
>
> The test programs which I used:
>
> ****************************************************************************************
> BufferedImageExample.java
> ****************************************************************************************
> import java.awt.Font;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.RenderingHints;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.IOException;
>
> import javax.imageio.ImageIO;
>
> public class BufferedImageExample {
>
> public static void main(String[] args) throws IOException {
>
> int width = 160;
> int height = 60;
>
> BufferedImage image = new BufferedImage(width, height,
> BufferedImage.TYPE_INT_RGB);
> Graphics g = image.getGraphics();
> g.setFont(new Font("SansSerif", Font.BOLD, 36));
>
> Graphics2D graphics = (Graphics2D) g;
> graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
> RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
> graphics.drawString("JavaFX", 15, 45);
>
> //save this image
> ImageIO.write(image, "png", new File("BufferedImageExample.png"));
> }
> }
> ****************************************************************************************
>
> ****************************************************************************************
> WritableImageExample.java
> ****************************************************************************************
> import java.io.File;
> import java.io.IOException;
>
> import javafx.application.Application;
> import javafx.embed.swing.SwingFXUtils;
> import javafx.scene.canvas.Canvas;
> import javafx.scene.canvas.GraphicsContext;
> import javafx.scene.image.WritableImage;
> import javafx.scene.text.Font;
> import javafx.scene.text.FontWeight;
> import javafx.scene.paint.Color;
> import javafx.stage.Stage;
>
> import javax.imageio.ImageIO;
>
> public class WritableImageExample extends Application {
>
> @Override
> public void start(final Stage primaryStage) throws IOException {
>
> int width = 160;
> int height = 60;
>
> Canvas canvas = new Canvas(width, height);
> GraphicsContext gc = canvas.getGraphicsContext2D();
>
> gc.setFill(Color.BLACK);
> gc.fillRect(0, 0, width, height);
> gc.setFill(Color.WHITE);
> gc.setFont(Font.font("SansSerif", FontWeight.BOLD, 36));
> gc.fillText("JavaFX", 15, 45);
>
> WritableImage image = canvas.snapshot(null, null);
>
> //save this image
> ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new
> File("WritableImageExample.png"));
>
> System.exit(0);
> }
> }
> ****************************************************************************************
>
>
More information about the openjfx-dev
mailing list