【问题标题】:How can I generate a transparent GIF in Java?如何在 Java 中生成透明的 GIF?
【发布时间】:2015-04-15 20:08:05
【问题描述】:

下面的代码会生成一个半红半蓝的 GIF 图像。 我怎样才能得到一个半红半透明的?

我尝试使用将透明像素索引作为参数的IndexColorModel 构造函数,并在对BufferedImage 构造函数的调用中将图像类型更改为IMAGE_TYPE_ARGB,但对我没有任何作用。

    int pixels[] = new int[90000];
    for (int x = 0; x < 300; x++) {
        for (int y = 0; y < 300; y++) {
            pixels[(300 * y) + x] = (x < y) ? 1 : 0;
        }
    }

    Color oneColor = Color.red;
    Color anotherColor = Color.blue;

    byte[] redMap = {(byte) (oneColor.getRed()), (byte) (anotherColor.getRed())};
    byte[] greenMap = {(byte) (oneColor.getGreen()), (byte) (anotherColor.getGreen())};
    byte[] blueMap = {(byte) (oneColor.getBlue()), (byte) (anotherColor.getBlue())};

    IndexColorModel colorModel = new IndexColorModel(1, 2, redMap, greenMap, blueMap);

    MemoryImageSource mis = new MemoryImageSource(300, 300, colorModel, pixels, 0, 300);

    Image image = Toolkit.getDefaultToolkit().createImage(mis);
    BufferedImage bufferedImage = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(image, 0, 0, null);

    try {
        ImageIO.write(bufferedImage, "gif", new File("example.gif"));
    } catch (IOException e) {
        e.printStackTrace();
    }

【问题讨论】:

    标签: java image gif


    【解决方案1】:

    根据我的经验,我使用 alpha 更改了颜色的透明度。例如,透明的红色看起来像这样:

    Color transparentred = new Color (255, 0, 0, alpha);
    

    也许尝试为您的 redMap、blueMap、greenMap 设置 alpha

    【讨论】:

      【解决方案2】:

      事实证明BufferedImage.TYPE_BYTE_INDEXED 更适合这种情况。这段代码可以解决问题:

          Color oneColor = Color.blue;
          Color anotherColor = Color.red;
      
          byte[] redMap = {(byte) (oneColor.getRed()), (byte) (anotherColor.getRed())};
          byte[] greenMap = {(byte) (oneColor.getGreen()), (byte) (anotherColor.getGreen())};
          byte[] blueMap = {(byte) (oneColor.getBlue()), (byte) (anotherColor.getBlue())};
      
          IndexColorModel colorModel = new IndexColorModel(1, 2, redMap, greenMap, blueMap, 0);
      
          int transparency = colorModel.getTransparency();
          int transparentPixel = colorModel.getTransparentPixel();
          System.out.println("colorModel.getTransparency(): " + transparency);
          System.out.println("colorModel.getTransparentPixel(): " + transparentPixel);
      
          BufferedImage bufferedImage = new BufferedImage(300, 300, BufferedImage.TYPE_BYTE_INDEXED, colorModel);
      
          WritableRaster writableRaster = bufferedImage.getRaster();
      
          for (int x = 0; x < 300; x++) {
              for (int y = 0; y < 300; y++) {
                  int[] fill = new int[1]; // A large block...
                  Arrays.fill(fill, (x < y) ? 0 : 1);  // ..  filled with one of the 7 first colors in the LUT.
                  writableRaster.setSamples(x, y, 1, 1, 0, fill);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2015-05-12
        • 2021-03-18
        • 2012-08-26
        • 2018-10-13
        • 2013-11-25
        • 1970-01-01
        相关资源
        最近更新 更多