在我阅读的所有教程中,都涉及在OpenCV Camera
应用中使用Android
的情况,
override fun onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): Mat {
// retrieve the frame from `inputFrame`
// - the grayscale frame by imputFrame.gray()
// - the RGBA frame by inputFrame.rgba()
return inputFrame.rgba() // will show in the UI
}
我尝试了imputFrame.gray()
和inputFrame.rgba()
,但在两种情况下都遇到了相同的错误:
Unresolved reference rgba()
如果仅尝试使用return inputFrame
,则会出现黑屏。
我也尝试了以下方法,但仍然出现黑屏:
import org.opencv.android.CameraBridgeViewBase
import org.opencv.core.CvType.CV_8U
import org.opencv.core.Mat
import org.opencv.imgproc.Imgproc
class OpenCVCListner(context: Context) : CameraBridgeViewBase.CvCameraViewListener {
private var mRgba: Mat? = null
private var mGray: Mat? = null
override fun onCameraViewStarted(width: Int, height: Int) {
mGray = Mat()
mRgba = Mat()
}
override fun onCameraViewStopped() {
}
override fun onCameraFrame(inputFrame: Mat?): Mat {
inputFrame!!.convertTo(mRgba, CV_8U, 1.0, 0.0)
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGRA2GRAY)
return mGray!!
}
}
在调用照相机的主要活动中,我已经完成
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val ocvLoaded = OpenCVLoader.initDebug()
if (ocvLoaded) {
loadModel(this)
Toast.makeText(
this@MainActivity, "OpenCV loaded",
Toast.LENGTH_SHORT
).show()
opencv_jcv.apply {
setCvCameraViewListener(OpenCVCListner(this@MainActivity))
setCameraIndex(1)
enableView()
}
} else {
Toast.makeText(
this@MainActivity, "Unable to load OpenCV",
Toast.LENGTH_SHORT
).show()
Log.d("openCV", "loader: ${OpenCVLoader.initDebug()}")
}
}
}
在我的布局中,我已经:
<org.opencv.android.JavaCameraView
android:id="@+id/opencv_jcv"
android:layout_width="343dp"
android:layout_height="378dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
tools:layout_editor_absoluteX="34dp" />