收到相机许可后,预览不显示

时间:2019-11-10 13:56:45

标签: android firebase android-camerax

我正在尝试读取QR码,当我将此逻辑加载到与我允许的逻辑不同的运行时,一切都很好

但是,当我想在权限运行期间执行此操作时,预览未加载。 我发现一些SO帖子说,在您获得许可后加载ui,但这也不起作用。

private fun startCamera() {
    val previewConfig = PreviewConfig.Builder()
        // We want to show input from back camera of the device
        .setLensFacing(CameraX.LensFacing.BACK).build()

    val preview = Preview(previewConfig)

    preview.setOnPreviewOutputUpdateListener { previewOutput ->
        textureView.surfaceTexture = previewOutput.surfaceTexture
    }

    val imageAnalysisConfig = ImageAnalysisConfig.Builder().build()
    val imageAnalysis = ImageAnalysis(imageAnalysisConfig)

    val qrCodeAnalyzer = QRCodeAnalyzer { qrCodes ->
        qrCodes.forEach {
            if (!TextUtils.isEmpty(it.rawValue)) {
                previewAction?.onQRCodeResult(it.rawValue)
            }
        }
    }

    context?.let {
        imageAnalysis.setAnalyzer(it.getExecutor(), qrCodeAnalyzer)
    }

    // We need to bind preview and imageAnalysis use cases
    CameraX.bindToLifecycle(this as LifecycleOwner, preview, imageAnalysis)
}

  override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        startCamera()
    }

我想念什么?

1 个答案:

答案 0 :(得分:0)

在我下面的工作代码中,您可以参考

fragment_camera.xml

public void button1_Click(object sender, EventArgs e) 
{
    if(ButtonClick != null)
        ButtonClick(this, e);
}

obj.ButtonClick += (Sender, e) => 
{
    splitContainer1.Panel2.Visible = false;
};

CameraFragment.kt

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextureView
            android:id="@+id/camera_preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_capture_photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:src="@drawable/ic_camera"
            app:fabSize="auto"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:tint="@android:color/white" />

</androidx.constraintlayout.widget.ConstraintLayout>

让我知道帮助