use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.
the class ExifEditorActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exif_editor);
AlbumItem albumItem = getIntent().getParcelableExtra(ALBUM_ITEM);
if (savedInstanceState != null && savedInstanceState.containsKey(EDITED_ITEMS)) {
editedItems = savedInstanceState.getParcelableArrayList(EDITED_ITEMS);
} else {
editedItems = new ArrayList<>();
}
if (albumItem == null) {
this.finish();
return;
}
String mimeType = MediaType.getMimeType(this, albumItem.getUri(this));
if (!MediaType.doesSupportWritingExifMimeType(mimeType)) {
Toast.makeText(this, "Editing Exif values is only supported for JPEG images", Toast.LENGTH_SHORT).show();
this.finish();
return;
}
exifInterface = null;
try {
exifInterface = new ExifInterface(albumItem.getPath());
} catch (IOException e) {
e.printStackTrace();
}
if (exifInterface == null) {
this.finish();
return;
}
final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new RecyclerViewAdapter(new RecyclerViewAdapter.OnEditCallback() {
@Override
public void onItemEdited(String tag, String newValue) {
for (ExifUtil.ExifItem item : editedItems) {
if (item.getTag().equals(tag)) {
item.setValue(newValue);
showSaveButton(true);
return;
}
}
// ExifItem wasn't previously edited
ExifUtil.ExifItem item = getItem(tag);
item.setValue(newValue);
editedItems.add(item);
showSaveButton(true);
}
@Override
public ExifUtil.ExifItem getItem(String tag) {
for (ExifUtil.ExifItem item : editedItems) {
if (item.getTag().equals(tag)) {
return item;
}
}
return new ExifUtil.ExifItem(tag, exifInterface.getAttribute(tag));
}
}));
final ViewGroup rootView = findViewById(R.id.root_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) {
toolbar.setPadding(toolbar.getPaddingStart() + insets.getSystemWindowInsetLeft(), toolbar.getPaddingTop() + insets.getSystemWindowInsetTop(), toolbar.getPaddingEnd() + insets.getSystemWindowInsetRight(), toolbar.getPaddingBottom());
recyclerView.setPadding(recyclerView.getPaddingStart() + insets.getSystemWindowInsetLeft(), recyclerView.getPaddingTop(), recyclerView.getPaddingEnd() + insets.getSystemWindowInsetRight(), recyclerView.getPaddingBottom() + insets.getSystemWindowInsetBottom());
// clear this listener so insets aren't re-applied
rootView.setOnApplyWindowInsetsListener(null);
return insets.consumeSystemWindowInsets();
}
});
} else {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// hacky way of getting window insets on pre-Lollipop
int[] screenSize = Util.getScreenSize(ExifEditorActivity.this);
int[] windowInsets = new int[] { Math.abs(screenSize[0] - rootView.getLeft()), Math.abs(screenSize[1] - rootView.getTop()), Math.abs(screenSize[2] - rootView.getRight()), Math.abs(screenSize[3] - rootView.getBottom()) };
toolbar.setPadding(toolbar.getPaddingStart() + windowInsets[0], toolbar.getPaddingTop() + windowInsets[1], toolbar.getPaddingEnd() + windowInsets[2], toolbar.getPaddingBottom());
recyclerView.setPadding(recyclerView.getPaddingStart() + windowInsets[0], recyclerView.getPaddingTop(), recyclerView.getPaddingEnd() + windowInsets[2], recyclerView.getPaddingBottom() + windowInsets[3]);
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.
the class ExifUtil method getExifOrientationAngle.
public static int getExifOrientationAngle(Context context, AlbumItem albumItem) {
ExifInterface exif = getExifInterface(context, albumItem);
if (exif == null) {
return 0;
}
int orientation = (int) getCastValue(exif, ExifInterface.TAG_ORIENTATION);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.
the class ExifUtil method getExifInterface.
public static ExifInterface getExifInterface(Context context, AlbumItem albumItem) {
if (albumItem == null) {
return null;
}
ExifInterface exif = null;
try {
Uri uri = albumItem.getUri(context);
InputStream is = context.getContentResolver().openInputStream(uri);
if (is != null) {
exif = new ExifInterface(is);
}
} catch (IOException | SecurityException e) {
e.printStackTrace();
return null;
}
return exif;
}
use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.
the class Util method getImageDimensions.
public static int[] getImageDimensions(Context context, Uri uri) {
int[] dimensions = new int[] { 0, 0 };
try {
InputStream is = context.getContentResolver().openInputStream(uri);
// try exif
String mimeType = MediaType.getMimeType(context, uri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && MediaType.doesSupportExifMimeType(mimeType) && is != null) {
ExifInterface exif = new ExifInterface(is);
if (exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH) != null && exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH) != null) {
int width = (int) ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_WIDTH);
int height = (int) ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_LENGTH);
if (width != 0 && height != 0) {
return new int[] { width, height };
}
}
}
// exif didn't work
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, new Rect(0, 0, 0, 0), options);
dimensions[0] = options.outWidth;
dimensions[1] = options.outHeight;
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return dimensions;
}
use of android.support.media.ExifInterface in project AnExplorer by 1hakr.
the class ImageUtils method decodeStream.
/**
* Wrapper around {@link BitmapFactory#decodeStream(InputStream, Rect,
* BitmapFactory.Options)} that returns {@code null} on {@link
* OutOfMemoryError}.
*
* @param factory Used to create input streams that holds the raw data to be decoded into a
* bitmap.
* @param outPadding If not null, return the padding rect for the bitmap if
* it exists, otherwise set padding to [-1,-1,-1,-1]. If
* no bitmap is returned (null) then padding is
* unchanged.
* @param opts null-ok; Options that control downsampling and whether the
* image should be completely decoded, or just is size returned.
* @return The decoded bitmap, or null if the image data could not be
* decoded, or, if opts is non-null, if opts requested only the
* size be returned (in opts.outWidth and opts.outHeight)
*/
public static Bitmap decodeStream(final InputStreamFactory factory, final Rect outPadding, final BitmapFactory.Options opts) throws FileNotFoundException {
InputStream is = null;
try {
// Determine the orientation for this image
is = factory.createInputStream();
final ExifInterface exif = new ExifInterface(is);
final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
is.close();
// Decode the bitmap
is = factory.createInputStream();
final Bitmap originalBitmap = BitmapFactory.decodeStream(is, outPadding, opts);
if (is != null && originalBitmap == null && !opts.inJustDecodeBounds) {
Log.w(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options): " + "Image bytes cannot be decoded into a Bitmap");
throw new UnsupportedOperationException("Image bytes cannot be decoded into a Bitmap.");
}
// Rotate the Bitmap based on the orientation
if (originalBitmap != null && orientation != 0) {
final Matrix matrix = new Matrix();
matrix.postRotate(orientation);
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
}
return originalBitmap;
} catch (OutOfMemoryError oome) {
Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an OOME", oome);
return null;
} catch (IOException ioe) {
Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an IOE", ioe);
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Do nothing
}
}
}
}
Aggregations