从具有颜色强度的双数组创建图像

问题描述 投票:0回答:1

我有一个带有图像颜色强度值的二维双数组,看起来与此类似(我的数组大小为256x256,填充了以下值:]

790.0 739.0 690.0 601.0 582.0 630.0 730.0 773.0
982.0 879.0 754.0 695.0 687.0 631.0 630.0 666.0
1046.0 1080.0 1070.0 990.0 872.0 730.0 647.0 657.0
1008.0 998.0 962.0 959.0 944.0 930.0 921.0 932.0

是否可以从该文件创建图像对象?

我当前的代码:

    Double imageLayer[][] = vtkLayers.get(layer);

    int xLenght = imageLayer.length;
    int yLength = imageLayer[0].length;

    System.out.println(xLenght);
    System.out.println(yLength);

    BufferedImage b = new BufferedImage(xLenght, yLength, 3);

    for(int x = 0; x < xLenght; x++) {
        for(int y = 0; y < yLength; y++) {
            int rgb = (imageLayer[x][y]).intValue() << 16 | (imageLayer[x][y]).intValue() << 8 | (imageLayer[x][y]).intValue();
            b.setRGB(x, y, rgb);
        }
    }
    try {
        File outputfile = new File("C:\\temp\\image.png");
        ImageIO.write(b, "png", outputfile);
    }
    catch (IOException e){
        System.out.println("Could not create picture");
    }

为了对其进行测试,我试图创建一个png文件。但是此代码当前仅产生一个空白的png文件。由于我是Java新手,因此我以this帖子为指南。

如果我可以直接创建Image对象而不先创建png,那将是最好的。

java double bufferedimage
1个回答
0
投票

我可以自己解决问题:

© www.soinside.com 2019 - 2024. All rights reserved.