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
Highlighted
Regular Contributor
Posts: 69
Registered: ‎01-17-2010
My Device: 9700 Bold
My Carrier: Airtel

BASE64 Image type !!!

[ Edited ]

Hi Everyone,

 

I need to decode an image which is in BASE64 encoded format.

Can anybody suggest me how to decode this image?

 

thanks n regards

 

 

 

Hi buddies Once again !!!

 

please provided me any solution if you have.

Its a bit urgent

 

thanks n regards

AbhishekPXP
Regular Contributor
Posts: 69
Registered: ‎01-17-2010
My Device: 9700 Bold
My Carrier: Airtel

Re: BASE64 Image type !!!

No one's there to help me out.

 

Peter_Strange bhai atleast you....

AbhishekPXP
Regular Contributor
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700
My Carrier: Airtel

Re: BASE64 Image type !!!

Various open source are available and you can search for them. For your refernce i am attaching one some code:

 

import java.io.*;

public class Base64Decoder {

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

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

	public static byte[] decode(String s) {

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			decode(s, bos);
		} catch (IOException e) {
			throw new RuntimeException();
		}
		return bos.toByteArray();
	}

	public static void decode(String s, OutputStream os) throws IOException {
		int i = 0;

		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)));

			os.write((tri >> 16) & 255);
			if (s.charAt(i + 2) == '=')
				break;
			os.write((tri >> 8) & 255);
			if (s.charAt(i + 3) == '=')
				break;
			os.write(tri & 255);

			i += 4;
		}
	}
}

 

Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: BASE64 Image type !!!

Have you actually looked to try to find the answer to this yourself?  What options have you considered?  Have you asked the folks giving you this image how they create the Base64 encoding?  Do you know what Base64 encoding actually is and why people use it?  Have you looked at the Threads on this forum that talk about Base 64 encoded data?

 

I suspect if you had done some work yourself you would have solved this yourself.  Not wishing to cause offence, but this is actually a fairly simple problem.  If something is Base64 encoded, then you have to Base 64 decode it.  There are streaming classes that do this on the BlackBerry, if not, then there are various bits of code on the internet to help.

 

So short answer, base 64 decode back into binary.

 

Once you get to this point, you then have to convert the binary into an image.  This question is asked reasonably frequently on the forum.  I think you will find the answer if you search so I won't repeat the information here.

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: BASE64 Image type !!!

Did you look in the javadocs?

Base64InputStream and Base64OutputStream