SWT透明像素变为白色

问题描述 投票:1回答:1

我正在尝试创建一个透明图像,然后在其上绘制包含透明/半透明像素的另一个图像。在添加图像之后,与颜色和α的混合相比,半透明像素被设置为白色。这导致图像周围出现白色轮廓。

图像必须作为具有透明像素的ToolItem显示在ToolBar上。

是否可以在不将透明像素更改为白色的情况下绘制具有透明度的图像?

叠加图像

Overlay Image

处理过的图像

Processed Image

final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(285,305);

// Create a transparent image
final Image transparentImage = new Image(null, 256, 256);
final ImageData imageData = transparentImage.getImageData();
imageData.transparentPixel = imageData.getPixel(0, 0);
transparentImage.dispose();

// Create an image with the transparent data
final Image processedImage = new Image(Display.getCurrent(),
        imageData);

// Get the image to draw onto the transparent image
final Image overlayImage = new Image(display, "overlay_image.png");

final GC imageGC = new GC(processedImage);

imageGC.setAntialias(SWT.ON);

imageGC.drawImage(overlayImage, 0, 0);

imageGC.dispose();

// Create the components and add the image
final ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);

final ToolItem item = new ToolItem(toolBar, SWT.NONE);
item.setImage(processedImage);

toolBar.pack();
shell.open();
while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}

overlayImage.dispose();
display.dispose();
java image swt
1个回答
0
投票

问题出在你使用的图像上,它有一个透明的白色,所以如果你想使用那个图像,或者你改变了用程序(GIMP?)操纵图像的透明色,或者你可以给出相同的透明色两个图像,所以首先从位置(0,0)的图像“overlay_image.png”获取透明色,并且只改变为第二个(透明)图像设置相同的透明色。这是您的图像的工作代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class TestTransparency {


    public static void main(String[] args) {


        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setSize(285,305);


        final Image overlayImage = new Image(display, "overlay_image.png");
        final ImageData imageDataOverlay = overlayImage.getImageData();
        imageDataOverlay.transparentPixel = imageDataOverlay.transparentPixel;


        // Create a transparent image
        final Image transparentImage = new Image(display, 800, 600);
        final ImageData imageData = transparentImage.getImageData();
        imageData.transparentPixel = imageDataOverlay.transparentPixel;
        transparentImage.dispose();
        final Image processedImage = new Image(Display.getCurrent(),
                imageData);



        final Image overlayImage2 = new Image(Display.getCurrent(),
                imageDataOverlay);
        final GC imageGC = new GC(processedImage);
        imageGC.setAntialias(SWT.ON);
        imageGC.drawImage(overlayImage2, 0, 0);
        imageGC.dispose();

        // Create the components and add the image
        final ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
        toolBar.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
        final ToolItem item = new ToolItem(toolBar, SWT.NONE);
        item.setImage(processedImage);

        toolBar.pack();
        shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }

        overlayImage.dispose();
        display.dispose();



    }

}

请注意,使用GIMP操作图像:

enter image description here

或使用像这样的良好透明图像:http://www.stickpng.com/img/icons-logos-emojis/tech-companies/youtube-play-logo您的代码工作正常!

© www.soinside.com 2019 - 2024. All rights reserved.