1

I have written code that produces png image and reduce aliasing from html code.

When i run it on my api endpoint i don't have same results that in a unit test

@Test
public void createImage2() throws Exception{
    String fileNamePng = "createImage2_"+String.valueOf(System.currentTimeMillis() + ".png");

    File image = new File("/tmp/img/"+ fileNamePng);
    HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator();
    htmlImageGenerator.loadHtml(svg);
    htmlImageGenerator.saveAsImage(image);

    BufferedImage sourceImage = ImageIO.read(image);
    Graphics g = sourceImage.getGraphics();

    Graphics2D g2d = (Graphics2D) g;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    ImageIO.write(sourceImage,"png",image);
}

Result is / Size is 7.8Kb enter image description here

In my api endpoint

}else if(svg.startsWith("<div")){

       File image = new File("/tmp/"+fileName);
       HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator();
       htmlImageGenerator.loadHtml(svg);
       htmlImageGenerator.saveAsImage(image);

       BufferedImage sourceImage = ImageIO.read(image);
       Graphics g = sourceImage.getGraphics();

       Graphics2D g2d = (Graphics2D) g;

       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

       ImageIO.write(sourceImage,"png",image);

       Files.copy(image.toPath(), response.getOutputStream());
       Files.delete(image.toPath());
   }

    response.flushBuffer();

Result is : /Size is 3kb

enter image description here

it look likes all part gd2 graphics is not applied but i can see in my tmp files that when debugger is in theses lines, the date "modified on" is changed.

svg / html code in input is the same for api endpoint and unit test. (i checked haschode)

Edit 1:

I used Scalr :

              }else if(svg.startsWith("<div")){

       System.out.println(svg.hashCode());

       File image = new File("/tmp/"+fileName);
       HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator();
       htmlImageGenerator.loadHtml(svg);
       height = (int) htmlImageGenerator.getSize().getHeight();
       width = (int) htmlImageGenerator.getSize().getWidth();

       BufferedImage test3 = Scalr.apply(htmlImageGenerator.getBufferedImage(),Scalr.OP_ANTIALIAS);

       ImageIO.write(test3,"png",image);

       Files.copy(image.toPath(), response.getOutputStream());
   }

    response.flushBuffer();

Result is / Size is 4.97 Kb enter image description here

It look like blured as described in scalr doc (antialiasing from scalr is more like a low blur effect)

mik3fly-4steri5k
  • 712
  • 3
  • 15
  • 32

1 Answers1

1

It might be worth looking at Controlling Rendering Quality from the Java 2D graphics tutorials. To summarize, the rendering hints are not guaranteed to be applied on all Java platforms.

On the platform you are running your unit test, these hints are supported and are being applied. On the API endpoint, it is clear that antialiasing is not being applied, but the file is still written to the disk properly.

Since interpolation and antialiasing settings affect the image produced, the sizes of the images are also affected.

If you need these to be applied, you can:

  1. Use a different image processing library. What is the best Java image processing library approach? might be of interest.
  2. Switch your API endpoint to a different implementation of Java.
user886
  • 1,149
  • 16
  • 16