以下是一个示例解决方案,展示了如何在Android相机活动中旋转图像:
public class ImageUtils {
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
}
public class CameraActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
imageView = findViewById(R.id.imageView);
Button captureButton = findViewById(R.id.captureButton);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
// 旋转图像
Bitmap rotatedBitmap = ImageUtils.rotateImage(imageBitmap, 90);
imageView.setImageBitmap(rotatedBitmap);
}
}
}
以上代码展示了一个简单的相机活动,当用户点击“captureButton”按钮时,会调用相机应用程序拍摄照片。在onActivityResult
方法中,我们获取照片的Bitmap对象,然后使用ImageUtils.rotateImage
方法将图像旋转90度。最后,将旋转后的图像设置到imageView
上显示给用户。
请注意,上述代码只是一个示例,你可以根据你的具体需求进行修改和调整。