CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Reading an array to display image

    Okay, I'm working on a fairly complicated (to my standards lol) project for coding that requires some things I need help with. First off, I have an image that was read into an array that the user has manipulated somehow - I know this works. However, I need to be able to display the image by reading from the array. What I would like to do is pass the array into a method that displays the image. Right now, I have this, which I essentially got online and modified for my needs;

    public void updateImage (int[][] arraySix)
    {
    InputStream imageArray = new ByteArrayInputStream(arraySix);
    BufferedImage imageUpdated = ImageIO.read(imageArray);
    }

    arraySix is a dummy array that is fixed to equal my array that holds the image information, whatever it may be. When I compile, I get an error saying that ByteArrayInputStream is an invalid variable name. I'm completely new to this sort of thing, so I don't even really know hwo to go about fixing it.

  2. #2
    Join Date
    Dec 2010
    Posts
    3

    Re: Reading an array to display image

    Sorry for the double post, but I got a lot further. My method works completely fine now, but the only problem is that somewhere in the code, it is setting all the pixels to 255 - essentially turning the entire image black. Here is what I have. For reference, "image" is the array that holds the image and arraySix is just a dummy array.

    public void updateImage (int[][] arraySix)
    {
    image=arraySix;
    int imageWidth = arraySix.length;
    int imageHeight = arraySix[0].length;

    BufferedImage showImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_USHORT_GRAY);

    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(showImage));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }


    I believe my problem is somewhere in "new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_USHORT_GRAY);", but I'm not sure.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Reading an array to display image

    It's not that something is turning the pixels black, the problem is you've haven't actually set the pixels to anything. You aren't using the arraySix values other than to compute the image size.

    Google for something like "java create image from array".
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Dec 2010
    Posts
    3

    Re: Reading an array to display image

    Oh, I'm sorry, I didn't explain very well. "image" is an array that has the picture in it. If it matters, here is my method that creates the image array;

    public int[][] readImageintoArray(String filename)
    {
    File f = new File(filename);
    BufferedImage readimage = null;
    try
    {
    readimage = ImageIO.read(f);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    int imageWidth = readimage.getWidth();
    int imageHeight = readimage.getHeight();

    int[][] GrayScaleData = new int[imageWidth][imageHeight];
    for (int i = 0; i < imageHeight; i++)
    {
    for (int j = 0; j < imageWidth; j++)
    {
    int pixel = readimage.getRGB(j, i);
    GrayScaleData[j][i] = (((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + ((pixel) & 0xff)) / 3;
    }
    }
    image=GrayScaleData;
    return GrayScaleData;
    }

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Reading an array to display image

    Please use code tags when posting code.

    "image" is an array that has the picture in it
    I realise that but where are you using its values?

    On another point all together why do you appear to have an attribute called 'image' that you set in the readImageintoArray() method and then set again in the updateImage() method?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured