image --load now store the file as is
authorNiki Roo <niki@nikiroo.be>
Wed, 30 Mar 2016 19:41:58 +0000 (21:41 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 30 Mar 2016 19:41:58 +0000 (21:41 +0200)
src/be/nikiroo/jvcard/launcher/Main.java
src/be/nikiroo/jvcard/resources/StringUtils.java

index eeeae4153a57eada0b58e04da065ea2d119101bc..65fbf9c635f51d4a097ca16604680faabe63c3eb 100644 (file)
@@ -319,8 +319,15 @@ public class Main {
                                                if (f.exists()) {
                                                        System.out.println("Loading " + f);
                                                        try {
-                                                               String b64 = StringUtils.fromImage(ImageIO
-                                                                               .read(f));
+                                                               String type = "jpeg";
+                                                               int dotIndex = filename.indexOf('.');
+                                                               if (dotIndex >= 0
+                                                                               && (dotIndex + 1) < filename.length()) {
+                                                                       type = filename.substring(dotIndex + 1)
+                                                                                       .toLowerCase();
+                                                               }
+
+                                                               String b64 = StringUtils.fromImage(f);
 
                                                                // remove previous photos:
                                                                for (Data photo = contact
@@ -332,7 +339,7 @@ public class Main {
 
                                                                List<TypeInfo> types = new LinkedList<TypeInfo>();
                                                                types.add(new TypeInfo("ENCODING", "b"));
-                                                               types.add(new TypeInfo("TYPE", "jpeg"));
+                                                               types.add(new TypeInfo("TYPE", type));
                                                                Data photo = new Data(types, "PHOTO", b64, null);
                                                                contact.add(photo);
                                                        } catch (IOException e) {
index e9203d5b5226b37d52fe102622f49ca729ba4f0b..1772bdfdb19582bd729cdc63e946e54d9a2705f9 100644 (file)
@@ -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.