【问题标题】:Drawing a dynamic transparent image over another Image using SWT Graphics使用 SWT Graphics 在另一个图像上绘制动态透明图像
【发布时间】:2019-03-27 18:21:22
【问题描述】:

我正在尝试在已就位的图像顶部绘制一个由组合形状组成的透明蒙版。我通过本文中包含的对话代码提供了一个我正在尝试的技术示例。这是它产生的屏幕截图。

示例 1(左上角)突出显示了我要解决的问题,我希望有 2 个圆圈或任何相交的形状/弧线,都以相同级别的 alpha 绘制在一起,即没有由绘制引起的复合不透明性互相顶。

示例 3(左下角)是我尝试通过创建一个带有实心形状的单独图像来解决该问题,然后使整个图像透明,我认为使用这种技术会产生一个处理白色的图像作为透明色,所以圆的边缘与白色混合,这样当你在上面绘制它时,它会在形状周围产生“光晕”效果。

示例 2(左上角)通过将图像中的圆圈也绘制为透明来进一步突出显示此问题,因此您可以看到由突出显示引起的更多粉红色。

我的问题是,在不了解背景颜色的情况下,在不关闭抗锯齿的情况下,我怎样才能达到我想要的效果?有没有办法,因为我所有的研究都是空白的?也许我需要使用不同的图像绘制解决方案并移植回 SWT?我知道如果直接从文件加载,它能够绘制透明图像,所以我知道它可以保存这种数据,但我该如何创建它呢?

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FMLDialog extends Dialog
{   
    private Color red;
    private Color blue;

    public FMLDialog(Shell parentShell)
    {
        super(parentShell);
    }



    @Override
    protected void configureShell(Shell shell)
    {
        red = new Color(shell.getDisplay(), new RGB(255,0,0));
        blue = new Color(shell.getDisplay(), new RGB(0,100,255));
        super.configureShell(shell);
        shell.setSize(new Point(450,550));
        shell.setText("FML");
    }   

    @Override
    public Control createDialogArea(final Composite comp)
    {

        Composite content = (Composite) super.createDialogArea(comp);
        Composite parent = new Composite(content, SWT.NONE);        

        GridLayout gridLayout2 = new GridLayout(1, false);
        parent.setLayout(gridLayout2);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));           

        final Canvas c = new Canvas(parent, SWT.BORDER);
        c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        c.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                e.gc.setAntialias(SWT.ON);
                drawFirstLayer(e.gc, 0, 0);
                drawFirstLayer(e.gc, 210, 0);
                drawFirstLayer(e.gc, 210, 210);
                drawFirstLayer(e.gc, 0, 210);

                drawSecondLayerTake1(e.gc, 0, 0);
                drawSecondLayerTake2(e.gc, 210, 0);
                drawSecondLayerTake3(e.gc, 0, 210);
                drawSecondLayerTake4(e.gc, 210, 210); 
            }
        });

        return content;     

    }

    private void drawFirstLayer(GC gc, int x, int y) {
        gc.setBackground(blue);
        gc.fillOval(x, y, 200 , 200);
    }

    private void drawSecondLayerTake1(GC gc, int x, int y) {
        // Simply draw 2 transparent circles
        // Issue here is the overlap between circles where the Alpha layers up
        gc.setAlpha(100);
        gc.setBackground(red);
        gc.fillOval(x + 70, y + 70, 60 , 60);
        gc.fillOval(x + 100, y + 100, 60 , 60);
        gc.setAlpha(255);
    }

    private void drawSecondLayerTake2(GC gc, int x, int y) {
        // Create an image with 2 transparent circles
        // Issue here is the overlap between circles where the Alpha layers up from the first
        // PLUS becasue my transparent colour is fixed to white the alpa on the circles is blended in to the white
        final Image src = new Image(null, 300, 300);
        final ImageData imageData = src.getImageData();
        imageData.transparentPixel = imageData.getPixel(0, 0);
        src.dispose();
        final Image processedImage = new Image(Display.getCurrent(), imageData);
        final GC imageGC = new GC(processedImage);      
        imageGC.setAntialias(SWT.ON);
        imageGC.setAlpha(100);
        imageGC.setBackground(red);
        imageGC.fillOval(70, 70, 60 , 60);
        imageGC.fillOval(100, 100, 60 , 60);
        imageGC.dispose();

        gc.drawImage(processedImage, x + 0, y + 0);

    }

    private void drawSecondLayerTake3(GC gc, int x, int y) {
        // Create an image with 2 solid circles, then draw that image on to the canvas with Alpha values.
        // Overlap issue goes away because the whole image is being made transparent together HOWEVER
        // there is a Halo effect around the edge of the red where the original circles were antialiased to blend into the "white"
        // background.

        final Image src = new Image(null, 300, 300);
        final ImageData imageData = src.getImageData();
        imageData.transparentPixel = imageData.getPixel(0, 0);
        src.dispose();
        final Image processedImage = new Image(Display.getCurrent(), imageData);
        final GC imageGC = new GC(processedImage);      
        imageGC.setAntialias(SWT.ON);
        imageGC.setBackground(red);
        imageGC.fillOval(70, 70, 60 , 60);
        imageGC.fillOval(100, 100, 60 , 60);
        imageGC.dispose();

        gc.setAlpha(100);
        gc.drawImage(processedImage, x + 0, y + 0);

    }

    private void drawSecondLayerTake4(GC gc, int x, int y) {
        // I need this one to draw like take 3 but without the white "halo" effect on the edge
        // How?!
    }

    public static void main(String[] args) {
        Display d = new Display();
        Shell s = new Shell();

        FMLDialog fml = new FMLDialog(s);
        fml.open();
    }

}

