Camera预览旋转设置

实现效果如下:
在这里插入图片描述
点击4个按钮分别实现4个角度的旋转。

  1. 目标版本设置为21,这样不需要动态申请权限。

  2. 在清单文件申请CAMERA权限

  3. 布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <FrameLayout
            android:id="@+id/fl_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
    
            <View
                android:layout_width="6dp"
                android:layout_height="0dp"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/btn_0"
                android:onClick="onClick"
                android:text="0"/>
    
            <View
                android:layout_width="6dp"
                android:layout_height="0dp"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/btn_90"
                android:onClick="onClick"
                android:text="90"/>
    
            <View
                android:layout_width="6dp"
                android:layout_height="0dp"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:id="@+id/btn_180"
                android:text="180"/>
    
            <View
                android:layout_width="6dp"
                android:layout_height="0dp"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:id="@+id/btn_270"
                android:text="270"/>
    
            <View
                android:layout_width="6dp"
                android:layout_height="0dp"/>
    
        </LinearLayout>
    
    </RelativeLayout>
    
  4. 代码

    class MainActivity : AppCompatActivity() {
    
        private var cameraView: FrameLayout? = null
        private var camera: Camera? = null
        private lateinit var cameraContainer: ViewGroup
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            cameraContainer = findViewById(R.id.fl_container)
        }
    
        override fun onResume() {
            super.onResume()
            startPreview()
        }
    
        override fun onPause() {
            super.onPause()
            stopPreview()
        }
    
        private fun startPreview() {
            val surfaceView = SurfaceView(this)
            surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
                override fun surfaceCreated(holder: SurfaceHolder) {
                    camera = Camera.open()
                    camera?.setPreviewDisplay(holder)
                    camera?.setDisplayOrientation(0)
                    camera?.startPreview()
                }
    
                override fun surfaceDestroyed(holder: SurfaceHolder) {
    
                }
    
                override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
            })
            cameraView = FrameLayout(this).apply { addView(surfaceView) }
            cameraContainer.addView(cameraView)
        }
    
        private fun stopPreview() {
            camera?.stopPreview()
            camera?.release()
            camera = null
    
            cameraView?.removeAllViews()
            (cameraView?.parent as? ViewGroup)?.removeAllViews()
            cameraView = null
        }
    
        fun onClick(view: View) {
            when (view.id) {
                R.id.btn_0 ->   camera?.setDisplayOrientation(0)
                R.id.btn_90 ->  camera?.setDisplayOrientation(90)
                R.id.btn_180 -> camera?.setDisplayOrientation(180)
                R.id.btn_270 -> camera?.setDisplayOrientation(270)
            }
        }
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

android_cai_niao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值