Java Code Examples for androidx.exifinterface.media.ExifInterface#getAttributeInt()

The following examples show how to use androidx.exifinterface.media.ExifInterface#getAttributeInt() . 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: BitmapUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static Pair<Integer, Integer> getExifDimensions(InputStream inputStream) throws IOException {
  ExifInterface exif   = new ExifInterface(inputStream);
  int           width  = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
  int           height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
  if (width == 0 || height == 0) {
    return null;
  }

  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  if (orientation == ExifInterface.ORIENTATION_ROTATE_90  ||
      orientation == ExifInterface.ORIENTATION_ROTATE_270 ||
      orientation == ExifInterface.ORIENTATION_TRANSVERSE ||
      orientation == ExifInterface.ORIENTATION_TRANSPOSE)
  {
    return new Pair<>(height, width);
  }
  return new Pair<>(width, height);
}
 
Example 2
Source File: BitmapUtils.java    From Lassi-Android with MIT License 6 votes vote down vote up
/**
 * Rotate the given image by given Exif value.<br>
 * If no rotation is required the image will not be rotated.<br>
 * New bitmap is created and the old one is recycled.
 */
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
    int degrees;
    int orientation =
            exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degrees = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degrees = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degrees = 270;
            break;
        default:
            degrees = 0;
            break;
    }
    return new RotateBitmapResult(bitmap, degrees);
}
 
Example 3
Source File: BitmapUtils.java    From leafpicrevived with GNU General Public License v3.0 6 votes vote down vote up
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;
        }
    }
 
Example 4
Source File: BitmapHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
private static int getOrientation(@NonNull final ExifInterface exif) {
	final int rotation = exif.getAttributeInt(
		ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

	int result;
	switch (rotation) {
	case ExifInterface.ORIENTATION_ROTATE_90:
		result = 90;
		break;
	case ExifInterface.ORIENTATION_ROTATE_180:
		result = 180;
		break;
	case ExifInterface.ORIENTATION_ROTATE_270:
		result = 270;
		break;
	case ExifInterface.ORIENTATION_UNDEFINED:
	default:
		result = 0;
		break;
	}
	if (DEBUG) Log.v(TAG, "getOrientation:" + result);
	return result;
}
 
Example 5
Source File: BitmapUtil.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static Pair<Integer, Integer> getExifDimensions(InputStream inputStream) throws IOException {
  ExifInterface exif   = new ExifInterface(inputStream);
  int           width  = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
  int           height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
  if (width == 0 && height == 0) {
    return null;
  }

  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  if (orientation == ExifInterface.ORIENTATION_ROTATE_90  ||
      orientation == ExifInterface.ORIENTATION_ROTATE_270 ||
      orientation == ExifInterface.ORIENTATION_TRANSVERSE ||
      orientation == ExifInterface.ORIENTATION_TRANSPOSE)
  {
    return new Pair<>(height, width);
  }
  return new Pair<>(width, height);
}
 
Example 6
Source File: BitmapUtils.java    From Android-Image-Cropper with Apache License 2.0 6 votes vote down vote up
/**
 * Rotate the given image by given Exif value.<br>
 * If no rotation is required the image will not be rotated.<br>
 * New bitmap is created and the old one is recycled.
 */
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
  int degrees;
  int orientation =
      exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
      degrees = 90;
      break;
    case ExifInterface.ORIENTATION_ROTATE_180:
      degrees = 180;
      break;
    case ExifInterface.ORIENTATION_ROTATE_270:
      degrees = 270;
      break;
    default:
      degrees = 0;
      break;
  }
  return new RotateBitmapResult(bitmap, degrees);
}
 
Example 7
Source File: AppTools.java    From SSForms with GNU General Public License v3.0 5 votes vote down vote up
public int getCameraPhotoOrientation(Uri imageUri){
    int rotate = 0;
    try {
        File imageFile = new File(imageUri.getPath());

        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}
 
Example 8
Source File: DrawableProvider.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
/**
 * 读取图片的旋转的角度
 *
 * @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;
}
 
Example 9
Source File: ImageHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static Matrix getImageRotation(File file) {
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return null;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                return matrix;
            default:
                return null;
        }
    } catch (Throwable ex /* IOException */) {
        /*
            java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
            java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
              at android.media.MediaMetadataRetriever._setDataSource(Native Method)
              at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:226)
              at androidx.exifinterface.media.ExifInterface.getHeifAttributes(SourceFile:5716)
              at androidx.exifinterface.media.ExifInterface.loadAttributes(SourceFile:4556)
              at androidx.exifinterface.media.ExifInterface.initForFilename(SourceFile:5195)
              at androidx.exifinterface.media.ExifInterface.<init>(SourceFile:3926)
         */
        Log.w(ex);
        return null;
    }
}
 