【问题讨论】:

    标签: java swt eclipse-rcp alpha-transparency


    【解决方案1】:

    我能够使用Sean Bright 描述的方法获得所需的结果:https://stackoverflow.com/a/15685473/6245535

    基本上:

    1. 我们创建一个图像src 并使用gc 填充透明颜色
    2. 我们用纯色画椭圆
    3. 我们得到结果图像数据:现在,图像的像素数据数组(imageData.data)也将包含 alpha 值,而图像的 alpha 数据数组(imageData.alphaData)是null
    4. 我们通过从imageData.data中提取正确位置的alpha值来手动修复imageData.alphaData;这部分假设我们正在使用 32 位颜色深度;否则将无法正常工作
    5. 现在imageDataalphaData 已修复,我们用它创建一个图像processedImage
    6. 使用gc,我们最终绘制了具有部分透明度的processedImage

    这是代码(这是 Sean 的代码,有一些更改):

    private void drawSecondLayerTake4(GC gc, int x, int y) {
    
        final int width = 300;
        final int height = 300;
    
        final Image src = new Image(null, width, height);
    
        final GC imageGC = new GC(src);
    
        imageGC.setAntialias(SWT.ON);
    
        // This sets the alpha on the entire canvas to transparent
        imageGC.setAlpha(0);
        imageGC.fillRectangle(0, 0, width, height);
    
        // Reset our alpha and draw the ovals
        imageGC.setAlpha(255);
        imageGC.setBackground(red);
        imageGC.fillOval(70, 70, 60, 60);
        imageGC.fillOval(100, 100, 60, 60);
    
        // We're done with the GC, so dispose of it
        imageGC.dispose();
    
        final ImageData imageData = src.getImageData();
        imageData.alphaData = new byte[width * height];
    
        // This is the hacky bit that is making assumptions about
        // the underlying ImageData.  In my case it is 32 bit data
        // so every 4th byte in the data array is the alpha for that
        // pixel...
        for (int idx = 0; idx < (width * height); idx++) {
            final int coord = (idx * 4) + 3;
            imageData.alphaData[idx] = imageData.data[coord];
        }
    
        // Now that we've set the alphaData, we can create our
        // final image
        final Image processedImage = new Image(Display.getCurrent(), imageData);
    
        gc.setAlpha(100);
        gc.drawImage(processedImage, x + 0, y + 0);
    
        // And get rid of the canvas
        src.dispose();
    
    }
    

    结果如下:

    【讨论】:

    • 你是我的新宠。
    • 我仍然觉得这不是很干净,边缘光晕效果仍然以某种形式存在,只是略有减弱?
    【解决方案2】:

    您可以使用Path 将两个圆圈合并为一个实体,然后用透明颜色填充它。

    这是一个比我的previous answer简单得多的解决方案,并且没有光环效应。

    代码:

    private void drawSecondLayerTake4(GC gc, int x, int y) {
    
        final Path path = new Path(Display.getCurrent());
        path.addArc(x + 70, y + 70, 60, 60, 0, 360);
        path.addArc(x + 100, y + 100, 60, 60, 0, 360);
    
        gc.setAlpha(100);
        gc.setBackground(red);
    
        // needed to avoid holes in the path
        gc.setFillRule(SWT.FILL_WINDING);
    
        gc.fillPath(path);
    
        path.dispose();
    }
    

    结果:

    【讨论】:

    • 这当然适用于最简单的解决方案,但问题比 2 圈场景大得多,我不得不简化它来问这个问题,但实际上我是从一个文件,然后制作一个更大的图像,其中包含文本和源图像。但对于其他有类似问题的人来说,这可能是一个很好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2022-09-08
    • 2021-06-30
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多