yellowcath / PhotoMovie

Using your photos to create cool videos.(高仿抖音照片电影功能)
Apache License 2.0
694 stars 145 forks source link

Images rotated automatically #35

Open Bilal512 opened 5 years ago

Bilal512 commented 5 years ago

Images selected from camera roll are rotated 180 degree automatically and there is no way to fix that.

Rahulucky03 commented 4 years ago

Replace loadBitmap method and Add rotateBitmap method in SimplePhotoData.java file

private Bitmap loadBitmap(String uri) {
        Bitmap bitmap = null;
        if (uri.startsWith("drawable://")) {
            String idStr = uri.substring("drawable://".length());
            int id = Integer.parseInt(idStr);
            bitmap = BitmapFactory.decodeResource(mContext.getResources(), id);
        } else if (uri.startsWith("file://")) {
            String path = uri.substring("file://".length());
            bitmap = BitmapFactory.decodeFile(path);
        } else if (uri.startsWith("http")) {
            InputStream is = null;
            try {
                URL url = new URL(uri);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                is = connection.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            String path = uri;

            ExifInterface exif = null;
            try {
                exif = new ExifInterface(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            bitmap = rotateBitmap(BitmapFactory.decodeFile(path), orientation);
        }
        return bitmap;
    }

    public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

        Matrix matrix = new Matrix();
        switch (orientation) {
            /*case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;*/
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }