Creating image from base64

1,515 views
Skip to first unread message

vmnikulin

unread,
Dec 25, 2011, 6:46:24 AM12/25/11
to Google Web Toolkit
Hi all!

How I can create Image from base64 string?

Thank in advance.

Ed

unread,
Dec 25, 2011, 8:02:45 AM12/25/11
to google-we...@googlegroups.com

vmnikulin

unread,
Dec 25, 2011, 11:46:09 AM12/25/11
to Google Web Toolkit
I need to decoding base64 string in GWT client code.

Now I use Image.setUrl(myImageServletUrl) to obtaining image from
server. But this method doesn't work if you want to get some
additional data with image in one server request. So I need create
class MyImage:

class MyImage implements Serializable {
    String base64Image;
    AddtionalData addtionalData;
}

And use it in GWT-RPC. And decode base64 string on client code.

How can I do it?


On Dec 25, 8:02 pm, Ed <post2edb...@gmail.com> wrote:
> I don't see what this has to do with GWT, but here it goes:
> Google is your friend:http://stackoverflow.com/questions/469695/decode-base64-data-in-javahttp://stackoverflow.com/questions/5441413/create-imageview-from-base...

vmnikulin

unread,
Dec 25, 2011, 2:58:09 PM12/25/11
to Google Web Toolkit
Also (and this is main question), how I can create Image from byte
array (or from base64) in GWT client code?
> > Google is your friend:http://stackoverflow.com/questions/469695/decode-base64-data-in-javah......

Mark

unread,
Dec 25, 2011, 7:19:20 PM12/25/11
to Google Web Toolkit
If you just want to display the image then just use what you have and
let the browser do the rest. Add an HTML object containing

<img src="data:<mime-type>;base64,<your base64 data>" width="X"
height="Y" alt="alternate text"/>

As shown here: http://www.websiteoptimization.com/speed/tweak/inline-images/

vmnikulin

unread,
Dec 26, 2011, 12:11:44 AM12/26/11
to Google Web Toolkit
Mark, your method doesn't work in IE8 because it has 32KB limitation
on data url size.
> > Thank in advance.- Скрыть цитируемый текст -
>
> - Показать цитируемый текст -

Ed

unread,
Dec 26, 2011, 6:54:12 AM12/26/11
to google-we...@googlegroups.com
As far as I know the GWT libs don't have an Base64 encode/decode code that you can use in the client.

But, there are people doing it.
With regards of Google:

Mark

unread,
Dec 26, 2011, 9:01:38 PM12/26/11
to Google Web Toolkit
The OP didn't say anything about a specific browser; this is the only
solution I know of for client-side handling of base64 data using GWT.
I would suggest that you make an HTML element containing an "<img
src=.../>" that points to a servlet that will stream the decoded data
back to you.
On Dec 26, 12:11 am, vmnikulin <vmniku...@gmail.com> wrote:
> Mark, your method doesn't work in IE8 because it has 32KB limitation
> on data url size.
>
> On 26 ÄÅË, 07:19, Mark <mdshol...@gmail.com> wrote:
>
>
>
>
>
>
>
> > If you just want to display the image then just use what you have and
> > let the browser do the rest. šAdd an HTML object containing
>
> > <img src="data:<mime-type>;base64,<your base64 data>" width="X"
> > height="Y" alt="alternate text"/>
>
> > As shown here: šhttp://www.websiteoptimization.com/speed/tweak/inline-images/
>
> > On Dec 25, 6:46šam, vmnikulin <vmniku...@gmail.com> wrote:
>
> > > Hi all!
>
> > > How I can create Image from base64 string?
>
> > > Thank in advance.- óËÒÙÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -
>
> > - ðÏËÁÚÁÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -

Jens

unread,
Dec 27, 2011, 8:41:34 AM12/27/11
to google-we...@googlegroups.com
If you want to support larger images (larger than 32kb) and/or IE6/IE7 you have to use your current approach:

class MyImage implements Serializable { 
    String imageUrl; //which will point to your image servlet
    AddtionalData addtionalData; 


And in my opinion there is nothing wrong with it. You fetch all the data you need in one GWT request and the browser then loads the image by its Url. That way you got the additional data faster and you can already display them while the image is still loading. Also using this approach the browser can cache the image if the URL for the image does not change. If you are trying to use base64 encoded images in a GWT response you always download the image unless you cache it yourself.

-- J.

vmnikulin

unread,
Dec 29, 2011, 4:16:19 AM12/29/11
to Google Web Toolkit
Thanks, Jens! It's a good decision in my opinion.
Reply all
Reply to author
Forward
0 new messages