use of android.support.media.ExifInterface in project muzei by romannurik.
the class WearableController method getRotation.
private static int getRotation(Context context) {
ContentResolver contentResolver = context.getContentResolver();
int rotation = 0;
try (InputStream in = contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI)) {
if (in == null) {
return 0;
}
ExifInterface exifInterface = new ExifInterface(in);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
}
} catch (IOException | StackOverflowError ignored) {
}
return rotation;
}
use of android.support.media.ExifInterface in project LeafPic by HoraApps.
the class BitmapUtils method getOrientation.
public static int getOrientation(Uri uri, Context ctx) {
try (InputStream in = ctx.getContentResolver().openInputStream(uri)) {
if (in == null) {
return 0;
}
ExifInterface exif = new ExifInterface(in);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
return SubsamplingScaleImageView.ORIENTATION_180;
case ExifInterface.ORIENTATION_ROTATE_90:
return SubsamplingScaleImageView.ORIENTATION_90;
case ExifInterface.ORIENTATION_ROTATE_270:
return SubsamplingScaleImageView.ORIENTATION_270;
default:
return SubsamplingScaleImageView.ORIENTATION_0;
}
} catch (IOException e) {
return 0;
}
}
use of android.support.media.ExifInterface in project android by nextcloud.
the class FilesSyncJob method onRunJob.
@NonNull
@Override
protected Result onRunJob(Params params) {
final Context context = MainApp.getAppContext();
final ContentResolver contentResolver = context.getContentResolver();
FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
PowerManager.WakeLock wakeLock = null;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire();
}
PersistableBundleCompat bundle = params.getExtras();
final boolean skipCustom = bundle.getBoolean(SKIP_CUSTOM, false);
final boolean overridePowerSaving = bundle.getBoolean(OVERRIDE_POWER_SAVING, false);
// If we are in power save mode, better to postpone upload
if (PowerUtils.isPowerSaveMode(context) && !overridePowerSaving) {
wakeLock.release();
return Result.SUCCESS;
}
Resources resources = MainApp.getAppContext().getResources();
boolean lightVersion = resources.getBoolean(R.bool.syncedFolder_light);
FilesSyncHelper.restartJobsIfNeeded();
FilesSyncHelper.insertAllDBEntries(skipCustom);
// Create all the providers we'll need
final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
if ((syncedFolder.isEnabled()) && (!skipCustom || MediaFolderType.CUSTOM != syncedFolder.getType())) {
for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(), Long.toString(syncedFolder.getId()))) {
File file = new File(path);
Long lastModificationTime = file.lastModified();
final Locale currentLocale = context.getResources().getConfiguration().locale;
if (MediaFolderType.IMAGE == syncedFolder.getType()) {
String mimeTypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
if ("image/jpeg".equalsIgnoreCase(mimeTypeString) || "image/tiff".equalsIgnoreCase(mimeTypeString)) {
try {
ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
String exifDate = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
if (!TextUtils.isEmpty(exifDate)) {
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", currentLocale);
sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
Date dateTime = sFormatter.parse(exifDate, pos);
lastModificationTime = dateTime.getTime();
}
} catch (IOException e) {
Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
}
}
}
String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
Account account = AccountUtils.getOwnCloudAccountByName(context, syncedFolder.getAccount());
String remotePath;
boolean subfolderByDate;
Integer uploadAction;
boolean needsCharging;
boolean needsWifi;
if (lightVersion) {
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
needsCharging = resources.getBoolean(R.bool.syncedFolder_light_on_charging);
needsWifi = account == null || arbitraryDataProvider.getBooleanValue(account.name, Preferences.SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI);
String uploadActionString = resources.getString(R.string.syncedFolder_light_upload_behaviour);
uploadAction = getUploadAction(uploadActionString);
subfolderByDate = resources.getBoolean(R.bool.syncedFolder_light_use_subfolders);
remotePath = resources.getString(R.string.syncedFolder_remote_folder);
} else {
needsCharging = syncedFolder.getChargingOnly();
needsWifi = syncedFolder.getWifiOnly();
uploadAction = syncedFolder.getUploadAction();
subfolderByDate = syncedFolder.getSubfolderByDate();
remotePath = syncedFolder.getRemotePath();
}
if (!subfolderByDate) {
String adaptedPath = file.getAbsolutePath().replace(syncedFolder.getLocalPath(), "").replace("/" + file.getName(), "");
remotePath += adaptedPath;
}
requester.uploadFileWithOverwrite(context, account, file.getAbsolutePath(), FileStorageUtils.getInstantUploadFilePath(currentLocale, remotePath, file.getName(), lastModificationTime, subfolderByDate), uploadAction, mimeType, // create parent folder if not existent
true, UploadFileOperation.CREATED_AS_INSTANT_PICTURE, needsWifi, needsCharging, true);
filesystemDataProvider.updateFilesystemFileAsSentForUpload(path, Long.toString(syncedFolder.getId()));
}
}
}
if (wakeLock != null) {
wakeLock.release();
}
return Result.SUCCESS;
}
use of android.support.media.ExifInterface in project subsampling-scale-image-view by davemorrissey.
the class SubsamplingScaleImageView method getExifOrientation.
/**
* Helper method for load tasks. Examines the EXIF info on the image file to determine the orientation.
* This will only work for external files, not assets, resources or other URIs.
*/
@AnyThread
private int getExifOrientation(Context context, String sourceUri) {
int exifOrientation = ORIENTATION_0;
if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
Cursor cursor = null;
try {
String[] columns = { MediaStore.Images.Media.ORIENTATION };
cursor = context.getContentResolver().query(Uri.parse(sourceUri), columns, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int orientation = cursor.getInt(0);
if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
exifOrientation = orientation;
} else {
Log.w(TAG, "Unsupported orientation: " + orientation);
}
}
}
} catch (Exception e) {
Log.w(TAG, "Could not get orientation of image from media store");
} finally {
if (cursor != null) {
cursor.close();
}
}
} else if (sourceUri.startsWith(ImageSource.FILE_SCHEME) && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
try {
ExifInterface exifInterface = new ExifInterface(sourceUri.substring(ImageSource.FILE_SCHEME.length() - 1));
int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientationAttr == ExifInterface.ORIENTATION_NORMAL || orientationAttr == ExifInterface.ORIENTATION_UNDEFINED) {
exifOrientation = ORIENTATION_0;
} else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_90) {
exifOrientation = ORIENTATION_90;
} else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_180) {
exifOrientation = ORIENTATION_180;
} else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_270) {
exifOrientation = ORIENTATION_270;
} else {
Log.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
}
} catch (Exception e) {
Log.w(TAG, "Could not get EXIF orientation of image");
}
}
return exifOrientation;
}
use of android.support.media.ExifInterface in project react-native-camera by react-native-community.
the class ResolveTakenPictureAsyncTask method doInBackground.
@Override
protected WritableMap doInBackground(Void... voids) {
WritableMap response = Arguments.createMap();
ByteArrayInputStream inputStream = null;
// we need the stream only for photos from a device
if (mBitmap == null) {
mBitmap = BitmapFactory.decodeByteArray(mImageData, 0, mImageData.length);
inputStream = new ByteArrayInputStream(mImageData);
}
try {
if (inputStream != null) {
ExifInterface exifInterface = new ExifInterface(inputStream);
// Get orientation of the image from mImageData via inputStream
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
if (mOptions.hasKey("width")) {
mBitmap = resizeBitmap(mBitmap, mOptions.getInt("width"));
}
// Rotate the bitmap to the proper orientation if needed
if (mOptions.hasKey("fixOrientation") && mOptions.getBoolean("fixOrientation") && orientation != ExifInterface.ORIENTATION_UNDEFINED) {
mBitmap = rotateBitmap(mBitmap, getImageRotation(orientation));
}
if (mOptions.hasKey("mirrorImage") && mOptions.getBoolean("mirrorImage")) {
mBitmap = flipHorizontally(mBitmap);
}
// Write Exif data to the response if requested
if (mOptions.hasKey("exif") && mOptions.getBoolean("exif")) {
WritableMap exifData = RNCameraViewHelper.getExifData(exifInterface);
response.putMap("exif", exifData);
}
}
// Upon rotating, write the image's dimensions to the response
response.putInt("width", mBitmap.getWidth());
response.putInt("height", mBitmap.getHeight());
// Cache compressed image in imageStream
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, getQuality(), imageStream);
// Write compressed image to file in cache directory
String filePath = writeStreamToFile(imageStream);
File imageFile = new File(filePath);
String fileUri = Uri.fromFile(imageFile).toString();
response.putString("uri", fileUri);
// Write base64-encoded image to the response if requested
if (mOptions.hasKey("base64") && mOptions.getBoolean("base64")) {
response.putString("base64", Base64.encodeToString(imageStream.toByteArray(), Base64.DEFAULT));
}
// Cleanup
imageStream.close();
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
return response;
} catch (Resources.NotFoundException e) {
mPromise.reject(ERROR_TAG, "Documents directory of the app could not be found.", e);
e.printStackTrace();
} catch (IOException e) {
mPromise.reject(ERROR_TAG, "An unknown I/O exception has occurred.", e);
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// An exception had to occur, promise has already been rejected. Do not try to resolve it again.
return null;
}
Aggregations