package fr.bamlab.rncameraroll;

import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import java.text.SimpleDateFormat;

import java.io.IOException;

/**
 * Created by florian on 02/12/15.
 */
class CameraImage {
    private String localPath;
    private int width;
    private int height;
    private int orientation;
    private long timestamp;

    CameraImage(String localPath) {
        this.localPath = localPath;

        computeDimensions();
        computeOrientation();
        computeTimestamp();
    };

    private void computeDimensions() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; //Avoid decoding the entire file

        BitmapFactory.decodeFile(this.localPath, options);
        width = options.outWidth;
        height = options.outHeight;
    }

    private void computeOrientation() {
        ExifInterface exif;
        orientation = 0;
        try {
            exif = new ExifInterface(localPath);
            int exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (exifOrientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;

                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;

                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
            }
        } catch (IOException e) {
            // Can't retrieve the orientation, let it to 0.
        }
    }

    private void computeTimestamp() {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

            ExifInterface exif = new ExifInterface(localPath);
            String dateString = exif.getAttribute(ExifInterface.TAG_DATETIME);

            timestamp = fmt.parse(dateString).getTime() / 1000;
        } catch (Exception e) {
            // Can't retrieve the timestamp, let it be 0.
        }
    }

    public String getLocalPath() {
        return localPath;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getOrientation() {
        return orientation;
    }

    public long getTimestamp() {
        return timestamp;
    }
}

@@ -100,16 +100,45 @@
 terface;
+%0A%0Aimport java.io.IOException;
 %0Aimport 
@@ -170,37 +170,8 @@
 t;%0A%0A
-import java.io.IOException;%0A%0A
 /**%0A
@@ -458,32 +458,270 @@
 teDimensions();%0A
+        computeExifProperties();%0A    %7D%0A%0A    private void computeExifProperties() %7B%0A        ExifInterface exif;%0A        try %7B%0A            exif = new ExifInterface(localPath);%0A        %7D catch(IOException e) %7B%0A            return;%0A        %7D%0A%0A
         computeO
@@ -731,16 +731,20 @@
 ntation(
+exif
 );%0A     
@@ -763,24 +763,28 @@
 mestamp(
+exif
 );%0A    %7D
 ;%0A%0A    p
@@ -775,17 +775,16 @@
 );%0A    %7D
-;
 %0A%0A    pr
@@ -1134,28 +1134,16 @@
 ntation(
-) %7B%0A        
 ExifInte
@@ -1156,102 +1156,12 @@
 exif
-;%0A        orientation = 0;%0A        try %7B%0A            exif = new ExifInterface(localPath);%0A    
+) %7B%0A
     
@@ -1216,36 +1216,32 @@
                 
-  
-  
 ExifInterface.TA
@@ -1255,20 +1255,16 @@
 TATION,%0A
-    
         
@@ -1315,20 +1315,16 @@
         
-    
 switch (
@@ -1346,36 +1346,32 @@
 ) %7B%0A            
-  
-  
 case ExifInterfa
@@ -1404,36 +1404,32 @@
                 
-    
 orientation = 90
@@ -1438,36 +1438,32 @@
                 
-    
 break;%0A%0A        
@@ -1458,36 +1458,32 @@
 k;%0A%0A            
-  
-  
 case ExifInterfa
@@ -1517,36 +1517,32 @@
                 
-    
 orientation = 18
@@ -1552,36 +1552,32 @@
                 
-    
 break;%0A%0A        
@@ -1572,36 +1572,32 @@
 k;%0A%0A            
- 
-   
 case ExifInterfa
@@ -1631,36 +1631,32 @@
                 
-    
 orientation = 27
@@ -1678,127 +1678,15 @@
     
-    
 break;%0A
-            %7D%0A        %7D catch (IOException e) %7B%0A            // Can't retrieve the orientation, let it to 0.%0A
     
@@ -1736,30 +1736,30 @@
 amp(
-) %7B%0A        try %7B%0A    
+ExifInterface exif) %7B%0A
     
@@ -1834,76 +1834,8 @@
 %22);%0A
-%0A            ExifInterface exif = new ExifInterface(localPath);%0A    
     
@@ -1902,24 +1902,38 @@
 DATETIME);%0A%0A
+        try %7B%0A
             

Set Exif only once for camera image