【问题标题】:PNG with transparent background turns black when resized with Scalr使用 Scale 调整大小时,具有透明背景的 PNG 变黑
【发布时间】:2012-08-30 03:43:16
【问题描述】:

我正在使用 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);
}

有人知道这件事吗?

谢谢,阿布舍克

【问题讨论】:

    标签: java image-processing scalr imgscalr


    【解决方案1】:

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

    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 值与颜色匹配为透明。

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2014-06-03
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 2020-12-20
        • 2011-08-07
        • 2012-10-15
        • 1970-01-01
        相关资源
        最近更新 更多