Example 10
Source File: ImageViewFragment.java    From tindroid with Apache License 2.0 4 votes vote down vote up
@SuppressLint("SetTextI18n")
@Override
public void onResume() {
    super.onResume();

    Activity activity = getActivity();
    Bundle args = getArguments();
    if (activity == null || args == null) {
        return;
    }

    mMatrix.reset();

    Bitmap bmp = null;
    byte[] bits = args.getByteArray(AttachmentHandler.ARG_SRC_BYTES);
    if (bits != null) {
        bmp = BitmapFactory.decodeByteArray(bits, 0, bits.length);
    } else {
        Uri uri = args.getParcelable(AttachmentHandler.ARG_SRC_URI);
        if (uri != null) {
            final ContentResolver resolver = activity.getContentResolver();
            // Resize image to ensure it's under the maximum in-band size.
            try {
                InputStream is = resolver.openInputStream(uri);
                if (is != null) {
                    bmp = BitmapFactory.decodeStream(is, null, null);
                    is.close();
                }
                // Make sure the bitmap is properly oriented in preview.
                is = resolver.openInputStream(uri);
                if (is != null) {
                    ExifInterface exif = new ExifInterface(is);
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    if (bmp != null) {
                        bmp = UiUtils.rotateBitmap(bmp, orientation);
                    }
                    is.close();
                }
            } catch (IOException ex) {
                Log.i(TAG, "Failed to read image from " + uri, ex);
            }
        }
    }

    if (bmp != null) {
        String filename = args.getString(AttachmentHandler.ARG_FILE_NAME);
        if (TextUtils.isEmpty(filename)) {
            filename = getResources().getString(R.string.tinode_image);
        }

        activity.findViewById(R.id.metaPanel).setVisibility(View.VISIBLE);

        mInitialRect = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());
        mWorkingRect = new RectF(mInitialRect);
        if (bits == null) {
            // The image is being previewed before sending.
            activity.findViewById(R.id.sendImagePanel).setVisibility(View.VISIBLE);
            activity.findViewById(R.id.annotation).setVisibility(View.GONE);
            setHasOptionsMenu(false);
        } else {
            // The received image is viewed.
            String size = ((int) mInitialRect.width()) + " \u00D7 " + ((int) mInitialRect.height()) + "; ";
            activity.findViewById(R.id.sendImagePanel).setVisibility(View.GONE);
            activity.findViewById(R.id.annotation).setVisibility(View.VISIBLE);
            ((TextView) activity.findViewById(R.id.content_type)).setText(args.getString("mime"));
            ((TextView) activity.findViewById(R.id.file_name)).setText(filename);
            ((TextView) activity.findViewById(R.id.image_size)).setText(size + UiUtils.bytesToHumanSize(bits.length));
            setHasOptionsMenu(true);
        }

        mImageView.setImageDrawable(new BitmapDrawable(getResources(), bmp));

        // ImageView size is set later. Must add an observer to get the size.
        mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ensure we call it only once.
                mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mScreenRect = new RectF(0, 0, mImageView.getWidth(), mImageView.getHeight());
                mMatrix.setRectToRect(mInitialRect, mScreenRect, Matrix.ScaleToFit.CENTER);
                mWorkingMatrix = new Matrix(mMatrix);

                mImageView.setImageMatrix(mMatrix);
            }
        });
    } else {
        // Show broken image.
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_broken_image));
        activity.findViewById(R.id.metaPanel).setVisibility(View.INVISIBLE);

        setHasOptionsMenu(false);
    }
}
 
Example 11
Source File: ImageUpdater.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 13) {
            PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        orientation = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        orientation = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        orientation = 270;
                        break;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            final ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false, 0, 0, 0));
            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, PhotoViewer.SELECT_TYPE_AVATAR, false, new PhotoViewer.EmptyPhotoViewerProvider() {
                @Override
                public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo, boolean notify, int scheduleDate) {
                    String path = null;
                    MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
                    if (photoEntry.imagePath != null) {
                        path = photoEntry.imagePath;
                    } else if (photoEntry.path != null) {
                        path = photoEntry.path;
                    }
                    Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
                    processBitmap(bitmap);
                }

                @Override
                public boolean allowCaption() {
                    return false;
                }

                @Override
                public boolean canScrollAway() {
                    return false;
                }
            }, null);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 14) {
            if (data == null || data.getData() == null) {
                return;
            }
            startCrop(null, data.getData());
        }
    }
}
 
Example 12
Source File: ImageUpdater.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 13) {
            PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        orientation = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        orientation = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        orientation = 270;
                        break;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            final ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false, 0, 0, 0));
            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, PhotoViewer.SELECT_TYPE_AVATAR, false, new PhotoViewer.EmptyPhotoViewerProvider() {
                @Override
                public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo, boolean notify, int scheduleDate) {
                    String path = null;
                    MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
                    if (photoEntry.imagePath != null) {
                        path = photoEntry.imagePath;
                    } else if (photoEntry.path != null) {
                        path = photoEntry.path;
                    }
                    Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
                    processBitmap(bitmap);
                }

                @Override
                public boolean allowCaption() {
                    return false;
                }

                @Override
                public boolean canScrollAway() {
                    return false;
                }
            }, null);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 14) {
            if (data == null || data.getData() == null) {
                return;
            }
            startCrop(null, data.getData());
        }
    }
}