views:

386

answers:

3

My image is passed into my program from a server and saved as a string. I want to convert that string into an image, and then in turn display that image within a label inside of a GridBagLayout. When I execute the below code, I get a blank frame. No exceptions during execution. When I look at the image object from the ToolKit in debug, it does say height and width are -1 (but the "imagedata" within the "source = ByteArrayImageSource" has byte[5144]). Any ideas?

Added Note: Image is stored in program as a String because the data is serialized in C# and is being deserialized within Java. This process apparently does not like byte[] in the deserialize process so I save it as a string and use getBytes when I want to use the image.

imageToDisplay = Toolkit.getDefaultToolkit().createImage(myString.getBytes());
ImageIcon logoIcon = new ImageIcon(imageToDisplay);
JLabel logolabel = new JLabel(logoIcon);
     mainPanel.add(logolabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
       GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
       new Insets(2, 2, 2, 2), 0, 0));
     mainFrame.add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
       GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
       new Insets(2, 2, 2, 2), 0, 0));
A: 

First you need to find out if the problem is the image or the layout. Do mmyers suggestion and place it a panel (possibly by itself) with a FlowLayout. If it still doesn't show up, it might the image. Is there a reason why the input is converted to a String? The default charset may not be able to handle the conversion cleanly (that is, bytes -> String -> bytes may not give you the same initial bytes).

Kathy Van Stone
I have checked trying to place an image from my file system into the label (just by placing the file path into the ImageIcon constructor) and it shows correctly in the label.
Please see added note as to why the label is stored as a String. But it looks like something is not converting as you thought bytes -> string -> bytes
You need to check how C# serializes it. What is the actual code you use to get the String? I would have expected C# to serialize as bytes.
Kathy Van Stone
A: 

you should always give an explicit encoding when converting a String to bytes, since the default value is platform dependent.

Using ISO-8859-1 will work in most cases, since that encoding maps the bytes 0 to 255 to the characters U+0000 to U+00FF.

Of course, you have to make sure that the data did not get mangled when you converted them to a String in C# (you can give an encoding there as well...).

mihi
Adding the encoding type of "UTF-8" into the getBytes method did not help. Even when I try outputting the image to a file using ImageIO it throws an exception saying height and width are -1.
My guess now is errors going from byte[] in C#...to String in Java...back to byte[] in Java
If you haven't yet, check the article "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)" (http://www.joelonsoftware.com/articles/Unicode.html).
Kathy Van Stone
A: 

Turns out this was my own fault. There is nothing wrong with the code I have written above other than I forgot to decode the image string. It is passed from the server Base64 encoded, and I was using getBytes on that encoded string and passing that into the "createImage" function. Thanks for your suggestions and help. The correct code is below:

try 
{
     imageToDisplay = Toolkit.getDefaultToolkit().createImage(Base64.decode(myString));
} catch (Exception e1) {
     //   // TODO Auto-generated catch block
     e1.printStackTrace();
}

ImageIcon logoIcon = new ImageIcon(imageToDisplay);
JLabel logolabel = new JLabel(logoIcon);
mainPanel.add(logolabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
    GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
    new Insets(2, 2, 2, 2), 0, 0));