Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8296848

[macos] Incorrect pixel color in AWT screen capture in Xcode 14.1 build

XMLWordPrintable

    • generic
    • os_x

      ADDITIONAL SYSTEM INFORMATION :

      macOS Monterey
      openjdk version "17.0.4.1" 2022-08-12
      OpenJDK Runtime Environment (build 17.0.4.1+1)
      OpenJDK 64-Bit Server VM (build 17.0.4.1+1, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      When building JDK17u with Xcode 14.1, the
      awt.Robot's following API causes incorrect pixel color in screen captured BufferedImage.

      * java.awt.Robot.createScreenCapture()
      * java.awt.Robot.createMultiResolutionScreenCapture()

      Screen capture by Robot in JavaFX works correctly.
      It can be reproduced on both x86_64 and aarch64.

      I note that this report is not a duplicate of JDK-8295779. The recently reported JDK-8296654 is also not a duplicate of JDK-8295779.

      The evaluator failed in the build step.
      They appear to be ignoring the reproduction steps.

      The JDK can be built with the following build options
      --disable-warnings-as-errors

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Build with Xcode 14.1
      2. Launch a test program
      3. Press the screenshot button

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The screenshot image appears in the application without pixel color issues.

      ACTUAL -
      Pixel color is incorrect.

      ---------- BEGIN SOURCE ----------
      import java.awt.AWTException;
      import java.awt.BorderLayout;
      import java.awt.EventQueue;
      import java.awt.Graphics;
      import java.awt.Rectangle;
      import java.awt.Robot;
      import java.awt.Toolkit;
      import java.awt.image.BufferedImage;
      import java.awt.image.MultiResolutionImage;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JToolBar;
      import javax.swing.WindowConstants;

      public class AWTScreenshotTest extends JFrame {

      static Robot robot;
      static {
      try {
      robot = new Robot();
      } catch (AWTException e) {
      e.printStackTrace();
      }
      }

      AWTScreenshotTest(){
              setLayout(new BorderLayout());
              setSize(700, 700);
              
      final JButton screenCapButtonWithAWT = new JButton("createScreenCapture");
      final JButton screenCapButtonWithAWT2 = new JButton("createMultiResolutionScreenCapture");
      JToolBar toolBar = new JToolBar();
      toolBar.add(screenCapButtonWithAWT);
      toolBar.add(screenCapButtonWithAWT2);
              Canvas canvas = new Canvas();
              add(canvas, BorderLayout.CENTER);
              add(toolBar, BorderLayout.SOUTH);

      screenCapButtonWithAWT.addActionListener(e->{
      Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
      final BufferedImage bimage = robot.createScreenCapture(rect);
      if (bimage != null) {
      canvas.bimage = bimage;
      canvas.repaint();
      }
      });
      screenCapButtonWithAWT2.addActionListener(e->{
      Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
      final MultiResolutionImage bimages = robot.createMultiResolutionScreenCapture(rect);
      if (bimages != null) {
      canvas.bimage = (BufferedImage) bimages.getResolutionVariants().get(0);
      canvas.repaint();
      }
      });
      }

      private static class Canvas extends JPanel{
      BufferedImage bimage;
      @Override
      protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if(this.bimage!=null) {
      int w = this.getWidth();
      double scale = (double)w/this.bimage.getWidth();
      g.drawImage(this.bimage, 0, 0, w, (int)(this.bimage.getHeight()*scale), null);
      }
      }
      }
      public static void main(String args[]) {
      EventQueue.invokeLater(()->{
      AWTScreenshotTest frame = new AWTScreenshotTest();
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      });
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            serb Sergey Bylokhov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: