SurfaceView预览返回黑屏

时间:2019-04-23 20:00:21

标签: android kotlin surfaceview

我需要一些帮助。我编写代码用法SurfaceView。代码编译没有错误和交战。当我用相机打开活动时,这仅返回黑屏。我已经允许了。你能帮我吗?

科特琳课

class ScanActivity : AppCompatActivity(), SurfaceHolder.Callback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan)

        cameraInit()
    }

    private fun cameraInit(){
        val surfaceView: SurfaceView = findViewById(R.id.testCamera)
        val surfaceHolder: SurfaceHolder = surfaceView.holder.apply {
            addCallback(this@ScanActivity)
            setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
        }

        val cam: Camera = Camera.open().apply {
            setPreviewDisplay(surfaceHolder)
            startPreview()
        }
}

   override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {

    }

    override fun surfaceDestroyed(p0: SurfaceHolder?) {

    }

    override fun surfaceCreated(p0: SurfaceHolder?) {

    }
}


xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".ScanActivity">

    <SurfaceView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/testCamera"/>
</RelativeLayout>

0 个答案:

没有答案