java - java中如何将矩阵转换为图像

标签 java image matrix

我有一个矩阵 a[512][512],其中填充了 1 和 0,我如何将该矩阵转换为图像(.png、.jpg 等)。

最佳答案

这将从您创建的矩阵创建灰度图像:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

//Consider class and methods predefined. The following will be within a method

try {
    BufferedImage image;
    for(int i=0; i<yourmatrix.length; i++) {
        for(int j=0; j<yourmatrix[].length; j++) {
            int a = yourmatrix[i][j];
            Color newColor = new Color(a,a,a);
            image.setRGB(j,i,newColor.getRGB());
        }
    }
    File output = new File("GrayScale.jpg");
    ImageIO.write(image, "jpg", output);
}

catch(Exception e) {}

关于java - java中如何将矩阵转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088505/

相关文章:

java - JSch:有没有办法将用户环境变量暴露给 "exec" channel ?

java - Egit/Github - 从合作伙伴处获取文件

image - 如何在不知道扩展名的情况下在 <img> 标签中呈现 base-64 字符串

Javascript 函数可在单击一张图像时更改各种图像

C++在循环期间将 vector 存储在数组中

java - 在 VideoView 中播放视频

java - 在 Android 中将 RGBA 转换为 ARGB_8888

javascript - 图像变暗/不透明

opencv - 使用opencv旋转矩阵

java - 螺旋打印 mxn 矩阵 - java