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
Trusted Contributor
rrr_04
Posts: 177
Registered: ‎03-02-2010
My Device: Not Specified

base64Binary decoding

Hi,

 

I have base64binary encoded string and now I want to convert it in bitmap by getting bytes from encoded string but I am getting "Electroencephalographic" exception. I tried by following way but got same result.

 

String ss = "c9f7be7e428c023ecaa192b50be1ff7fd52d1991";

 

1. byte[] c =Base64InputStream.decode(ss);    
    EncodedImage en = EncodedImage.createEncodedImage(c, 0, c.length);// here throw exception.

 

2

 EncodedImage en = EncodedImage.createEncodedImage(ss.getBytes(), 0, ss.getBytes().length);// here throw exception.

 

 

Please help me how to convert above encoded string in Bitmap or EncodedImage.

 

Thanks

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: base64Binary decoding

You need to convert back to Binary first.  I suspect you will find

Base64InputStream

and

ByteArrayOutputStream

useful in doing this.

Please use plain text.
Trusted Contributor
rrr_04
Posts: 177
Registered: ‎03-02-2010
My Device: Not Specified

Re: base64Binary decoding

Hi, Could you please explain it little bit more. How I can convert back it to binary. Thank you
Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: base64Binary decoding

I suspect Wikipedia can explain the process much better than I could...

 

http://en.wikipedia.org/wiki/Base64

 

If you review this and the various API's I think what you need to do will become clear.

Please use plain text.
Developer
rizinbox
Posts: 139
Registered: ‎10-24-2010
My Device: BlackBerry 10 Dev Alpha
My Carrier: du

Re: base64Binary decoding

 

Read about algorithm from here

 

http://www.herongyang.com/encoding/Base64-Encoding-Algorithm.html

http://www.kbcafe.com/articles/HowTo.Base64.pdf

 

 

And this is the code of Base64

 

 

import java.io.*;

public class Base64 {

    static final char[] charTab = 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray (); 


    public static String encode (byte [] data) {
        return encode (data, 0, data.length, null).toString ();
    }


    
    public static StringBuffer encode (byte [] data, int start, int len, StringBuffer buf) {

        if (buf == null) 
            buf = new StringBuffer (data.length * 3 / 2);

        int end = len - 3;
        int i = start;
        int n = 0;

        while (i <= end) {
            int d = ((( data [i]) & 0x0ff) << 16) 
                | ((( data [i+1]) & 0x0ff) << 8)
                | (( data [i+2]) & 0x0ff);

            buf.append (charTab [(d >> 18) & 63]);
            buf.append (charTab [(d >> 12) & 63]);
            buf.append (charTab [(d >> 6) & 63]);
            buf.append (charTab [d & 63]);

            i += 3;

            if (n++ >= 14) {
                n = 0;
                buf.append ("\r\n");
            }
        }


        if (i == start + len - 2) {
            int d = ((( data [i]) & 0x0ff) << 16) 
                | ((( data [i+1]) & 255) << 8);

            buf.append (charTab [(d >> 18) & 63]);
            buf.append (charTab [(d >> 12) & 63]);
            buf.append (charTab [(d >> 6) & 63]);
            buf.append ("=");
        }
        else if (i == start + len - 1) {
            int d = (( data [i]) & 0x0ff) << 16;

            buf.append (charTab [(d >> 18) & 63]);
            buf.append (charTab [(d >> 12) & 63]);
            buf.append ("==");
        }

        return buf;
    }


