Java Code Examples for android.media.ExifInterface#getAttribute()

The following examples show how to use android.media.ExifInterface#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageEditorModule.java    From react-native-image-editor with MIT License 7 votes vote down vote up
private static void copyExif(Context context, Uri oldImage, File newFile) throws IOException {
  File oldFile = getFileFromUri(context, oldImage);
  if (oldFile == null) {
    FLog.w(ReactConstants.TAG, "Couldn't get real path for uri: " + oldImage);
    return;
  }

  ExifInterface oldExif = new ExifInterface(oldFile.getAbsolutePath());
  ExifInterface newExif = new ExifInterface(newFile.getAbsolutePath());
  for (String attribute : EXIF_ATTRIBUTES) {
    String value = oldExif.getAttribute(attribute);
    if (value != null) {
      newExif.setAttribute(attribute, value);
    }
  }
  newExif.saveAttributes();
}
 
Example 2
Source File: ImageEditingManager.java    From react-native-GPay with MIT License 6 votes vote down vote up
private static void copyExif(Context context, Uri oldImage, File newFile) throws IOException {
  File oldFile = getFileFromUri(context, oldImage);
  if (oldFile == null) {
    FLog.w(ReactConstants.TAG, "Couldn't get real path for uri: " + oldImage);
    return;
  }

  ExifInterface oldExif = new ExifInterface(oldFile.getAbsolutePath());
  ExifInterface newExif = new ExifInterface(newFile.getAbsolutePath());
  for (String attribute : EXIF_ATTRIBUTES) {
    String value = oldExif.getAttribute(attribute);
    if (value != null) {
      newExif.setAttribute(attribute, value);
    }
  }
  newExif.saveAttributes();
}
 
Example 3
Source File: MediaSnippetsActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void listing17_20() {
  // Listing 17-20: Reading and modifying EXIF data
  File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
    "test.jpg");
  try {
    ExifInterface exif = new ExifInterface(file.getCanonicalPath());

    // Read the camera model
    String model = exif.getAttribute(ExifInterface.TAG_MODEL);
    Log.d(TAG, "Model: " + model);

    // Set the camera make
    exif.setAttribute(ExifInterface.TAG_MAKE, "My Phone");

    // Finally, call saveAttributes to save the updated tag data
    exif.saveAttributes();
  } catch (IOException e) {
    Log.e(TAG, "IO Exception", e);
  }
}
 
Example 4
Source File: CursorUtils.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
private static int getOrientationFromExif(String path) {
    int orientation = -1;
    try {
        ExifInterface exif = new ExifInterface(path);
        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                orientation = 270;
        }
    } catch (Exception e) {
        Logger.e(e);
    }
    return orientation;
}
 
Example 5
Source File: ImageRotateModule.java    From react-native-image-rotate with MIT License 6 votes vote down vote up
private static void copyExif(Context context, Uri oldImage, File newFile) throws IOException {
    File oldFile = getFileFromUri(context, oldImage);
    if (oldFile == null) {
        FLog.w(ReactConstants.TAG, "Couldn't get real path for uri: " + oldImage);
        return;
    }

    ExifInterface oldExif = new ExifInterface(oldFile.getAbsolutePath());
    ExifInterface newExif = new ExifInterface(newFile.getAbsolutePath());
    for (String attribute : EXIF_ATTRIBUTES) {
        String value = oldExif.getAttribute(attribute);
        if (value != null) {
            newExif.setAttribute(attribute, value);
        }
    }
    newExif.saveAttributes();
}
 
Example 6
Source File: RTMediaImpl.java    From memoir with Apache License 2.0 5 votes vote down vote up
protected int getHeight(String filePath) {
    int height = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String h = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        height = h.equals("0") ? getDimension(filePath, false) : Integer.parseInt(h);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return height;
}
 
