使用毕加索从 uri 地址旋转图像



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

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

Firebase 数据库结构,显示链接到 Firebase 存储区域的配置文件映像 uri

当我在页面上显示图像时,图像以错误的方式旋转。我手机上的照片是纵向的,但它在Firebase存储区域中保存为横向。

图片在 Firebase 存储中以横向存储,但以纵向拍摄

手机上以错误的方向呈现的图像

我希望能够做的是让用户从库中选择一个图像,然后在页面上显示该图像。然后我希望能够让用户使用两个按钮自己向左或向右旋转图像。

当我按下旋转按钮时。图像成功旋转一次。 然后,当我按下"保存个人资料图像"按钮时,它会将原始图像从图库发送到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);
}

首先让我们看看问题出在哪里。首先,毕卡索打开图像文件并从中检索位图。此位图是显示在图像视图中的内容。旋转图像时,您正在执行的操作是旋转位图,但包含原始照片的文件永远不会更改。因此,如果要上传旋转照片,则必须从图像视图中检索新位图,使用它创建一个新文件并上传新文件。

这必须奏效。

祝你好运

最新更新