JavaFX PixelReader Alpha-Channel无法正常工作

时间:2017-09-27 09:47:42

标签: java javafx rendering pixel pixels

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值,因此也没有透明的像素。我在互联网上搜索了几个小时,但我找不到答案。也许格式存在问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我还没有尝试过(因为你没有提供完整的可执行示例),但我认为你的问题是你使用带有预乘alpha的像素格式。使用其中一种简单的rgb格式再试一次,无需预乘。