Example 7
Source File: FileUtil.java    From bither-android with Apache License 2.0 5 votes vote down vote up
public static int getOrientationOfFile(String fileName) {
    int orientation = 0;
    try {
        ExifInterface exif = new ExifInterface(fileName);
        String orientationString = exif
                .getAttribute(ExifInterface.TAG_ORIENTATION);
        if (Utils.isNubmer(orientationString)) {
            int orc = Integer.valueOf(orientationString);
            switch (orc) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
                default:
                    break;
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return orientation;

}
 
Example 8
Source File: CameraImage.java    From rn-camera-roll with MIT License 5 votes vote down vote up
private void computeTimestamp(ExifInterface exif) {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    String dateString = exif.getAttribute(ExifInterface.TAG_DATETIME);

    try {
        timestamp = fmt.parse(dateString).getTime() / 1000;
    } catch (Exception e) {
        // Can't retrieve the timestamp, let it be 0.
    }
}
 
Example 9
Source File: ImageHelper.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
public static BitmapSize getImageSize(String fileName) {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(fileName, o);
    if (o.outWidth == 0 || o.outHeight == 0) {
        return null;
    }

    int w = o.outWidth;
    int h = o.outHeight;

    try {
        ExifInterface exif = new ExifInterface(fileName);
        String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (exifOrientation != null) {
            if (exifOrientation.equals("5") ||
                    exifOrientation.equals("6") ||
                    exifOrientation.equals("7") ||
                    exifOrientation.equals("8")) {
                w = o.outHeight;
                h = o.outWidth;

            }
        }
    } catch (IOException e) {
        // e.printStackTrace();
    }

    return new BitmapSize(w, h);
}
 
Example 10
Source File: ImageHelper.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Bitmap loadOptimizedHQ(String fileName) {
    int scale = getScaleFactor(getImageSize(fileName), MAX_PIXELS_HQ);

    BitmapFactory.Options o = new BitmapFactory.Options();

    o.inScaled = false;
    o.inSampleSize = scale;
    o.inPreferredConfig = Bitmap.Config.ARGB_8888;

    if (Build.VERSION.SDK_INT >= 10) {
        o.inPreferQualityOverSpeed = true;
    }

    if (Build.VERSION.SDK_INT >= 11) {
        o.inMutable = true;
    }

    if (!new File(fileName).exists()) {
        return null;
    }

    Bitmap res = BitmapFactory.decodeFile(fileName, o);
    if (res == null) {
        return null;
    }

    try {
        ExifInterface exif = new ExifInterface(fileName);
        String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = 0;
        if (exifOrientation != null) {
            orientation = Integer.parseInt(exifOrientation);
        }
        res = fixExif(res, orientation);
    } catch (IOException e) {
        // e.printStackTrace();
    }

    return res;
}
 
Example 11
Source File: RTMediaImpl.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
protected int getHeight(String filePath) {
    int height = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String h = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        height = h.equals("0") ? getDimension(filePath, false) : Integer.parseInt(h);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return height;
}
 
Example 12
Source File: RTMediaImpl.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
protected int getWidth(String filePath) {
    int width = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String w = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        width = w.equals("0") ? getDimension(filePath, true) : Integer.parseInt(w);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return width;
}
 
Example 13
Source File: RTMediaImpl.java    From memoir with Apache License 2.0 5 votes vote down vote up
protected int getWidth(String filePath) {
    int width = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String w = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        width = w.equals("0") ? getDimension(filePath, true) : Integer.parseInt(w);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return width;
}
 
Example 14
Source File: RTMediaImpl.java    From memoir with Apache License 2.0 5 votes vote down vote up
protected int getHeight(String filePath) {
    int height = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String h = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        height = h.equals("0") ? getDimension(filePath, false) : Integer.parseInt(h);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return height;
}
 
Example 15
Source File: RTMediaImpl.java    From memoir with Apache License 2.0 5 votes vote down vote up
protected int getWidth(String filePath) {
    int width = 0;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String w = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        width = w.equals("0") ? getDimension(filePath, true) : Integer.parseInt(w);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
    return width;
}
 
Example 16
Source File: ExifUtil.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public static int getExifRotation(String imgPath) {
	int rotate = 0;
    try {
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) {
            case ExifInterface.ORIENTATION_NORMAL:
            	rotate = 0;
            	break;
            case ExifInterface.ORIENTATION_ROTATE_90:
            	rotate = 90;
            	break;
            case ExifInterface.ORIENTATION_ROTATE_180:
            	rotate = 180;
            	break;
            case ExifInterface.ORIENTATION_ROTATE_270:
            	rotate = 270;
            	break;
            default:
            	rotate = 0;
            	break;
            }
        }
    } catch (Exception e) {
        Logger.debug("ExifUtil", e);
    }
    
    return rotate;
}
 
