Added support for SVG data URI images

pull/215/head
M66B 5 months ago
parent 7865c2317f
commit 42a17c3a3d

@ -70,6 +70,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
@ -662,6 +663,27 @@ class ImageHelper {
if (comma < 0) if (comma < 0)
throw new IllegalArgumentException("Comma missing"); throw new IllegalArgumentException("Comma missing");
int colon = source.indexOf(':');
int semi = source.indexOf(';');
String type = null;
if (colon > 0 && semi > colon)
type = source.substring(colon + 1, semi);
else if (colon > 0 && comma > colon)
type = source.substring(colon + 1, comma);
String enc = (semi > 0 && comma > semi ? source.substring(semi + 1, comma) : null);
if ("image/svg".equalsIgnoreCase(type) &&
(enc == null || "utf8".equalsIgnoreCase(enc)))
try {
InputStream is = new ByteArrayInputStream(source.substring(comma + 1).getBytes(StandardCharsets.UTF_8));
Bitmap bm = ImageHelper.renderSvg(is, Color.WHITE, 1024);
Helper.ByteArrayInOutStream s = new Helper.ByteArrayInOutStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, s);
return s.getInputStream();
} catch (IOException ex) {
throw new IllegalArgumentException("SVG", ex);
}
String base64 = source.substring(comma + 1); String base64 = source.substring(comma + 1);
byte[] bytes = Base64.decode(base64.getBytes(), 0); byte[] bytes = Base64.decode(base64.getBytes(), 0);
return new ByteArrayInputStream(bytes); return new ByteArrayInputStream(bytes);

Loading…
Cancel
Save