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

The following examples show how to use android.media.ExifInterface#saveAttributes() . 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: ImageFileUtil.java    From bither-android with Apache License 2.0 6 votes vote down vote up
private static void saveExifInterface(File file, int orientation)
        throws IOException {
    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
    switch (orientation) {
        case 90:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION,
                    Integer.toString(ExifInterface.ORIENTATION_ROTATE_90));
            break;
        case 180:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION,
                    Integer.toString(ExifInterface.ORIENTATION_ROTATE_180));
            break;
        case 270:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION,
                    Integer.toString(ExifInterface.ORIENTATION_ROTATE_270));
            break;
        default:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION,
                    Integer.toString(ExifInterface.ORIENTATION_NORMAL));
            break;
    }
    exif.saveAttributes();
}
 
Example 3
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 4
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 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: LocalImage.java    From medialibrary with Apache License 2.0 6 votes vote down vote up
@Override
public void rotate(int degrees) throws Exception {
    GalleryUtils.assertNotInRenderThread();
    Uri baseUri = Images.Media.EXTERNAL_CONTENT_URI;
    ContentValues values = new ContentValues();
    int rotation = (this.rotation + degrees) % 360;
    if (rotation < 0) rotation += 360;

    if (mimeType.equalsIgnoreCase("image/jpeg")) {
        ExifInterface exifInterface = new ExifInterface(filePath);
        exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
                String.valueOf(rotation));
        exifInterface.saveAttributes();
        fileSize = new File(filePath).length();
        values.put(Images.Media.SIZE, fileSize);
    }

    values.put(Images.Media.ORIENTATION, rotation);
    mApplication.getContentResolver().update(baseUri, values, "_id=?",
            new String[]{String.valueOf(id)});
}
 
Example 7
Source File: MutableImage.java    From react-native-camera-face-detector with MIT License 5 votes vote down vote up
public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
    fos.close();

    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());

        // copy original exif data to the output exif...
        // unfortunately, this Android ExifInterface class doesn't understand all the tags so we lose some
        for (Directory directory : originalImageMetaData().getDirectories()) {
            for (Tag tag : directory.getTags()) {
                int tagType = tag.getTagType();
                Object object = directory.getObject(tagType);
                exif.setAttribute(tag.getTagName(), object.toString());
            }
        }

        writeLocationExifData(options, exif);

        if(hasBeenReoriented)
            rewriteOrientation(exif);

        exif.saveAttributes();
    } catch (ImageProcessingException  | IOException e) {
        Log.e(TAG, "failed to save exif data", e);
    }
}
 
Example 8
Source File: CropUtil.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 9
Source File: CropUtil.java    From ImageChoose with MIT License 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        CLog.e("Error copying Exif data" + e.getMessage());
        return false;
    }
}
 
Example 10
Source File: CropUtil.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 11
Source File: CropUtil.java    From HaiNaBaiChuan with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 12
Source File: CropUtil.java    From CloudPan with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 13
Source File: CropUtil.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 14
Source File: CropUtil.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 15
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 16
Source File: CropUtil.java    From LockDemo with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 17
Source File: CropUtil.java    From styT with Apache License 2.0 5 votes vote down vote up
public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null) return false;
    try {
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        //Log.e("Error copying Exif data", e);
        return false;
    }
}
 
Example 18
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());
    }
}
 
Example 19
Source File: LocationUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void writeLocationToExif(
        File imgFile,
        Location location)
        throws IOException
{
    if (location == null) {
        return;
    }

    ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());

    double lat = location.getLatitude();
    double absLat = Math.abs(lat);
    String dms = Location.convert(absLat, Location.FORMAT_SECONDS);
    String[] splits = dms.split(":");
    String[] secondsArr = (splits[2]).split("\\.");
    String seconds;

    if (secondsArr.length == 0) {
        seconds = splits[2];
    } else {
        seconds = secondsArr[0];
    }

    String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitudeStr);
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat > 0 ? "N" : "S");

    double lon = location.getLongitude();
    double absLon = Math.abs(lon);
    dms = Location.convert(absLon, Location.FORMAT_SECONDS);
    splits = dms.split(":");
    secondsArr = (splits[2]).split("\\.");

    if (secondsArr.length == 0) {
        seconds = splits[2];
    } else {
        seconds = secondsArr[0];
    }

    String longitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitudeStr);
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon > 0 ? "E" : "W");

    exif.saveAttributes();
}
 
Example 20
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();
  }
}