Search in sources :

Example 6 with ExifInterface

use of androidx.exifinterface.media.ExifInterface in project Signal-Android by signalapp.

the class MediaUtil method getDimensions.

@WorkerThread
public static Pair<Integer, Integer> getDimensions(@NonNull Context context, @Nullable String contentType, @Nullable Uri uri) {
    if (uri == null || (!MediaUtil.isImageType(contentType) && !MediaUtil.isVideoType(contentType))) {
        return new Pair<>(0, 0);
    }
    Pair<Integer, Integer> dimens = null;
    if (MediaUtil.isGif(contentType)) {
        try {
            GifDrawable drawable = GlideApp.with(context).asGif().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).load(new DecryptableUri(uri)).submit().get();
            dimens = new Pair<>(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        } catch (InterruptedException e) {
            Log.w(TAG, "Was unable to complete work for GIF dimensions.", e);
        } catch (ExecutionException e) {
            Log.w(TAG, "Glide experienced an exception while trying to get GIF dimensions.", e);
        }
    } else if (MediaUtil.hasVideoThumbnail(context, uri)) {
        Bitmap thumbnail = MediaUtil.getVideoThumbnail(context, uri, 1000);
        if (thumbnail != null) {
            dimens = new Pair<>(thumbnail.getWidth(), thumbnail.getHeight());
        }
    } else {
        InputStream attachmentStream = null;
        try {
            if (MediaUtil.isJpegType(contentType)) {
                attachmentStream = PartAuthority.getAttachmentStream(context, uri);
                dimens = BitmapUtil.getExifDimensions(new ExifInterface(attachmentStream));
                attachmentStream.close();
                attachmentStream = null;
            }
            if (dimens == null) {
                attachmentStream = PartAuthority.getAttachmentStream(context, uri);
                dimens = BitmapUtil.getDimensions(attachmentStream);
            }
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to find file when retrieving media dimensions.", e);
        } catch (IOException e) {
            Log.w(TAG, "Experienced a read error when retrieving media dimensions.", e);
        } catch (BitmapDecodingException e) {
            Log.w(TAG, "Bitmap decoding error when retrieving dimensions.", e);
        } finally {
            if (attachmentStream != null) {
                try {
                    attachmentStream.close();
                } catch (IOException e) {
                    Log.w(TAG, "Failed to close stream after retrieving dimensions.", e);
                }
            }
        }
    }
    if (dimens == null) {
        dimens = new Pair<>(0, 0);
    }
    Log.d(TAG, "Dimensions for [" + uri + "] are " + dimens.first + " x " + dimens.second);
    return dimens;
}
Also used : InputStream(java.io.InputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) FileNotFoundException(java.io.FileNotFoundException) GifDrawable(com.bumptech.glide.load.resource.gif.GifDrawable) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair) WorkerThread(androidx.annotation.WorkerThread)

Example 7 with ExifInterface

use of androidx.exifinterface.media.ExifInterface in project RxTools by vondear.

the class RxScaleImageView 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 {
                        TLog.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
            }
        } catch (Exception e) {
            TLog.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 {
                TLog.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
            }
        } catch (Exception e) {
            TLog.w(TAG, "Could not get EXIF orientation of image");
        }
    }
    return exifOrientation;
}
Also used : ExifInterface(androidx.exifinterface.media.ExifInterface) Cursor(android.database.Cursor) Point(android.graphics.Point) Paint(android.graphics.Paint) AnyThread(androidx.annotation.AnyThread)

Example 8 with ExifInterface

use of androidx.exifinterface.media.ExifInterface in project Signal-Android by WhisperSystems.

the class MediaUtil method getDimensions.

