将剪贴板设置为透明图像

将剪贴板设置为透明图像

本文介绍了将剪贴板设置为透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,该程序需要将图像复制到剪贴板。
问题是图像具有透明背景,每当我复制图像时,图像的背景都是黑色的而不是透明的。
自2天前以来,我尝试了很多事情,但没有任何效果。
类imageSelection基于

I am creating a program that needs to copy an image to clipboard.The problem is that the image has a transparent background and, whenever I copy it, the image comes out with a black background instead of transparent.I tried lots of things since 2 days ago but none worked.The class imageSelection is based on http://www.java2s.com/Tutorial/Java/0120__Development/SettinganimageontheclipboardwithacustomTransferableobjecttoholdtheimage.htm

package Package1;

import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

/** Transferable image */
public class imageSelection implements Transferable {
    private Image image;

    /** Creates a "transferable image" */
    public imageSelection(Image image) {
        this.image = image;
    }


    public DataFlavor[] getTransferDataFlavors() {
        DataFlavor[] transferData = new DataFlavor[] { DataFlavor.imageFlavor }; // <--- Works but gives me a black background instead of transparent

        /* I tried this (based on https://stackoverflow.com/questions/15977001/clipboard-copy-from-outlook-always-has-black-background-set-when-retrieved-as-im) but wasn't able to achieve any good result with it.

        DataFlavor transferData = null;
        try {
            transferData = new DataFlavor(Image.class, null); // <---- How to get an object of the type DataFlavor[] from this ( DataFlavor("image/x-emf") is of the type DataFlavor, not DataFlavor[] )
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            error.displayError(e.getStackTrace(), "Error creating DataFlavor (mime type: image/x-emf)");
        }

        return new DataFlavor[] { transferData }
        */

        return transferData;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return DataFlavor.imageFlavor.equals(flavor);
    }


    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return image;
    }
}

致电:

imageSelection imgSel = new imageSelection(new ImageIcon(emojiLocation).getImage());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);

谢谢

我正在测试内容通过将其粘贴到Discord(聊天应用程序,它确实支持透明性,我确定了这一点)。
我正在使用jdk1.8.0_131。
我正在使用Windows 10 64位完全更新。
如果需要,请在此处查看完整的源代码:
该程序的作用是将剪贴板更改为选定的图像,然后用AHK编写的程序将模拟

I am testing the contents by pasting it into Discord (chat app and it does support transparency, I made sure of that).I am using jdk1.8.0_131.I am using Windows 10 64bits fully updated.If needed, full source code here: https://github.com/KingOffNothing/EmojiMenu-for-discord/tree/master/src/Package1What the program does is change the clipboard to a selected image and then a program written in AHK will simulate the key press ctrl+v that pastes the image.

推荐答案

我无法完全解决透明度问题,但能够解决解决方法(更改透明像素以匹配背景颜色)

I was not able to exactly fix the transparency issue but was able to do a workaround (change transparent pixels to match the background's color)

private static BufferedImage fixTransparency(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        Color discordChatColor = new Color(54,57,62, 255);

        for (int xx = 0; xx < width; xx++) {
            for (int yy = 0; yy < height; yy++) {
                Color originalColor = new Color(image.getRGB(xx, yy), true);
                if (originalColor.getAlpha() == 0) {
                    image.setRGB(xx, yy, discordChatColor.getRGB());
                }
            }
        }
        return image;
    }

这篇关于将剪贴板设置为透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 16:13