use of android.support.media.ExifInterface in project Tusky by Vavassor.
the class DownsizeImageTask method getOrientation.
private static int getOrientation(Uri uri, ContentResolver contentResolver) {
InputStream inputStream;
try {
inputStream = contentResolver.openInputStream(uri);
} catch (FileNotFoundException e) {
Log.d(TAG, Log.getStackTraceString(e));
return ExifInterface.ORIENTATION_UNDEFINED;
}
if (inputStream == null) {
return ExifInterface.ORIENTATION_UNDEFINED;
}
ExifInterface exifInterface;
try {
exifInterface = new ExifInterface(inputStream);
} catch (IOException e) {
Log.d(TAG, Log.getStackTraceString(e));
IOUtils.closeQuietly(inputStream);
return ExifInterface.ORIENTATION_UNDEFINED;
}
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
IOUtils.closeQuietly(inputStream);
return orientation;
}
use of android.support.media.ExifInterface in project muzei by romannurik.
the class GalleryArtSource method ensureMetadataExists.
private void ensureMetadataExists(@NonNull Uri imageUri) {
Cursor existingMetadata = getContentResolver().query(GalleryContract.MetadataCache.CONTENT_URI, new String[] { BaseColumns._ID }, GalleryContract.MetadataCache.COLUMN_NAME_URI + "=?", new String[] { imageUri.toString() }, null);
if (existingMetadata == null) {
return;
}
boolean metadataExists = existingMetadata.moveToFirst();
existingMetadata.close();
if (!metadataExists) {
// No cached metadata or it's stale, need to pull it separately using Exif
ContentValues values = new ContentValues();
values.put(GalleryContract.MetadataCache.COLUMN_NAME_URI, imageUri.toString());
try (InputStream in = getContentResolver().openInputStream(imageUri)) {
if (in == null) {
return;
}
ExifInterface exifInterface = new ExifInterface(in);
String dateString = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
if (!TextUtils.isEmpty(dateString)) {
Date date = sExifDateFormat.parse(dateString);
values.put(GalleryContract.MetadataCache.COLUMN_NAME_DATETIME, date.getTime());
}
float[] latlong = new float[2];
if (exifInterface.getLatLong(latlong)) {
// Reverse geocode
List<Address> addresses = null;
try {
addresses = mGeocoder.getFromLocation(latlong[0], latlong[1], 1);
} catch (IllegalArgumentException e) {
Log.w(TAG, "Invalid latitude/longitude, skipping location metadata", e);
}
if (addresses != null && addresses.size() > 0) {
Address addr = addresses.get(0);
String locality = addr.getLocality();
String adminArea = addr.getAdminArea();
String countryCode = addr.getCountryCode();
StringBuilder sb = new StringBuilder();
if (!TextUtils.isEmpty(locality)) {
sb.append(locality);
}
if (!TextUtils.isEmpty(adminArea)) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(adminArea);
}
if (!TextUtils.isEmpty(countryCode) && !sOmitCountryCodes.contains(countryCode)) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(countryCode);
}
values.put(GalleryContract.MetadataCache.COLUMN_NAME_LOCATION, sb.toString());
}
}
getContentResolver().insert(GalleryContract.MetadataCache.CONTENT_URI, values);
} catch (ParseException | IOException | NumberFormatException | StackOverflowError e) {
Log.w(TAG, "Couldn't read image metadata.", e);
}
}
}
use of android.support.media.ExifInterface in project collect by opendatakit.
the class ImageConverter method rotateImageIfNeeded.
/**
* Sometimes an image might be taken up sideways.
* https://github.com/opendatakit/collect/issues/36
*/
private static void rotateImageIfNeeded(String imagePath) {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
} catch (IOException e) {
Timber.w(e);
}
if (exif != null) {
Bitmap image = FileUtils.getBitmap(imagePath, new BitmapFactory.Options());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateBitmap(image, 90, imagePath);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateBitmap(image, 180, imagePath);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateBitmap(image, 270, imagePath);
break;
}
}
}
use of android.support.media.ExifInterface in project StreetComplete by westnordost.
the class AttachPhotoUtils method getRotationMatrix.
private static Matrix getRotationMatrix(String imagePath) {
int orientation = 0;
try {
ExifInterface exifInterface = new ExifInterface(imagePath);
orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
} catch (IOException e) {
}
Matrix matrix = new Matrix();
switch(orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
}
return matrix;
}
use of android.support.media.ExifInterface in project Tusky by tuskyapp.
the class DownsizeImageTask method getOrientation.
private static int getOrientation(Uri uri, ContentResolver contentResolver) {
InputStream inputStream;
try {
inputStream = contentResolver.openInputStream(uri);
} catch (FileNotFoundException e) {
Log.d(TAG, Log.getStackTraceString(e));
return ExifInterface.ORIENTATION_UNDEFINED;
}
if (inputStream == null) {
return ExifInterface.ORIENTATION_UNDEFINED;
}
ExifInterface exifInterface;
try {
exifInterface = new ExifInterface(inputStream);
} catch (IOException e) {
Log.d(TAG, Log.getStackTraceString(e));
IOUtils.closeQuietly(inputStream);
return ExifInterface.ORIENTATION_UNDEFINED;
}
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
IOUtils.closeQuietly(inputStream);
return orientation;
}
Aggregations