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

The following examples show how to use android.media.ExifInterface#setAttribute() . 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: 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 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: BitmapOptimizer.java    From SmileEssence with MIT License 6 votes vote down vote up
private static int getRotateDegreeFromExif(String filePath) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(filePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            degree = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            degree = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            degree = 270;
        }
        if (degree != 0) {
            exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0");
            exifInterface.saveAttributes();
        }
    } catch (IOException e) {
        degree = -1;
        e.printStackTrace();
        Logger.error(e);
    }
    Logger.debug("Exif degree = " + degree);
    return degree;
}
 
Example 7
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 8
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 9
Source File: CropUtil.java    From GalleryFinal 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) {
        ILogger.e(e);
        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: FaceTrackActivity.java    From FaceDetect with Apache License 2.0 5 votes vote down vote up
/**
 * 将图片的旋转角度置为0
 * @Title: setPictureDegreeZero
 * @param path
 * @return void
 * @date 2012-12-10 上午10:54:46
 */
private void setPictureDegreeZero(String path){
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        //修正图片的旋转角度,设置其不旋转。这里也可以设置其旋转的角度,可以传值过去,
        //例如旋转90度,传值ExifInterface.ORIENTATION_ROTATE_90,需要将这个值转换为String类型的
       // exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "no");
        exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_ROTATE_270+"");
        exifInterface.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
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 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 15
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 16
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 17
Source File: MutableImage.java    From react-native-camera-face-detector with MIT License 4 votes vote down vote up
public static void writeExifData(double latitude, double longitude, ExifInterface exif) throws IOException {
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, toDegreeMinuteSecods(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, latitudeRef(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, toDegreeMinuteSecods(longitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, longitudeRef(longitude));
}
 
Example 18
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 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: 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());
    }
}