Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
TeeC
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330
Accepted Solution

Decoding a byte back into an image

[ Edited ]

In my blackberry project I'm talking to a .net web service that is returning a person class. The person class consist of a firstname, lastname, and image file. In the service, I'm coverting the image to a base64 string; I'm configuring the image property as such:

 

byte[] toEncodedBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(pngFile);

person.imageByte = System.Convert.ToBase64String(toEncodedBytes);

 

The information is sent to the blackberry device successfully and is placed in a similar class. I compared the Base64 string in the webservice and device and they are the same. However, I'm not able to transform my png file back to it's original state. On the device I have the following code:

 

byte[] imageBytes = person.imageByte.getBytes("UTF-8");

 

BYTE[] bs = Base64InputStream.decode(imageBytes, 0, imageBytes.length);

 

EncodedImage myImage = EncodedImage.createEncodedImage(bs, 0, bs.length);

 

BitmapField bmf = new BitmapField();

bmf.setImage(myImage);

add(bmf)

 

When I run this I get a java.lang.nullpointer exception. I can print the object's values out just fine but I'm having an issue converting the base64 string back to an actual image and displaying it. Any help or suggestions would be wornderful and greatly appreciated.

Please use plain text.
Developer
TeeC
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330

Re: Decoding a byte back into an image

Ok, I figured it out. My issue with the service. In the service I was sending the path string. Duh! Anyhow, I corrected the issue by doing System.IO.File(path); I'm now getting my image over to the blackberry device from a web service using ksoap2. Thanks.

Please use plain text.
New Contributor
MikeEck
Posts: 2
Registered: ‎08-20-2010
My Device: 9659

Re: Decoding a byte back into an image

Let me ask- what did you do to populate your "pngFile" variable in your .net webservice code? I'm reading in the contents of the the png file I want like so:

 

using (FileStream fileStream = new FileStream(fileNameAndPathFileMode.OpenFileAccess.Read))
              {
                    BinaryReader binaryReader = new BinaryReader(fileStream;
                    fileAsString = binaryReader.ReadString();
                    binaryReader.Close();
                    fileStream.Close();
                }
byte[] toEncodedBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(fileAsString);
                 return System.Convert.ToBase64String(toEncodedBytes);

 

 

The complex type I'm passing back to the handset stores the png file as a String. Near as I can tell, it looks just like your code, but I get an IllegalArgumentException when I try and call createEncodedImage in the following (Image is a String that contains the png contents returned from the server):

 

 

byte[] imageBytes = Image.getBytes("UTF-8");
BYTE[] byteStream = Base64InputStream.decode(imageBytes, 0, imageBytes.length);
EncodedImage encodedImage = EncodedImage.createEncodedImage(byteStream, 0, byteStream.length, "image/png");

 

byte[] imageBytes = Image.getBytes("UTF-8");

 byte[] byteStream = Base64InputStream.decode(imageBytes, 0, imageBytes.length);

EncodedImage encodedImage = EncodedImage.createEncodedImage(byteStream, 0, byteStream.length, "image/png");

 

Thanks for any assistance or insights!

Please use plain text.
Developer
TeeC
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330

Re: Decoding a byte back into an image

[ Edited ]

Sure in my class I have

 

public class person implements KvmSerializable

{

    public String name;

    public String image;

public Object getProperty(int arg0)

{

    switch (arg0)

  {

      case 0:

         return name;

 

      case 1:

          return image;

   }

}

 

public getPropertyInfo(int index, Hashtable arg1, PropertyInfo info)

{

    switch 0:

     info.type = PropertyInfo.STRING_CLASS;

     info.name = "name";

 

    switch 1:

      info.type = MarshalBase64.BYTE_ARRAY_CLASS;

      info.name = "image";

}

 

public setProperty(int index, Object value)

{

      switch(index)

       {

           case 0: this.name = value.toString();

           break;

 

           case 1:

               this.image = value.toString();

                break;

       }

}

}

 

Once I populate my person object I pass it to another screen and decode the image and place it in a bitmap object using the below code. I hope that this helps.

 

byte[] imageByte = imageString.getBytes();

byte[] bs = Base64InputStream.decode(imageByte, 0, imageByte.length);

offenderImage = Bitmap.createBitmapFromBytes(bs, 0, bs.length, 3);

 

 

Please use plain text.
New Contributor
MikeEck
Posts: 2
Registered: ‎08-20-2010
My Device: 9659

Re: Decoding a byte back into an image

[ Edited ]

@TeeC - Dude, thanks a ton, that's exactly what I was looking for. Appreciate it!

 

Also, for all you .NET developers out there (like me) who've been spoiled and only developed homogeneously in C# before, and never had to convert a binary file to UTF-8 before, here's what you want to do in your C#:

 

FileInfo fi = new FileInfo(fileNameAndPath);                
byte[] myBinaryFile;

using (FileStream fileStream = new FileStream(fileNameAndPath, FileMode.Open, FileAccess.Read))
{
	BinaryReader binaryReader = new BinaryReader(fileStream);
	myBinaryFile = binaryReader.ReadBytes(System.Convert.ToInt32(fi.Length));
	binaryReader.Close();
}
return Convert.ToBase64String(myBinaryFile);

 

 

Please use plain text.