X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2FStringUtils.java;h=1772bdfdb19582bd729cdc63e946e54d9a2705f9;hb=a1783d00d03245556a833bc02fbe2865225c2501;hp=bfe0d3da44782e82db58416549c1b11a3f74a33e;hpb=7da41ecd30228908bf2afcd07ff7943ab59d4c01;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/resources/StringUtils.java b/src/be/nikiroo/jvcard/resources/StringUtils.java index bfe0d3d..1772bdf 100644 --- a/src/be/nikiroo/jvcard/resources/StringUtils.java +++ b/src/be/nikiroo/jvcard/resources/StringUtils.java @@ -1,5 +1,13 @@ 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.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.Normalizer; @@ -9,6 +17,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 { @@ -22,8 +33,7 @@ public class StringUtils { * @param text * the {@link String} to fix * @param width - * the size of the resulting {@link String} if the text fits or - * if cut is TRUE + * the size of the resulting {@link String} or -1 for a noop * * @return the resulting {@link String} of size size */ @@ -39,7 +49,7 @@ public class StringUtils { * the {@link String} to fix * @param width * the size of the resulting {@link String} if the text fits or - * if cut is TRUE + * if cut is TRUE or -1 for a noop * @param cut * cut the {@link String} shorter if needed * @param align @@ -173,6 +183,83 @@ 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 + * + * @throws IOException + * in case of IO error + */ + static public String fromImage(BufferedImage image) throws IOException { + String imageString = null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + ImageIO.write(image, "jpeg", out); + byte[] imageBytes = out.toByteArray(); + + imageString = DatatypeConverter.printBase64Binary(imageBytes); + + out.close(); + + return imageString; + } + + /** + * Convert the given {@link File} image into a Base64 representation of the + * same {@link File}. + * + * @param file + * the {@link File} image to convert + * + * @return the Base64 representation + * + * @throws IOException + * in case of IO error + */ + static public String fromImage(File file) throws IOException { + String fileString = null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte[] buf = new byte[8192]; + InputStream in = new FileInputStream(file); + + int c = 0; + while ((c = in.read(buf, 0, buf.length)) > 0) { + out.write(buf, 0, c); + } + out.flush(); + in.close(); + + fileString = DatatypeConverter.printBase64Binary(out.toByteArray()); + out.close(); + + return fileString; + } + + /** + * 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}. *