    static int decode (char c) {
        if (c >= 'A' && c <= 'Z') 
            return ( c) - 65;
        else if (c >= 'a' && c <= 'z') 
            return ( c) - 97 + 26;
        else if (c >= '0' && c <= '9')
            return (c) - 48 + 26 + 26;
        else switch (c) {
        case '+': return 62;
        case '/': return 63;
        case '=': return 0;
        default:
            throw new RuntimeException (new StringBuffer("unexpected code: ").append(c).toString());
        }
    }
                

  
    public static byte [] decode (String s) {

        int i = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream ();
        int len = s.length ();
        
        while (true) { 
            while (i < len && s.charAt (i) <= ' ') i++;

            if (i == len) break;

            int tri = (decode (s.charAt (i)) << 18)
                + (decode (s.charAt (i+1)) << 12)
                + (decode (s.charAt (i+2)) << 6)
                + (decode (s.charAt (i+3)));
            
            bos.write ((tri >> 16) & 255);
            if (s.charAt (i+2) == '=') break;
            bos.write ((tri >> 8) & 255);
            if (s.charAt (i+3) == '=') break;
            bos.write (tri & 255);

            i += 4;
        }
        return bos.toByteArray ();
    }
}

 

 

Hope this helps to you..

 

if your problem solve then mark as solved

 

--
please mark posts as solved if you found a solution.
press the like button on the right side to thank the user that helped you.
------------------------------------------------------------
Riz
Please use plain text.
Developer
paulkayuk
Posts: 133
Registered: ‎04-30-2010
My Device: Curve 8520
My Carrier: T-Mobile

Re: base64Binary decoding

This is how I do it:

 

byte[] b = YourBase64EncodedString.getBytes();

String s = decodeBase64(b, 0, b.length);

byte[] _raw = s.getBytes();

Bitmap _image = Bitmap.createBitmapFromBytes( _raw, 0, -1, 1 );

 

// Decodes the given byte array from Base64. 

// Offset and length specify which part of the array to decode. 

// The decoded data is returned as a String.

public static String decodeBase64( byte[] toDecode, int offset, int length )

{

// Create the input byte array to pass in the content.

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( toDecode, offset, length );

 

// Create the base64 input stream.

Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream);

 

StringBuffer sb = new StringBuffer();

int read;

try 

{

// Call read to decode the base64 content.

while ((read = base64InputStream.read()) >= 0) 

{

sb.append( (char)read );

}

 

base64InputStream.close();

} 

catch (IOException ioe) 

{

return null; 

}

 

return sb.toString();

}

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: base64Binary decoding

The code supplied in the preceding post will not work for image data.  It relies on being able to convert each byte into a character, and of course the binary data is NOT characters.  So please process the Base 64 data into a byte array, and then use the byte array to create your image. 

Please use plain text.
Developer
paulkayuk
Posts: 133
Registered: ‎04-30-2010
My Device: Curve 8520
My Carrier: T-Mobile

Re: base64Binary decoding

Well, the code I provided works in a live application I have under ongoing use and development.

 

I take a picture on the device, and encode it into a base 64 string for embedding in xml to pass to the application server side via a web-service.  I then retrieve the base 64 string embedded in xml via a second web-service and decode it as per my sample code for displaying as an image on the device.

 

Maybe you need both (matching encoding/decoding) sides of the process for it to work, but work it does.

Please use plain text.
Developer
rizinbox
Posts: 139
Registered: ‎10-24-2010
My Device: BlackBerry 10 Dev Alpha
My Carrier: du

Re: base64Binary decoding

paulkayuk.

 

but when you send xml on server via webservice is there you face any file size issue. 

 

mean if image size will 1 mb then can we send it to server after converting it in xml. 

 

or we need to divide it in chunks..

 

--
please mark posts as solved if you found a solution.
press the like button on the right side to thank the user that helped you.
------------------------------------------------------------
Riz
Please use plain text.
Developer
paulkayuk
Posts: 133
Registered: ‎04-30-2010
My Device: Curve 8520
My Carrier: T-Mobile

Re: base64Binary decoding

Different question to that of the OP, but the usual would apply, if you hit a limitation with the size of a data item in your Web Service based upload / download then, yes, construct your web service methods so that you can upload / download large data items in chunks and re-construct them at the receiving end.

Please use plain text.