生成的图像的的base64串在数据URI来使用 [英] Generate base64 string of an image to use in data URI
问题描述
如何生成的图像的的base64串在数据的URI使用?
How to generate base64 string of an image to use in data URI?
我有一个Base64编码图像编码的问题,我希望有人能帮忙带。我想在我的网页使用数据的URI(即< IMG SRC =数据:图像/ PNG; BASE64,iVBORw .../>
与 org.apache.commons。codec.binary.Base64
V1.8生成png图片的BASE64字符串。
I have an Base64 image encoding issue that I hope someone can help with. I'm trying to use data uri in my web page (ie <img src="data:image/png;base64,iVBORw..."/>
with org.apache.commons.codec.binary.Base64
v1.8 generating the base64 string of png image.
要生成我已经使用中的Base64字符串:
To generate the base64 string I have used:
Base64.encodeBase64URLSafeString(imageFile)
问题是浏览器无法呈现图像。我比较了一个工程这个生成的字符串,我注意到的差异是Apache的Base64编码的版本有_和 - 而不是/和+字。从我在网上看到有不同的Base64格式,所以我假设Apache的实现是不与浏览器兼容。
The problem is the browser cannot render the image. I compared this generated string with one that works and I noticed the differences are the Apache Base64 version has "_" and "-" characters instead of "/" and "+". From the internet I see there are different Base64 formats so I assume Apache's implementation is not compatible with the browsers.
所以我想知道有没有实现,将我的目的是适当的Base64格式的库?我目前的解决办法是只需更换字符,但我宁愿使用库。
So I was wondering is there a library that implements the base64 format that would be appropriate for my purposes? My current fix is to just replace the characters but I would rather use a library.
推荐答案
据的<一的Javadoc href=\"http://commons.apache.org/proper/commons-$c$cc/apidocs/org/apache/commons/$c$cc/binary/Base64.html#en$c$cBase64URLSafe%28byte%5B%5D%29\"相对=nofollow> Base64.en codeBase64URLSafeString 的方法,这似乎是由设计。看看我提供的链接,它在Javadoc中这样说:
According to the javadoc of the Base64.encodeBase64URLSafeString method, that seems to be by design. Check out the link I provided, it says this in the javadoc:
恩$ c。使用BASE64算法的安全网址的变化$ CS的二进制数据,但是不会大块输出。 的网址安全发射的变化 - 和_代替+和/字符。注意:没有填充添加
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.
所以你要确保你使用不安全的URL变化。这是调用的方法<一个href=\"http://commons.apache.org/proper/commons-$c$cc/apidocs/org/apache/commons/$c$cc/binary/Base64.html#en$c$cBase64%28byte%5B%5D%29\"相对=nofollow>恩codeBase64 。使用此方法来代替:
So you want to make sure you use the url-unsafe variation. That's the method called encodeBase64. Use this method instead:
Base64.encodeBase64(imageFile)
这篇关于生成的图像的的base64串在数据URI来使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!