Search code examples
javabufferedimagegraphics2d

Set Java BufferedImage User Space to EMU units


I am creating and rendering to a BufferedImage. This bitmap will be written to a printer after it's complete. I want to do all drawing using EMUs as the units. The bitmap will be 600 dpi. So [0,0] is the upper left corner but a pixel written to [914400,914400], will be at [600,600] in the bitmap.

How do I set the User Space for this? I know it's using the transform somehow, but everything I've tried hasn't worked (I think).


Solution

  • Ok, this is ridiculously simple. I was trying to build the AffineTransform from scratch. All I needed to do is:

    /** Emus Per Inch */
    public static final int EPI = 914400;
    
    image = new BufferedImage(pixelsWidth, pixelsHeight, BufferedImage.TYPE_INT_ARGB);
    graphics = image.createGraphics();
    
    AffineTransform scaleToEmus = AffineTransform.getScaleInstance((float)dpi / (float)IDrawingSurface.EPI, (float)dpi / (float)IDrawingSurface.EPI);
    graphics.transform(scaleToEmus);
    

    And that's it. getScaleInstance() does all the heavy lifting (I remembered that it was something very simple, I just couldn't remember what!)

    dpi is whatever DPI you want for the bitmap.