< prev index next >

src/java.desktop/share/classes/java/awt/Robot.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -25,10 +25,12 @@
 
 package java.awt;
 
 import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
+import java.awt.geom.AffineTransform;
+import java.awt.image.BaseMultiResolutionImage;
 import java.awt.image.BufferedImage;
 import java.awt.image.DataBufferInt;
 import java.awt.image.DirectColorModel;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;

@@ -403,15 +405,36 @@
      * @throws  SecurityException if {@code readDisplayPixels} permission is not granted
      * @see     SecurityManager#checkPermission
      * @see     AWTPermission
      */
     public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
+        return (BufferedImage) createScreenCapture(screenRect, false);
+    }
+
+    /**
+     * Creates an image containing pixels read from the screen.
+     * This image does not include the mouse cursor.
+     * Returns BufferedImage for Non-HiDPI display and  
+     * MultiResolutionImage for HiDPI display with two resolution variants, 
+     * Base Image with user specified size and
+     * High Resolution Image with original size.
+     * @param   screenRect     Rect to capture in screen coordinates
+     * @param   isHiDPI     Indicates if HiDPI Display
+     * @return  The captured image
+     * @throws  IllegalArgumentException if {@code screenRect} width and height are not greater than zero
+     * @throws  SecurityException if {@code readDisplayPixels} permission is not granted
+     * @see     SecurityManager#checkPermission
+     * @see     AWTPermission
+     */
+    
+    public synchronized Image createScreenCapture(Rectangle screenRect, boolean isHiDPI) {
         checkScreenCaptureAllowed();
 
         checkValidRect(screenRect);
 
-        BufferedImage image;
+        BufferedImage lowResolutionImage;
+        BufferedImage highResolutionImage;
         DataBufferInt buffer;
         WritableRaster raster;
 
         if (screenCapCM == null) {
             /*

@@ -424,30 +447,88 @@
                                                /* red mask */    0x00FF0000,
                                                /* green mask */  0x0000FF00,
                                                /* blue mask */   0x000000FF);
         }
 
+        int[] bandmasks = new int[3];
+        bandmasks[0] = screenCapCM.getRedMask();
+        bandmasks[1] = screenCapCM.getGreenMask();
+        bandmasks[2] = screenCapCM.getBlueMask();
+        
         // need to sync the toolkit prior to grabbing the pixels since in some
         // cases rendering to the screen may be delayed
         Toolkit.getDefaultToolkit().sync();
-
+        AffineTransform tx = GraphicsEnvironment.
+                getLocalGraphicsEnvironment().getDefaultScreenDevice().
+                getDefaultConfiguration().getDefaultTransform();
+        double uiScaleX = tx.getScaleX();
+        double uiScaleY = tx.getScaleY();
         int pixels[];
-        int[] bandmasks = new int[3];
 
+        if (uiScaleX == 1 && uiScaleY == 1) {
         pixels = peer.getRGBPixels(screenRect);
         buffer = new DataBufferInt(pixels, pixels.length);
 
         bandmasks[0] = screenCapCM.getRedMask();
         bandmasks[1] = screenCapCM.getGreenMask();
         bandmasks[2] = screenCapCM.getBlueMask();
 
-        raster = Raster.createPackedRaster(buffer, screenRect.width, screenRect.height, screenRect.width, bandmasks, null);
+            raster = Raster.createPackedRaster(buffer, screenRect.width,
+                    screenRect.height, screenRect.width, bandmasks, null);
         SunWritableRaster.makeTrackable(buffer);
 
-        image = new BufferedImage(screenCapCM, raster, false, null);
+            return new BufferedImage(screenCapCM, raster, false, null);
+
+        } else {
+
+            int x = screenRect.x;
+            int y = screenRect.y;
+            int width = screenRect.width;
+            int height = screenRect.height;
+            int pminx = (int) Math.floor(x * uiScaleX);
+            int pminy = (int) Math.floor(y * uiScaleY);
+            int pmaxx = (int) Math.ceil((x + width) * uiScaleX);
+            int pmaxy = (int) Math.ceil((y + height) * uiScaleY);
+            int pwidth = pmaxx - pminx;
+            int pheight = pmaxy - pminy;
+            int temppixels[];
+            Rectangle scaledRect = new Rectangle(pminx, pminy, pwidth, pheight);
+            temppixels = peer.getRGBPixels(scaledRect);
 
-        return image;
+            // HighResolutionImage
+            pixels = temppixels;
+            buffer = new DataBufferInt(pixels, pixels.length);
+            raster = Raster.createPackedRaster(buffer, scaledRect.width,
+                    scaledRect.height, scaledRect.width, bandmasks, null);
+            SunWritableRaster.makeTrackable(buffer);
+
+            highResolutionImage = new BufferedImage(screenCapCM, raster,
+                    false, null);
+
+            // LowResolutionImage
+            lowResolutionImage = new BufferedImage(screenRect.width,
+                    screenRect.height, highResolutionImage.getType());
+            Graphics2D g = lowResolutionImage.createGraphics();
+            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+            g.setRenderingHint(RenderingHints.KEY_RENDERING,
+                    RenderingHints.VALUE_RENDER_QUALITY);
+            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+                    RenderingHints.VALUE_ANTIALIAS_ON);
+            g.drawImage(highResolutionImage, 0, 0,
+                    screenRect.width, screenRect.height,
+                    0, 0, scaledRect.width, scaledRect.height, null);
+            g.dispose();
+
+            if (!isHiDPI) {
+                return lowResolutionImage;
+            } else {
+                // MultiResoltuionImage
+                return new BaseMultiResolutionImage(
+                        lowResolutionImage, highResolutionImage);
+            }
+        }
     }
 
     private static void checkValidRect(Rectangle rect) {
         if (rect.width <= 0 || rect.height <= 0) {
             throw new IllegalArgumentException("Rectangle width and height must be > 0");
< prev index next >