static void testToolkitMultiResolutionImageLoad(Image image) throws Exception { MediaTracker tracker = new MediaTracker(new JPanel()); tracker.addImage(image, 0); tracker.waitForID(0); if (tracker.isErrorAny()) { throw new RuntimeException("Error during image loading"); } tracker.removeImage(image, 0); testImageLoaded(image); int w = image.getWidth(null); int h = image.getHeight(null); Image resolutionVariant = ((MultiResolutionImage) image) .getResolutionVariant(2 * w, 2 * h); if (image == resolutionVariant) { throw new RuntimeException("Resolution variant is not loaded"); } testImageLoaded(resolutionVariant); }
static void testToolkitMultiResolutionImageChache(String fileName, URL url) { Image img1 = Toolkit.getDefaultToolkit().getImage(fileName); if (!(img1 instanceof MultiResolutionImage)) { throw new RuntimeException("Not a MultiResolutionImage"); } Image img2 = Toolkit.getDefaultToolkit().getImage(fileName); if (img1 != img2) { throw new RuntimeException("Image is not cached"); } img1 = Toolkit.getDefaultToolkit().getImage(url); if (!(img1 instanceof MultiResolutionImage)) { throw new RuntimeException("Not a MultiResolutionImage"); } img2 = Toolkit.getDefaultToolkit().getImage(url); if (img1 != img2) { throw new RuntimeException("Image is not cached"); } }
private static void getImageFromRobot() { try { Point butLoc = button.getLocationOnScreen(); Dimension butSize = button.getSize(); MultiResolutionImage multiResolutionImage = robot.createMultiResolutionScreenCapture( new Rectangle((int)butLoc.getX(), (int)butLoc.getY(), (int)butSize.getWidth(), (int)butSize.getHeight())); images = multiResolutionImage.getResolutionVariants(); } catch (Exception e) { throw new RuntimeException(e); } if(images.size() < 2) { throw new RuntimeException("HiDpi Image not captured - " + "Check is this HiDpi display system?"); } }
protected Image getImageWithResolutionVariant(String fileName, String resolutionVariantName) { synchronized (fileImgCache) { Image image = getImageFromHash(this, fileName); if (image instanceof MultiResolutionImage) { return image; } Image resolutionVariant = getImageFromHash(this, resolutionVariantName); image = createImageWithResolutionVariant(image, resolutionVariant); fileImgCache.put(fileName, image); return image; } }
protected Image getImageWithResolutionVariant(URL url, URL resolutionVariantURL) { synchronized (urlImgCache) { Image image = getImageFromHash(this, url); if (image instanceof MultiResolutionImage) { return image; } Image resolutionVariant = getImageFromHash(this, resolutionVariantURL); image = createImageWithResolutionVariant(image, resolutionVariant); String key = url.toString(); urlImgCache.put(key, image); return image; } }
public static Image map(MultiResolutionImage mrImage, Function<Image, Image> mapper) { if (mrImage instanceof MultiResolutionToolkitImage) { MultiResolutionToolkitImage mrtImage = (MultiResolutionToolkitImage) mrImage; return MultiResolutionToolkitImage.map(mrtImage, mapper); } BiFunction<Integer, Integer, Image> sizeMapper = (w, h) -> mapper.apply(mrImage.getResolutionVariant(w, h)); if (mrImage instanceof MultiResolutionCachedImage) { MultiResolutionCachedImage mrcImage = (MultiResolutionCachedImage) mrImage; return new MultiResolutionCachedImage(mrcImage.baseImageWidth, mrcImage.baseImageHeight, mrcImage.sizes, sizeMapper, false); } Image image = (Image) mrImage; int width = image.getWidth(null); int height = image.getHeight(null); return new MultiResolutionCachedImage(width, height, sizeMapper); }
private CImage createFromImage(final Image image, final boolean prepareImage) { if (image instanceof MultiResolutionImage) { List<Image> resolutionVariants = ((MultiResolutionImage) image).getResolutionVariants(); return createFromImages(resolutionVariants, prepareImage); } int[] buffer = imageToArray(image, prepareImage); if (buffer == null) { return null; } return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null))); }
public static void main(String[] args) throws Exception { Image baseMRImage = new BaseMultiResolutionImage(createImage(1), createImage(2)); testMRDisabledImage(baseMRImage); saveImages(); Image toolkitMRImage = Toolkit.getDefaultToolkit().getImage(IMAGE_NAME_1X); if (toolkitMRImage instanceof MultiResolutionImage) { testMRDisabledImage(toolkitMRImage); } }
public static void main(String[] args) throws Exception { if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) { return; } String icon = "NSImage://NSApplicationIcon"; final Image image = Toolkit.getDefaultToolkit().getImage(icon); if (!(image instanceof MultiResolutionImage)) { throw new RuntimeException("Icon does not have resolution variants!"); } MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image; int width = 0; int height = 0; for (Image resolutionVariant : multiResolutionImage.getResolutionVariants()) { int rvWidth = resolutionVariant.getWidth(null); int rvHeight = resolutionVariant.getHeight(null); if (rvWidth < width || rvHeight < height) { throw new RuntimeException("Resolution variants are not sorted!"); } width = rvWidth; height = rvHeight; } }
static void testRVSizes() { int imageSize = getSize(1); double[][] sizeArray = { {-imageSize, imageSize}, {2 * imageSize, -2 * imageSize}, {Double.POSITIVE_INFINITY, imageSize}, {Double.POSITIVE_INFINITY, -imageSize}, {imageSize, Double.NEGATIVE_INFINITY}, {-imageSize, Double.NEGATIVE_INFINITY}, {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}, {Double.NaN, imageSize}, {imageSize, Double.NaN}, {Double.NaN, Double.NaN}, {Double.POSITIVE_INFINITY, Double.NaN} }; for (double[] sizes : sizeArray) { try { MultiResolutionImage mrImage = new BaseMultiResolutionImage( 0, createRVImage(0), createRVImage(1)); mrImage.getResolutionVariant(sizes[0], sizes[1]); } catch (IllegalArgumentException ignored) { continue; } throw new RuntimeException("IllegalArgumentException is not thrown!"); } }
private static BufferedImage getBufferedImage(Image image) { if (image instanceof MultiResolutionImage) { MultiResolutionImage mrImage = (MultiResolutionImage) image; return (BufferedImage) mrImage.getResolutionVariant(32, 32); } return (BufferedImage) image; }
public static void main(String[] args) throws IOException { MultiResolutionImage tokio = loadTokio(); int desiredImageWidth = new Random().nextInt(1500); Image variant = tokio.getResolutionVariant(desiredImageWidth, 1); System.out.printf("Width of image for %d: %d%n", desiredImageWidth, variant.getWidth(null)); }
private static MultiResolutionImage loadTokio() throws IOException { List<Image> tokios = new ArrayList<>(); for (String url : IMAGE_URLS) { tokios.add(ImageIO.read(new URL(url))); } return new BaseMultiResolutionImage(tokios.toArray(new Image[0])); }
public void validateScreenCapture() throws AWTException { Robot robot = new Robot(); outputControlPanel = new JPanel(layout); GridBagConstraints ogbc = new GridBagConstraints(); MultiResolutionImage image = robot.createMultiResolutionScreenCapture(inputImageFrame.getBounds()); List<Image> imageList = image.getResolutionVariants(); int size = imageList.size(); BufferedImage lowResImage = (BufferedImage) imageList.get(0); BufferedImage highResImage = (BufferedImage) imageList.get(1); outputImageFrame = new JFrame("Output"); outputImageFrame.getContentPane().setLayout(new GridBagLayout()); ogbc.gridx = 0; ogbc.gridy = 0; ogbc.fill = GridBagConstraints.HORIZONTAL; outputControlPanel.add(new JLabel(new ImageIcon(lowResImage)), ogbc); int width = lowResImage.getWidth(); int height = lowResImage.getHeight(); JLabel labelImg1 = new JLabel("LEFT:Width: " + width + " Height: " + height); ogbc.gridx = 0; ogbc.gridy = 1; outputControlPanel.add(labelImg1, ogbc); ogbc.gridx = 1; ogbc.gridy = 0; outputControlPanel.add(new JLabel(new ImageIcon(highResImage)), ogbc); width = highResImage.getWidth(); height = highResImage.getHeight(); JLabel labelImg2 = new JLabel("RIGHT:Width: " + width + " Height: " + height); ogbc.gridx = 1; ogbc.gridy = 1; outputControlPanel.add(labelImg2, ogbc); outputControlPanel.setBackground(Color.GRAY); outputImageFrame.add(outputControlPanel); outputImageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); outputImageFrame.setBounds(600, 0, 400, 300); outputImageFrame.setLocationRelativeTo(null); outputImageFrame.setVisible(true); }
static void testToolkitMultiResolutionImage(Image image, boolean enableImageScaling) throws Exception { MediaTracker tracker = new MediaTracker(new JPanel()); tracker.addImage(image, 0); tracker.waitForID(0); if (tracker.isErrorAny()) { throw new RuntimeException("Error during image loading"); } final BufferedImage bufferedImage1x = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics(); setImageScalingHint(g1x, false); g1x.drawImage(image, 0, 0, null); checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false); Image resolutionVariant = ((MultiResolutionImage) image). getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT); if (resolutionVariant == null) { throw new RuntimeException("Resolution variant is null"); } MediaTracker tracker2x = new MediaTracker(new JPanel()); tracker2x.addImage(resolutionVariant, 0); tracker2x.waitForID(0); if (tracker2x.isErrorAny()) { throw new RuntimeException("Error during scalable image loading"); } final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics(); setImageScalingHint(g2x, enableImageScaling); g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null); checkColor(bufferedImage2x.getRGB(3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling); if (!(image instanceof MultiResolutionImage)) { throw new RuntimeException("Not a MultiResolutionImage"); } MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image; Image image1x = multiResolutionImage.getResolutionVariant( IMAGE_WIDTH, IMAGE_HEIGHT); Image image2x = multiResolutionImage.getResolutionVariant( 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT); if (image1x.getWidth(null) * 2 != image2x.getWidth(null) || image1x.getHeight(null) * 2 != image2x.getHeight(null)) { throw new RuntimeException("Wrong resolution variant size"); } }
private Image getResolutionVariant(MultiResolutionImage img, int srcWidth, int srcHeight, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, AffineTransform xform) { if (srcWidth <= 0 || srcHeight <= 0) { return null; } int sw = sx2 - sx1; int sh = sy2 - sy1; if (sw == 0 || sh == 0) { return null; } AffineTransform tx; if (xform == null) { tx = transform; } else { tx = new AffineTransform(transform); tx.concatenate(xform); } int type = tx.getType(); int dw = dx2 - dx1; int dh = dy2 - dy1; double destImageWidth; double destImageHeight; if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_BASE) { destImageWidth = srcWidth; destImageHeight = srcHeight; } else if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_DPI_FIT) { AffineTransform configTransform = getDefaultTransform(); if (configTransform.isIdentity()) { destImageWidth = srcWidth; destImageHeight = srcHeight; } else { destImageWidth = srcWidth * configTransform.getScaleX(); destImageHeight = srcHeight * configTransform.getScaleY(); } } else { double destRegionWidth; double destRegionHeight; if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP)) == 0) { destRegionWidth = dw; destRegionHeight = dh; } else if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP | TYPE_MASK_SCALE)) == 0) { destRegionWidth = dw * tx.getScaleX(); destRegionHeight = dh * tx.getScaleY(); } else { destRegionWidth = dw * Math.hypot( tx.getScaleX(), tx.getShearY()); destRegionHeight = dh * Math.hypot( tx.getShearX(), tx.getScaleY()); } destImageWidth = Math.abs(srcWidth * destRegionWidth / sw); destImageHeight = Math.abs(srcHeight * destRegionHeight / sh); } Image resolutionVariant = img.getResolutionVariant(destImageWidth, destImageHeight); if (resolutionVariant instanceof ToolkitImage && ((ToolkitImage) resolutionVariant).hasError()) { return null; } return resolutionVariant; }
private Image getResolutionVariant(MultiResolutionImage img, int srcWidth, int srcHeight, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, AffineTransform xform) { if (srcWidth <= 0 || srcHeight <= 0) { return null; } int sw = sx2 - sx1; int sh = sy2 - sy1; if (sw == 0 || sh == 0) { return null; } AffineTransform tx; if (xform == null) { tx = transform; } else { tx = new AffineTransform(transform); tx.concatenate(xform); } int type = tx.getType(); int dw = dx2 - dx1; int dh = dy2 - dy1; double destImageWidth; double destImageHeight; if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_BASE) { destImageWidth = srcWidth; destImageHeight = srcHeight; } else if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_DPI_FIT) { AffineTransform configTransform = getDefaultTransform(); if (configTransform.isIdentity()) { destImageWidth = srcWidth; destImageHeight = srcHeight; } else { destImageWidth = srcWidth * configTransform.getScaleX(); destImageHeight = srcHeight * configTransform.getScaleY(); } } else { double destRegionWidth; double destRegionHeight; if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP)) == 0) { destRegionWidth = dw; destRegionHeight = dh; } else if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP | TYPE_MASK_SCALE)) == 0) { destRegionWidth = dw * transform.getScaleX(); destRegionHeight = dh * transform.getScaleY(); } else { destRegionWidth = dw * Math.hypot( transform.getScaleX(), transform.getShearY()); destRegionHeight = dh * Math.hypot( transform.getShearX(), transform.getScaleY()); } destImageWidth = Math.abs(srcWidth * destRegionWidth / sw); destImageHeight = Math.abs(srcHeight * destRegionHeight / sh); } Image resolutionVariant = img.getResolutionVariant(destImageWidth, destImageHeight); if (resolutionVariant instanceof ToolkitImage && ((ToolkitImage) resolutionVariant).hasError()) { return null; } return resolutionVariant; }
/** * Creates an image containing pixels read from the screen. * This image does not include the mouse cursor. * This method can be used in case there is a scaling transform * from user space to screen (device) space. * Typically this means that the display is a high resolution screen, * although strictly it means any case in which there is such a transform. * Returns a {@link java.awt.image.MultiResolutionImage}. * <p> * For a non-scaled display, the {@code MultiResolutionImage} * will have one image variant: * <ul> * <li> Base Image with user specified size. * </ul> * <p> * For a high resolution display where there is a scaling transform, * the {@code MultiResolutionImage} will have two image variants: * <ul> * <li> Base Image with user specified size. This is scaled from the screen. * <li> Native device resolution image with device size pixels. * </ul> * <p> * Example: * <pre>{@code * Image nativeResImage; * MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds()); * List<Image> resolutionVariants = mrImage.getResolutionVariants(); * if (resolutionVariants.size() > 1) { * nativeResImage = resolutionVariants.get(1); * } else { * nativeResImage = resolutionVariants.get(0); * } * }</pre> * @param screenRect Rect to capture in screen coordinates * @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 * * @since 9 */ public synchronized MultiResolutionImage createMultiResolutionScreenCapture(Rectangle screenRect) { return new BaseMultiResolutionImage( createCompatibleImage(screenRect, true)); }