3

我正在使用 org.imgscalr.Scalr(http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#solve) 库来调整图像的大小,但是具有透明背景的 PNG 在调整大小时会变黑。我的代码如下所述:

    public void scaleImage(String originalImage, int width, int height) throws IOException {
    File imgFl = new File(originalImage);
    BufferedImage originalBufImage = ImageIO.read(imgFl);
    BufferedImage scaledImage = Scalr.resize(originalBufImage, Scalr.Method.ULTRA_QUALITY, width, height, Scalr.OP_BRIGHTER);
    File outputfile = new File(originalImage);
    String fileExtension = originalImage.substring(originalImage.indexOf(".") + 1, originalImage.length());
    ImageIO.write(scaledImage, fileExtension, outputfile);
}

有人对此有任何线索吗?

谢谢, 阿布舍克

4

2 回答 2

2

我不知道为什么会这样,但假设图像中没有其他东西是黑色的,你可以用这个方法使黑色透明:

private static Image mct(Image im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
      // the color we are looking for... Alpha bits are set to opaque
      public int markerRGB = color.getRGB() | 0xFF000000;

      public final int filterRGB(int x, int y, int rgb) {
        if ( ( rgb | 0xFF000000 ) == markerRGB ) {
          // Mark the alpha bits as zero - transparent
          return 0x00FFFFFF & rgb;
          }
        else {
          // nothing to do
          return rgb;
          }
        }
      };

它返回一个图像,其所有 RGB 值与颜色匹配为透明。

我希望这会有所帮助。

于 2012-09-05T12:29:44.940 回答
1

在创建新的 BufferedImage 时,Scalr 可能没有使用 BufferedImage.TYPE_INT_ARGB - 也许你可以要求它?

于 2014-12-30T06:40:20.140 回答