@WorkerThread
public static Pair<Integer, Integer> getDimensions(@NonNull Context context, @Nullable String contentType, @Nullable Uri uri) {
    if (uri == null || (!MediaUtil.isImageType(contentType) && !MediaUtil.isVideoType(contentType))) {
        return new Pair<>(0, 0);
    }
    Pair<Integer, Integer> dimens = null;
    if (MediaUtil.isGif(contentType)) {
        try {
            GifDrawable drawable = GlideApp.with(context).asGif().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).load(new DecryptableUri(uri)).submit().get();
            dimens = new Pair<>(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        } catch (InterruptedException e) {
            Log.w(TAG, "Was unable to complete work for GIF dimensions.", e);
        } catch (ExecutionException e) {
            Log.w(TAG, "Glide experienced an exception while trying to get GIF dimensions.", e);
        }
    } else if (MediaUtil.hasVideoThumbnail(context, uri)) {
        Bitmap thumbnail = MediaUtil.getVideoThumbnail(context, uri, 1000);
        if (thumbnail != null) {
            dimens = new Pair<>(thumbnail.getWidth(), thumbnail.getHeight());
        }
    } else {
        InputStream attachmentStream = null;
        try {
            if (MediaUtil.isJpegType(contentType)) {
                attachmentStream = PartAuthority.getAttachmentStream(context, uri);
                dimens = BitmapUtil.getExifDimensions(new ExifInterface(attachmentStream));
                attachmentStream.close();
                attachmentStream = null;
            }
            if (dimens == null) {
                attachmentStream = PartAuthority.getAttachmentStream(context, uri);
                dimens = BitmapUtil.getDimensions(attachmentStream);
            }
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to find file when retrieving media dimensions.", e);
        } catch (IOException e) {
            Log.w(TAG, "Experienced a read error when retrieving media dimensions.", e);
        } catch (BitmapDecodingException e) {
            Log.w(TAG, "Bitmap decoding error when retrieving dimensions.", e);
        } finally {
            if (attachmentStream != null) {
                try {
                    attachmentStream.close();
                } catch (IOException e) {
                    Log.w(TAG, "Failed to close stream after retrieving dimensions.", e);
                }
            }
        }
    }
    if (dimens == null) {
        dimens = new Pair<>(0, 0);
    }
    Log.d(TAG, "Dimensions for [" + uri + "] are " + dimens.first + " x " + dimens.second);
    return dimens;
}
Also used : InputStream(java.io.InputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) FileNotFoundException(java.io.FileNotFoundException) GifDrawable(com.bumptech.glide.load.resource.gif.GifDrawable) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair) WorkerThread(androidx.annotation.WorkerThread)

Example 9 with ExifInterface

use of androidx.exifinterface.media.ExifInterface in project Signal-Android by WhisperSystems.

the class ZoomingImageView method setSubsamplingImageViewUri.

private void setSubsamplingImageViewUri(@NonNull Uri uri) {
    subsamplingImageView.setBitmapDecoderFactory(new AttachmentBitmapDecoderFactory());
    subsamplingImageView.setRegionDecoderFactory(new AttachmentRegionDecoderFactory());
    subsamplingImageView.setVisibility(View.VISIBLE);
    photoView.setVisibility(View.GONE);
    // https://github.com/signalapp/Signal-Android/issues/11732#issuecomment-963203545
    try {
        final InputStream inputStream = PartAuthority.getAttachmentStream(getContext(), uri);
        final int orientation = BitmapUtil.getExifOrientation(new ExifInterface(inputStream));
        inputStream.close();
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_270);
        } else {
            subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_0);
        }
    } catch (IOException e) {
        Log.w(TAG, e);
    }
    subsamplingImageView.setImage(ImageSource.uri(uri));
}
Also used : InputStream(java.io.InputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint)

Example 10 with ExifInterface

use of androidx.exifinterface.media.ExifInterface in project MVPArms by JessYanCoding.

the class DrawableProvider method getBitmapDegree.

/**
 * 读取图片的旋转的角度
 *
 * @param path 图片绝对路径
 * @return 图片的旋转角度
 */
public static int getBitmapDegree(String path) {
    int degree = 0;
    try {
        // 从指定路径下读取图片,并获取其EXIF信息
        ExifInterface exifInterface = new ExifInterface(path);
        // 获取图片的旋转信息
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            default:
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}
Also used : ExifInterface(androidx.exifinterface.media.ExifInterface) IOException(java.io.IOException)

Aggregations

ExifInterface (androidx.exifinterface.media.ExifInterface)15 IOException (java.io.IOException)10 InputStream (java.io.InputStream)5 Paint (android.graphics.Paint)4 Bitmap (android.graphics.Bitmap)3 FileNotFoundException (java.io.FileNotFoundException)3 SuppressLint (android.annotation.SuppressLint)2 Cursor (android.database.Cursor)2 Point (android.graphics.Point)2 Pair (android.util.Pair)2 AnyThread (androidx.annotation.AnyThread)2 WorkerThread (androidx.annotation.WorkerThread)2 GifDrawable (com.bumptech.glide.load.resource.gif.GifDrawable)2 FileOutputStream (java.io.FileOutputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 DecryptableUri (org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri)2 Resources (android.content.res.Resources)1 BitmapFactory (android.graphics.BitmapFactory)1 Matrix (android.graphics.Matrix)1 ImageProcessingException (com.drew.imaging.ImageProcessingException)1