我在透明度方面遇到一些问题。
我正在使用 https://stackoverflow.com/a/27453793 中提供的代码片段,但是在 Windows 上它可以工作,但在 Mac 上却不行。
经过进一步调查,不同的似乎是从 getPixel
返回的值。
以下例,使用我创建的 1x1 像素透明图像。
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
public class ImageTest {
public static void main(final String[] args) throws MalformedURLException, IOException {
try (InputStream stream = new URL("https://i.imgur.com/BFlkniE.png").openStream()) {
final Path path = Files.createTempFile(null, ".png");
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
final ImageDescriptor element = ImageDescriptor.createFromFile(null, path.toString());
final Image image = element.createImage();
final ImageData imageData = image.getImageData();
System.out.println(imageData.getPixel(0, 0));
}
}
}
在 Windows 上,输出为 0,在 Mac 上为 16711680。这是什么原因?
最佳答案
ImageData.getPixel
返回的值不一定是RGB值,它只是当前颜色表的值。您必须使用ImageData
中的PaletteData
来获取RGB值:
RGB rgb = imageData.palette.getRGB(imageData.getPixel(0, 0));
关于java - Eclipse SWT ImageData getPixel 在 Windows 和 Mac OSX 上返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60939789/