I have a 2D array with dimensions [10][5] that I am trying to convert into an image. This is the code that I have tried, but it doesn't seem to be saving the image. What am I doing wrong?
public class GrayScale {
BufferedImage image;
int width;
int height;
public GrayScale() {
try {
int[][] yourmatrix = new int[][]{
{ 0, 1, 0, 0, 234, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 },
{ 0, 45, 0, 0, 0, 0, 0, 231, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 89, 0, 0, 0, 1 }
};
width = yourmatrix.length;
height = yourmatrix[0].length;
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
int u = yourmatrix[i][j];
image.setRGB(j,i,u);
}
}
File ouptut = new File("C:\\Users\\Pratik\\Desktop\\UPWORK\\JAVA\\grayscale.jpg");
ImageIO.write(image, "jpg", ouptut);
} catch (Exception e) {}
}
static public void main(String args[]) throws Exception{
GrayScale obj = new GrayScale();
}
}
e.printStackTrace();inside yourcatch(Exception e){}block and let us know what it prints out