我想用CameraX拍些照片,但不行。照片+按钮上只有黑屏。摄像头没有预览。我的Android手机版本是11。有人能帮忙吗?我发现了一些代码的例子,它使用了“android扩展”,但现在它被弃用了,我改变了一些代码。
我想这里有问题,但我不知道如何解决它:
it.setSurfaceProvider(Preview.SurfaceProvider { pv_camera })
这是MainActivity的完整代码:
` package com.example.testcamerax
import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private lateinit var cameraExecutor: ExecutorService
private lateinit var pv_camera: View
companion object {
private const val TAG = "CameraX"
private const val FILE_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
private const val PERMISSION_CODE = 10
private val PERMISSION = arrayOf(Manifest.permission.CAMERA)
}
private fun allPermissionGranted() = PERMISSION.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (allPermissionGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, PERMISSION, PERMISSION_CODE)
}
cameraExecutor = Executors.newSingleThreadExecutor()
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_CODE) {
if (allPermissionGranted()) {
startCamera()
}
else {
Toast.makeText(this, "Permission error", Toast.LENGTH_SHORT).show()
finish()
}
}
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
pv_camera = findViewById(R.id.pv_camera)
val cameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder().build()
.also {
it.setSurfaceProvider(Preview.SurfaceProvider { pv_camera })
}
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
this,cameraSelector,
preview
)
} catch (e: Exception) {
Log.e(TAG, "Bind error")
}
}, ContextCompat.getMainExecutor(this))
}
}
”
这里是XML
”
<FrameLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pv_camera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_save"
android:text="Save"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="120dp"/>
</FrameLayout>
”
感谢您阅读此问题。如果有人帮忙我会很高兴的。
我创建了一个新的变量pv_view,但它不起作用。第一个代码是it.setSurfaceProvider(pv_camera.surfaceProvider)
,但不推荐使用的插件有问题。
1条答案
按热度按时间t5zmwmid1#
1-将pv_camera类型更改为PreviewView
2-将此代码放入startCamera函数中