code: public static BufferedImage resizeScreenshotIfNeeded(WebDriver driver, BufferedImage screenshotImage) {
          Double devicePixelRatio = 1.0;
  
          try {
              devicePixelRatio = ((Number) ((JavascriptExecutor) driver).executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
  
          if (devicePixelRatio > 1.0 && screenshotImage.getWidth() > 0) {
              Long screenSize = ((Number) ((JavascriptExecutor) driver).executeScript("return Math.max(" +
                              "document.body.scrollWidth, document.documentElement.scrollWidth," +
                              "document.body.offsetWidth, document.documentElement.offsetWidth," +
                              "document.body.clientWidth, document.documentElement.clientWidth);"
              )).longValue();
  
              Double estimatedPixelRatio = ((double)screenshotImage.getWidth()) / ((double)screenSize);
  
              if (estimatedPixelRatio > 1.0) {
  
                  int newWidth = (int) (screenshotImage.getWidth() / estimatedPixelRatio);
                  int newHeight = (int) (screenshotImage.getHeight() / estimatedPixelRatio);
  
                  Image tmp = screenshotImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
                  BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
  
                  Graphics2D g2d = scaledImage.createGraphics();
                  g2d.drawImage(tmp, 0, 0, null);
                  g2d.dispose();
  
                  return scaledImage;
              }
              else return screenshotImage;
          }
          else return screenshotImage;
      }
docs: Check the devicePixelRatio and adapts the size of the screenshot as if the ratio was 1.0
  @param driver
  @param screenshotImage
  @return