首页 > 解决方案 > 当我缩放图像时,我会在它上面得到一个红色层。并非所有图像都发生这种情况

问题描述

图像尺寸要求:

private static final int WIDTH = 640;
private static final int HEIGHT = 480;

缩放的主要方法:

    public byte[] getScaledCustomImageByteArray(byte[] imageBytes, int width, int height) throws IOException {
      if (imageBytes == null) {
        return null; //NOSONAR
      }
      try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
           ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes)) {
        BufferedImage before = ImageIO.read(byteArrayInputStream);
        if (before == null) {
          throw new ImageProcessingException("ByteArray is not an image!");
        }
        BufferedImage after = getScaledInstance(before, width, height);
        ImageIO.write(after, "png", byteArrayOutputStream);
        byteArrayOutputStream.flush();
        return byteArrayOutputStream.toByteArray();
      }
  }

这种方法可以正常工作,它可以保持比例。

    private Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
      int originalWidth = imgSize.width;
      int originalHeight = imgSize.height;
      int boundWidth = boundary.width;
      int boundHeight = boundary.height;
      int newWidth = originalWidth;
      int newHeight = originalHeight;
      if (originalWidth > boundWidth) {
        newWidth = boundWidth;
        newHeight = (newWidth * originalHeight) / originalWidth;
      }
      if (newHeight > boundHeight) {
        newHeight = boundHeight;
        newWidth = (newHeight * originalWidth) / originalHeight;
      }
      return new Dimension(newWidth, newHeight);
  }

这是我在调整图像大小时无法正确理解的问题。

    private BufferedImage getScaledInstance(BufferedImage image, int maxWidth, int maxHeight) {
      int width = image.getWidth();
      int height = image.getHeight();
    // if the image is already scaled, no further scaling is needed
      if (width <= maxWidth && height <= maxHeight) {
        return image;
      }

      int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : 
  BufferedImage.TYPE_INT_ARGB;
      BufferedImage result = image;
      BufferedImage scratchImage = null;
      Graphics2D graphics2D = null;
      int prevWidth = result.getWidth();
      int prevHeight = result.getHeight();
      Dimension targetDimensions = getScaledDimension(new Dimension(image.getWidth(), 
            image.getHeight()), new Dimension(maxWidth, maxHeight));
      int targetWidth = (int) targetDimensions.getWidth();
      int targetHeight = (int) targetDimensions.getHeight();
      do {
        if (width > targetWidth) {
          width /= 2;
          if (width < targetWidth) {
            width = targetWidth;
          }
        }

        if (height > targetHeight) {
          height /= 2;
          if (height < targetHeight) {
            height = targetHeight;
          }
        }
        if (scratchImage == null) {
          scratchImage = new BufferedImage(width, height, type);
          graphics2D = scratchImage.createGraphics();
          graphics2D.setComposite(AlphaComposite.Clear);
          graphics2D.fillRect(0, 0, width, height);
        }
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.setComposite(AlphaComposite.Src);
        graphics2D.drawImage(result, 0, 0, width, height, 0, 0, prevWidth, prevHeight, null);
        prevWidth = width;
        prevHeight = height;
        result = scratchImage;
      } while (width != targetWidth || height != targetHeight);
      graphics2D.dispose();
      if (targetWidth != result.getWidth() || targetHeight != result.getHeight()) {
        scratchImage = new BufferedImage(targetWidth, targetHeight, type);
        graphics2D = scratchImage.createGraphics();
        graphics2D.setComposite(AlphaComposite.Clear);
        graphics2D.fillRect(0, 0, targetWidth, targetHeight);
        graphics2D.setComposite(AlphaComposite.Src);
        graphics2D.drawImage(result, 0, 0, null);
        graphics2D.dispose();
        result = scratchImage;
      }
      return result;
  }

Google width 1856pixels && hegiht 886pixel 我有这个问题 在此处输入图像描述 在此处输入图像描述

但是当图像宽度为 750 像素 && 高度为 497 像素时,一切看起来都不错 在此处输入图像描述 在此处输入图像描述

遗留代码。我看不出问题出在哪里。

标签: javajpegbufferedimagejavax.imageioimage-conversion

解决方案


推荐阅读