X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2FStringUtils.java;h=1772bdfdb19582bd729cdc63e946e54d9a2705f9;hb=a1783d00d03245556a833bc02fbe2865225c2501;hp=e9203d5b5226b37d52fe102622f49ca729ba4f0b;hpb=59597d59aa262e31c2e1b7f66b4cb299f88ebd1b;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/resources/StringUtils.java b/src/be/nikiroo/jvcard/resources/StringUtils.java index e9203d5..1772bdf 100644 --- a/src/be/nikiroo/jvcard/resources/StringUtils.java +++ b/src/be/nikiroo/jvcard/resources/StringUtils.java @@ -4,7 +4,10 @@ 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; @@ -188,24 +191,56 @@ public class StringUtils { * the {@link Image} object to convert * * @return the Base64 representation + * + * @throws IOException + * in case of IO error */ - static public String fromImage(BufferedImage image) { + static public String fromImage(BufferedImage image) throws IOException { String imageString = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); - try { - ImageIO.write(image, "jpeg", out); - byte[] imageBytes = out.toByteArray(); + ImageIO.write(image, "jpeg", out); + byte[] imageBytes = out.toByteArray(); - imageString = DatatypeConverter.printBase64Binary(imageBytes); + imageString = DatatypeConverter.printBase64Binary(imageBytes); - out.close(); - } catch (IOException e) { - } + 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.