android - 无法找到支持的表面信息 - CameraId:null (CameraX) on android things
问题描述
我有一个运行 android 的树莓派 3。我已经连接了相机,我还可以在 Peripherals -> Camera 中看到输出。我还想预览相机并对每一帧进行图像处理。使用 CameraX api,我使用了此处找到的代码。
这是代码:
MainActivity.kt:
package com.example.sos_project
import android.graphics.Matrix
import android.os.Bundle
import android.util.Size
import android.view.Surface
import android.view.TextureView
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraX
import androidx.camera.core.Preview
import androidx.camera.core.PreviewConfig
/**
* Skeleton of an Android Things activity.
*
* Android Things peripheral APIs are accessible through the class
* PeripheralManagerService. For example, the snippet below will open a GPIO pin and
* set it to HIGH:
*
* <pre>{@code
* val service = PeripheralManagerService()
* val mLedGpio = service.openGpio("BCM6")
* mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
* mLedGpio.value = true
* }</pre>
* <p>
* For more complex peripherals, look for an existing user-space driver, or implement one if none
* is available.
*
* @see <a href="https://github.com/androidthings/contrib-drivers#readme">https://github.com/androidthings/contrib-drivers#readme</a>
*
*/
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewFinder = findViewById(R.id.view_finder)
viewFinder.post { startCamera() }
viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
updateTransform()
}
}
private lateinit var viewFinder: TextureView
private fun startCamera() {
// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
setTargetResolution(Size(640,480))
setLensFacing(CameraX.LensFacing.FRONT)
}.build()
// Build the viewfinder use case
val preview = Preview(previewConfig)
// Every time the viewfinder is updated, recompute layout
preview.setOnPreviewOutputUpdateListener {
// To update the SurfaceTexture, we have to remove it and re-add it
val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
parent.addView(viewFinder, 0)
viewFinder.surfaceTexture = it.surfaceTexture
updateTransform()
}
// Bind use cases to lifecycle
// If Android Studio complains about "this" being not a LifecycleOwner
// try rebuilding the project or updating the appcompat dependency to
// version 1.1.0 or higher.
CameraX.bindToLifecycle(this, preview)
}
private fun updateTransform() {
val matrix = Matrix()
// Compute the center of the view finder
val centerX = viewFinder.width / 2f
val centerY = viewFinder.height / 2f
// Correct preview output to account for display rotation
val rotationDegrees = when(viewFinder.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
// Finally, apply transformations to our TextureView
viewFinder.setTransform(matrix)
}
}
完全错误:
2019-11-21 11:03:33.555 3823-3823/com.example.___ E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.___, PID: 3823
java.lang.IllegalArgumentException: Fail to find supported surface info - CameraId:null
at androidx.camera.camera2.impl.Camera2DeviceSurfaceManager.requiresCorrectedAspectRatio(Camera2DeviceSurfaceManager.java:274)
at androidx.camera.core.Preview.updateUseCaseConfig(Preview.java:476)
at androidx.camera.core.UseCase.<init>(UseCase.java:96)
at androidx.camera.core.Preview.<init>(Preview.java:115)
at com.example.___.MainActivity.startCamera(MainActivity.kt:60)
at com.example.___.MainActivity.access$startCamera(MainActivity.kt:35)
at com.example.___.MainActivity$onCreate$1.run(MainActivity.kt:41)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
编辑:我试图用java实现同样的事情,我得到了同样的错误
java.lang.IllegalArgumentException: Unable to get camera ID for use case androidx.camera.core.Preview
我的代码有什么问题?
提前致谢
解决方案
推荐阅读
- node.js - Mongoose 不保存更改 (updateOne)
- python-imaging-library - 如何通过压缩将 PDF 文件转换为 TIFF 文件(第 4 组压缩)
- c# - 为什么 db.savechanges 也存储旧记录?
- apache-kafka - 卡夫卡消费者没有收到任何消息
- python - 单元测试中的“从未等待”警告
- java - 无法对 ArrayList 执行操作
- php - 无法解压_laravel8安装问题,请给出解决方案
- c++ - 在找到数组中的最大数时,正确的数组声明应该是什么?
- flutter - Flutter - 如何重置PageView中的当前页面索引(刷卡后)?
- javascript - 图标未从 react-icons 导出,并显示编译器错误