【问题标题】:JavaFX PixelReader Alpha-Channel not workingJavaFX PixelReader Alpha 通道不起作用
【发布时间】:2017-09-27 09:47:41
【问题描述】:
public class Loader {

    public static byte[] loadImage(String path) {
        Image image;
        try {
            image = new Image(new FileInputStream(path));
            int width = (int) image.getWidth();
            int height = (int) image.getHeight();
            byte[] data = new byte[width * height * 4];
            image.getPixelReader().getPixels(0, 0, width, height, PixelFormat.getByteBgraPreInstance(), data, 0, width * 4);
            return data;
        }catch(IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return null;
    }

}


@Override
    public void render(GraphicsContext gc) {
        gc.clearRect(0, 0, width, height);
        gc.getPixelWriter().setPixels(x++, 0, 819 ,720, PixelFormat.getByteBgraPreInstance(), data, 0, 819*4);
        gc.getPixelWriter().setPixels(400, 0,819 ,720, PixelFormat.getByteBgraPreInstance(), data, 0, 819*4);
    }

您好,目前我在使用 JavaFx 的 PixelWriter/PixelReader 时遇到问题。我尝试从图像中读取像素并将其存储在缓冲区中,之后我想将其渲染到屏幕上。但是图像现在不包含 Alpha 值,因此也没有透明像素。我在互联网上搜索了几个小时,但找不到答案。可能格式有问题。 提前致谢。

【问题讨论】:

  • 格式很可能有问题。在Image 类支持的 4 种格式中,只有两种具有定义透明度的机制 - GIF,可能具有透明颜色(因此 alpha=1 或 alpha=0),以及可能包含完整 alpha 通道的 PNG。如果您正在阅读 BMP 或 JPG 文件,您将没有 alpha 值。即使在读取 PNG 文件时,如果原始文件没有 alpha 通道,您也不会看到不同像素的 alpha 有任何变化。
  • 已经检查了 PNG 文件,它有一个 Alpha 通道。忘了说我只知道阅读带有 Alpha 通道的图像。但无论如何谢谢。

标签: java javafx rendering pixel pixels


【解决方案1】:

我没有尝试过(因为您没有提供完整且可执行的示例),但我认为您的问题是您使用带有预乘 alpha 的像素格式。使用不带预乘的简单 rgb 格式之一重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多