X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2FStringUtils.java;h=cee9fb62715158ec6c2479ea01da4975a84c1159;hp=2009dce628ef48864d48cf51c31bab5e6eae9813;hb=26d254a3ac6cddbd3583cbbcbf8d43aa15c6a32e;hpb=aecb3399b756d2ba04223bc6f553999fce73f9fb diff --git a/src/be/nikiroo/jvcard/resources/StringUtils.java b/src/be/nikiroo/jvcard/resources/StringUtils.java index 2009dce..cee9fb6 100644 --- a/src/be/nikiroo/jvcard/resources/StringUtils.java +++ b/src/be/nikiroo/jvcard/resources/StringUtils.java @@ -1,5 +1,10 @@ package be.nikiroo.jvcard.resources; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.Normalizer; @@ -9,6 +14,9 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; +import javax.imageio.ImageIO; +import javax.xml.bind.DatatypeConverter; + import com.googlecode.lanterna.gui2.LinearLayout.Alignment; public class StringUtils { @@ -49,7 +57,7 @@ public class StringUtils { */ static public String padString(String text, int width, boolean cut, Alignment align) { - + if (width >= 0) { if (text == null) text = ""; @@ -172,6 +180,51 @@ public class StringUtils { } } + /** + * Convert the given {@link Image} object into a Base64 representation of + * the same {@link Image}. object. + * + * @param image + * the {@link Image} object to convert + * + * @return the Base64 representation + */ + static public String fromImage(BufferedImage image) { + String imageString = null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + try { + ImageIO.write(image, "png", out); + byte[] imageBytes = out.toByteArray(); + + imageString = DatatypeConverter.printBase64Binary(imageBytes); + + out.close(); + } catch (IOException e) { + } + + return imageString; + } + + /** + * Convert the given Base64 representation of an image into an {@link Image} + * object. + * + * @param b64data + * the {@link Image} in Base64 format + * + * @return the {@link Image} object + * + * @throws IOException + * in case of IO error + */ + static public BufferedImage toImage(String b64data) throws IOException { + BufferedImage image = ImageIO.read(new ByteArrayInputStream( + DatatypeConverter.parseBase64Binary(b64data))); + image.toString(); + return image; + } + /** * Return a hash of the given {@link String}. *