Example 17
Source File: MediaDetails.java    From medialibrary with Apache License 2.0 5 votes vote down vote up
private static void setExifData(MediaDetails details, ExifInterface exif, String tag,
                                int key) {
    String value = exif.getAttribute(tag);
    if (value != null) {
        if (key == MediaDetails.INDEX_FLASH) {
            MediaDetails.FlashState state = new MediaDetails.FlashState(
                    Integer.valueOf(value.toString()));
            details.addDetail(key, state);
        } else {
            details.addDetail(key, value);
        }
    }
}
 
Example 18
Source File: RxExifTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 将经纬度信息写入JPEG图片文件里
 *
 * @param picPath JPEG图片文件路径
 * @param dLat    纬度
 * @param dLon    经度
 */
public static void writeLatLonIntoJpeg(String picPath, double dLat, double dLon) {
    File file = new File(picPath);
    if (file.exists()) {
        try {
            ExifInterface exif = new ExifInterface(picPath);
            String tagLat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            String tagLon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            if (tagLat == null && tagLon == null) {// 无经纬度信息
                exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, gpsInfoConvert(dLat));
                exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, dLat > 0 ? "N" : "S"); // 区分南北半球
                exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, gpsInfoConvert(dLon));
                exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, dLon > 0 ? "E" : "W"); // 区分东经西经
                exif.saveAttributes();
            }
            exif.saveAttributes();

            Logger.d(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) + "\n"
                    + exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) + "\n"
                    + exif.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD) + "\n"
                    + exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH) + "\n"
                    + exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
        } catch (Exception e) {

        }
    }
}
 
Example 19
Source File: Utils.java    From SimpleCropView with MIT License 4 votes vote down vote up
/**
 * Copy EXIF info to new file
 *
 * =========================================
 *
 * NOTE: PNG cannot not have EXIF info.
 *
 * source: JPEG, save: JPEG
 * copies all EXIF data
 *
 * source: JPEG, save: PNG
 * saves no EXIF data
 *
 * source: PNG, save: JPEG
 * saves only width and height EXIF data
 *
 * source: PNG, save: PNG
 * saves no EXIF data
 *
 * =========================================
 */
