1

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();
   }
}
3
  • why not filewriter and bufferedwriter? any specific reason? Commented Aug 7, 2017 at 12:37
  • 2
    Your best bet would be to add a e.printStackTrace(); inside your catch(Exception e){} block and let us know what it prints out Commented Aug 7, 2017 at 12:37
  • 1
    To expand the above comment: Never ever have a Pokemon Catch (catches all exceptions) with an empty catch block. Never. Commented Aug 7, 2017 at 12:58

2 Answers 2

1

You must initialize your BufferedImage after initializing the width and height:

image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

The image type can vary but since the constructor's name is GrayScale, I thought you would want TYPE_BYTE_GRAY.

Sign up to request clarification or add additional context in comments.

1 Comment

For color which constructor used?
0

Working example:

public class GrayScale {
    int width;
    int height;
    BufferedImage  image;

    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[0].length;
          height = yourmatrix.length;

          image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

          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:\\Others\\grayscale.jpg");
          ImageIO.write(image, "jpg", ouptut);

       }
       catch (Exception e)
       {
       }
    }
}

Corrections:
1. Width and height components are rearranged
2. "image" object defined on the correct place

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.