如何将位图旋转和翻转到特定方向,无论其初始位置如何?

问题描述 投票:0回答:1

我面临一个问题,我想确保图像的位置与图像编号 1 (F):

完全相同

Image No. 1 (F)

但是,从手机图库中选择图像时,该图像可能会以任何方向或位置显示。如何旋转和翻转图像以匹配第 1 号图像 (F) 的方向?

当我从相机本身拍摄图像时也会发生同样的问题,所以它不仅特定于 exif 数据

这是我当前的代码:

private Bitmap getStoredImage() {
    SharedPreferences sharedPreferences = requireContext().getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
    String imagePath = sharedPreferences.getString("front_face_image", null);

    if (imagePath != null) {
        File imgFile = new File(imagePath);
        if (imgFile.exists()) {
            return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        }
    }
    return null;  // Return null if the image path is not found or the file does not exist
}
android android-bitmap
1个回答
0
投票

试试这个, 使用此代码以特定方向旋转位图。您将在结果中获得旋转的位图。

   public int getCameraPhotoOrientation(String imagePath, Bitmap bmp){
        int rotate = 0;
        try {
            ExifInterface exif = new ExifInterface(imagePath);
            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;
            }


            Matrix mat = new Matrix();
            mat.postRotate((rotate)); //degree how much you rotate i rotate 270
            Bitmap bMapRotate=Bitmap.createBitmap(bmp, 0,0,bmp.getWidth(),bmp.getHeight(), mat, true);
return rotate;
}
© www.soinside.com 2019 - 2024. All rights reserved.