public static void copyExifInfo(Context context, Uri sourceUri, Uri saveUri, int outputWidth,
    int outputHeight) {
  if (sourceUri == null || saveUri == null) return;
  try {
    File sourceFile = Utils.getFileFromUri(context, sourceUri);
    File saveFile = Utils.getFileFromUri(context, saveUri);
    if (sourceFile == null || saveFile == null) {
      return;
    }
    String sourcePath = sourceFile.getAbsolutePath();
    String savePath = saveFile.getAbsolutePath();

    ExifInterface sourceExif = new ExifInterface(sourcePath);
    List<String> tags = new ArrayList<>();
    tags.add(ExifInterface.TAG_DATETIME);
    tags.add(ExifInterface.TAG_FLASH);
    tags.add(ExifInterface.TAG_FOCAL_LENGTH);
    tags.add(ExifInterface.TAG_GPS_ALTITUDE);
    tags.add(ExifInterface.TAG_GPS_ALTITUDE_REF);
    tags.add(ExifInterface.TAG_GPS_DATESTAMP);
    tags.add(ExifInterface.TAG_GPS_LATITUDE);
    tags.add(ExifInterface.TAG_GPS_LATITUDE_REF);
    tags.add(ExifInterface.TAG_GPS_LONGITUDE);
    tags.add(ExifInterface.TAG_GPS_LONGITUDE_REF);
    tags.add(ExifInterface.TAG_GPS_PROCESSING_METHOD);
    tags.add(ExifInterface.TAG_GPS_TIMESTAMP);
    tags.add(ExifInterface.TAG_MAKE);
    tags.add(ExifInterface.TAG_MODEL);
    tags.add(ExifInterface.TAG_WHITE_BALANCE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      tags.add(ExifInterface.TAG_EXPOSURE_TIME);
      //noinspection deprecation
      tags.add(ExifInterface.TAG_APERTURE);
      //noinspection deprecation
      tags.add(ExifInterface.TAG_ISO);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      tags.add(ExifInterface.TAG_DATETIME_DIGITIZED);
      tags.add(ExifInterface.TAG_SUBSEC_TIME);
      //noinspection deprecation
      tags.add(ExifInterface.TAG_SUBSEC_TIME_DIG);
      //noinspection deprecation
      tags.add(ExifInterface.TAG_SUBSEC_TIME_ORIG);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      tags.add(ExifInterface.TAG_F_NUMBER);
      tags.add(ExifInterface.TAG_ISO_SPEED_RATINGS);
      tags.add(ExifInterface.TAG_SUBSEC_TIME_DIGITIZED);
      tags.add(ExifInterface.TAG_SUBSEC_TIME_ORIGINAL);
    }

    ExifInterface saveExif = new ExifInterface(savePath);
    String value;
    for (String tag : tags) {
      value = sourceExif.getAttribute(tag);
      if (!TextUtils.isEmpty(value)) {
        saveExif.setAttribute(tag, value);
      }
    }
    saveExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, String.valueOf(outputWidth));
    saveExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, String.valueOf(outputHeight));
    saveExif.setAttribute(ExifInterface.TAG_ORIENTATION,
        String.valueOf(ExifInterface.ORIENTATION_UNDEFINED));

    saveExif.saveAttributes();
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 20
Source File: ImageHeaderParser.java    From Matisse-Kotlin with Apache License 2.0 4 votes vote down vote up
public static void copyExif(ExifInterface originalExif, int width, int height, String imageOutputPath) {
    String[] attributes = new String[]{
            ExifInterface.TAG_APERTURE,
            ExifInterface.TAG_DATETIME,
            ExifInterface.TAG_DATETIME_DIGITIZED,
            ExifInterface.TAG_EXPOSURE_TIME,
            ExifInterface.TAG_FLASH,
            ExifInterface.TAG_FOCAL_LENGTH,
            ExifInterface.TAG_GPS_ALTITUDE,
            ExifInterface.TAG_GPS_ALTITUDE_REF,
            ExifInterface.TAG_GPS_DATESTAMP,
            ExifInterface.TAG_GPS_LATITUDE,
            ExifInterface.TAG_GPS_LATITUDE_REF,
            ExifInterface.TAG_GPS_LONGITUDE,
            ExifInterface.TAG_GPS_LONGITUDE_REF,
            ExifInterface.TAG_GPS_PROCESSING_METHOD,
            ExifInterface.TAG_GPS_TIMESTAMP,
            ExifInterface.TAG_ISO,
            ExifInterface.TAG_MAKE,
            ExifInterface.TAG_MODEL,
            ExifInterface.TAG_SUBSEC_TIME,
            ExifInterface.TAG_SUBSEC_TIME_DIG,
            ExifInterface.TAG_SUBSEC_TIME_ORIG,
            ExifInterface.TAG_WHITE_BALANCE
    };

    try {
        ExifInterface newExif = new ExifInterface(imageOutputPath);
        String value;
        if (originalExif != null) {
            for (String attribute : attributes) {
                value = originalExif.getAttribute(attribute);
                if (!TextUtils.isEmpty(value)) {
                    newExif.setAttribute(attribute, value);
                }
            }
        }
        newExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, String.valueOf(width));
        newExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, String.valueOf(height));
        newExif.setAttribute(ExifInterface.TAG_ORIENTATION, "0");

        newExif.saveAttributes();

    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
}