Android: 相机活动,图像旋转
创始人
2024-10-06 01:29:24
0

以下是一个示例解决方案,展示了如何在Android相机活动中旋转图像:

  1. 创建一个旋转图像的工具类:
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);
    }
}
  1. 在相机活动中获取照片并旋转图像:
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上显示给用户。

请注意,上述代码只是一个示例,你可以根据你的具体需求进行修改和调整。

相关内容

热门资讯

透视规律!wpk官网下载链接,... 透视规律!wpk官网下载链接,佛手在线大菠萝为什么都输,手段教程(好像是有挂)-哔哩哔哩透视规律!w...
透视靠谱!黑侠破解wepoke... 透视靠谱!黑侠破解wepoker(透视)开挂辅助app(切实真的是有挂)-哔哩哔哩1、黑侠破解wep...
透视系统!德扑圈透视,德州局透... 透视系统!德扑圈透视,德州局透视,要领教程(一贯存在有挂)-哔哩哔哩;1、该软件可以轻松地帮助玩家将...
透视机器人!wepoker私人... 透视机器人!wepoker私人局透视教程(透视)开挂辅助挂(都是真的有挂)-哔哩哔哩1、玩家可以在w...
透视智能ai!wepoker私... 透视智能ai!wepoker私人局俱乐部怎么进,wepoker透视有用吗,练习教程(一贯是真的挂)-...
透视神器!wepoker插件功... 透视神器!wepoker插件功能辅助器(透视)开挂辅助技巧(好像是真的挂)-哔哩哔哩暗藏猫腻,小编详...
透视工具!wepoker底牌透... 透视工具!wepoker底牌透视,wpk透视工作室,妙招教程(一贯真的有挂)-哔哩哔哩透视工具!we...
透视数据!wepoker游戏的... 透视数据!wepoker游戏的安装教程(透视)开挂辅助方法(本来是真的挂)-哔哩哔哩运wepoker...
透视黑科技!wepoker怎么... 透视黑科技!wepoker怎么破解游戏,uupoker有透视吗,方式教程(一贯真的有挂)-哔哩哔哩1...
透视猫腻!智星德州插件2024... 透视猫腻!智星德州插件2024最新版(透视)开挂辅助工具(真是是有挂)-哔哩哔哩一、智星德州插件20...