java - 将Java中的二维数组转换为图像

标签 java image image-processing

我需要将灰度图像的二维像素强度数据数组转换回图像。我试过这个:

BufferedImage img = new BufferedImage(
    regen.length, regen[0].length, BufferedImage.TYPE_BYTE_GRAY);  
for(int x = 0; x < regen.length; x++){
    for(int y = 0; y<regen[x].length; y++){
        img.setRGB(x, y, (int)Math.round(regen[x][y]));
    }
}
File imageFile = new File("D:\\img\\conv.bmp");
ImageIO.write(img, "bmp", imageFile);

其中“regen”是一个二维 double 组。我得到一个相似但不准确的输出。很少有像素与它必须的完全相反(对于值为 255 的像素,我得到黑色)。少数灰色阴影也被视为白色。你能告诉我我做错了什么吗?

最佳答案

试试这样的代码:

public void writeImage(int Name) {
    String path = "res/world/PNGLevel_" + Name + ".png";
    BufferedImage image = new BufferedImage(color.length, color[0].length, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < 200; x++) {
        for (int y = 0; y < 200; y++) {
            image.setRGB(x, y, color[x][y]);
        }
    }

    File ImageFile = new File(path);
    try {
        ImageIO.write(image, "png", ImageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

关于java - 将Java中的二维数组转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767471/

相关文章:

java 用附加方法实现接口(interface)

javascript - 从 Android 应用程序运行 tor.so "net::ERR_NAME_NOT_RESOLVED"

java - 调整大小时,JFrame 在 Windows 8.1 上卡住

java - 如何在Java中为正则表达式转义文本

css - 我想将图像放在 Wordpress 标题中的特色内容 slider 的顶部,以便在 slider 在其下方淡入淡出时保持固定

jquery - JQuery/CSS 如何获取对象的旋转值

opencv - 获取主导色opencv

java - SVD、奇异值分解后矩阵值增加

c++ - Opencv hough圆圈未检测到圆圈

c++ - 具有 PBO 支持的高级 OpenGL 库