Android 系统Camera拍照照片旋转

  读取图片旋转的角度,然后根据角度把图片旋转过来。

  

/**
	 * 读取图片属性:旋转的角度
	 * @param path 图片绝对路径
	 * @return degree旋转的角度
	 */
    public static int readPictureDegree(String path) {
        int degree  = 0;
        try {
                ExifInterface exifInterface = new ExifInterface(path);
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
        } catch (IOException e) {
                e.printStackTrace(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤来实现: 1. 在 `build.gradle` 文件中添加依赖: ``` implementation "androidx.camera:camera-core:1.0.0" implementation "androidx.camera:camera-camera2:1.0.0" ``` 2. 在布局文件中添加一个按钮: ``` <Button android:id="@+id/btn_take_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Picture" /> ``` 3. 在 Activity 或 Fragment 中初始化 CameraX: ```kotlin private lateinit var imageCapture: ImageCapture private fun startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() val preview = Preview.Builder().build().also { it.setSurfaceProvider(viewFinder.surfaceProvider) } imageCapture = ImageCapture.Builder().build() val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA try { cameraProvider.unbindAll() cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture) } catch (e: Exception) { Log.e(TAG, "Use case binding failed", e) } }, ContextCompat.getMainExecutor(this)) } ``` 这里我们使用了 CameraX 的 `ImageCapture` 用例,该用例可以用于拍照。 4. 在按钮点击事件中触发拍照操作: ```kotlin btn_take_picture.setOnClickListener { val file = File(externalMediaDirs.first(), "${System.currentTimeMillis()}.jpg") val outputOptions = ImageCapture.OutputFileOptions.Builder(file).build() imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { val savedUri = Uri.fromFile(file) // TODO: 处理保存成功后的操作 } override fun onError(exception: ImageCaptureException) { Log.e(TAG, "Photo capture failed: ${exception.message}", exception) } }) } ``` 在这里我们将拍摄的照片保存到了外部存储器中,并且在保存成功后得到了保存的文件路径。 5. 添加相机权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> ``` 并在运行时动态请求相机权限。 以上是使用 CameraX 系统相机拍照的基本步骤,你可以根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值