使用 Picasso 从 uri 地址旋转图像

Rotate image from uri address, using Picasso

我已成功将用户个人资料图片上传到其用户 ID 下的 Firebase 存储区域。

从那里我将文件路径放在用户 ID 下的 Firebase 数据库中的该图像

Firebase Database Structure, Showing he profileImage uri that is linked to the firebase storage area

当我在页面上显示图像时,图像旋转方向错误。我 phone 上的照片是肖像,但它在 Firebase 存储区中保存为风景。

Image Stored In Landscape orientation in Firebase Storage, but was taken in portrait orientation

The image rendered on my phone in the wrong orientation

我想要做的是让用户 select 从图库中获取图像,然后将图像显示在 page.Then 我希望能够让用户旋转使用两个按钮向左或向右成像。

当我按下旋转按钮时。图像成功旋转一次。 然后,当我按下 Save Profile Image 按钮时,它将原始图像从图库发送到 Firebase 存储区域。它正在存储错误的图像并将其发送到存储。本质上,它是将原始的、未旋转的图像保存到存储器中。

有什么办法可以解决这个问题吗?

这是我的代码:

private FirebaseAuth auth;
private DatabaseReference myRef;
private FirebaseDatabase database;
private StorageReference storageReference;
private FirebaseUser user;

private ImageView profileImage;
private Uri imageUri;

private static final int GALLERY_INTENT = 2;

private ProgressDialog progressDialog;

/*
Skip irrelevant code
*/

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_upload_image, container, false);

        // Creating firebase links etc
        database = FirebaseDatabase.getInstance();
        myRef = FirebaseDatabase.getInstance().getReference();
        auth = FirebaseAuth.getInstance();
        storageReference = FirebaseStorage.getInstance().getReference();
        user = auth.getCurrentUser();

        // Setting buttons
        profileImage = (ImageView) view.findViewById(R.id.imageViewProfileImage);
        ImageView rotateLeft = (ImageView) view.findViewById(R.id.imageRotateLeft);
        ImageView rotateRight = (ImageView) view.findViewById(R.id.imageRotateRight);
        Button uploadProfileImage = (Button) view.findViewById(R.id.buttonUploadProfileImage);
        Button saveProfileImage = (Button) view.findViewById(R.id.buttonSaveProfileImage);

        // Rotate Left is a button
        rotateLeft.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(profileImage != null)
                {
                    // Rotate image 90
                    Picasso.get().load(imageUri).rotate(90).into(profileImage);
                }
            }
        });

        // Rotate Right is a button
        rotateRight.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(profileImage != null)
                {
                    // Rotate image -90
                    Picasso.get().load(imageUri).rotate(-90).into(profileImage);
                }
            }
        });

        uploadProfileImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // Send user to gallery
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, GALLERY_INTENT);
            }
        });

        // Save image to storage area
        saveProfileImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                progressDialog.setMessage("Uploading Image Please Wait...");
                progressDialog.show();

                final StorageReference filePath = storageReference.child("Images").child(user.getUid()).child(imageUri.getLastPathSegment());
                filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
                {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
                    {
                        Toast.makeText(getActivity(), "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
                        Uri downloadUri = taskSnapshot.getDownloadUrl();

                        // Save image uri in the Firebase database under the usersID
                        myRef.child("Users").child(user.getUid()).child("profileImage").setValue(downloadUri.toString());
                        progressDialog.dismiss();
                    }
                }).addOnFailureListener(new OnFailureListener()
                {
                    @Override
                    public void onFailure(@NonNull Exception e)
                    {
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(), "Failed To Upload!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        return view;
    }

    // Get image data and display on page
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
        {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Displaying Image...");
            progressDialog.show();

            imageUri = data.getData();
            Picasso.get().load(imageUri).into(profileImage);

            progressDialog.dismiss();
        }
    }

您可以尝试将图像下载为 Bitmap,然后旋转并保存。这是您可以使用 Picasso:

执行此操作的代码
int rotationAngle; // You set this angle before calling the method
Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotationAngle);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);

        // Save the rotatedBitmap elsewhere
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}
    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
void downloadImageRotateAndSave() {
    Picasso.with(getContext()).load(imageUri).into(target);
}

先看看是什么问题。首先,picaso 打开图像文件并从中检索位图。该位图显示在 imageView 中。当您旋转图像时,您所做的是旋转位图,但包含原始照片的文件永远不会改变。所以如果你想上传旋转照片,你必须从图像视图中检索新的位图,用它创建一个新文件并上传新文件。

这一定能解决问题。

祝你好运