安卓& OpenCV-用户界面更改时应用崩溃 [英] Android & OpenCV - App crashes on UI change
问题描述
我正在使用OpenCV人脸检测制作应用程序.我想在相机图像中检测到人脸时对UI进行一些更改.
I'm making an app using OpenCV face detection. I want to make some changes to the UI when a face is detected in the camera image.
布局分为两部分,左侧是一些文本,右侧是相机图像.我想在检测到脸部时更改文本颜色.
The layout is split in two parts, on the left some text and camera image on the right. I want to change the text color when a face is detected.
我没有问题地在onCameraFrame()方法中检测到面孔,但是如果我尝试从此方法更改UI元素,则应用程序将崩溃.
I detect the faces in the onCameraFrame() method with no problems, but if I try to change the UI elements from this method the app crashes.
这是全部外观. /*不是真实的代码,仅是示例*/
Here's how it all looks. /* Not real code, just example */
public class MyClass extends Activity implements CvCameraViewListener
{
private CameraBridgeViewBase mOpenCvCameraView;
private TextView myTextElement;
private FaceLocator faceLocator;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myclass);
myTextElement = (TextView) findViewById(R.id.text_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.my_class_face_detector_layout);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
}
@Override
public Mat onCameraFrame(Mat inputFrame)
{
if (faceLocator != null) {
bool face_detected = faceLocator.detectFaces(inputFrame);
if (face_detected) {
myTextElement.setTextColor(Color.GREEN);
}
}
return inputFrame;
}
}
任何人都可以帮忙吗?
这是堆栈跟踪...
at android.view.ViewRootImpl.checkThread
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1005)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4548)
at android.view.View.invalidate(View.java:11095)
at android.view.View.invalidate(View.java:11044)
at android.widget.TextView.updateTextColors(TextView.java:3473)
at android.widget.TextView.setTextColor(TextView.java:2663)
at com.riteh.mateo.cameracontrol.CCSettings.onCameraFrame(CCSettings.java:186)
at org.opencv.android.CameraBridgeViewBase$CvCameraViewListenerAdapter.onCameraFrame(CameraBridgeViewBase.java:157)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:393)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:346)
at java.lang.Thread.run(Thread.java:841)
推荐答案
两天后就解决了...堆栈跟踪将我推向正确的方向.
Just solved it after two days... The stack trace pushed me in the right direction.
问题在于该方法在单独的线程中运行,因此我不得不使用runOnUiThread()来更改UI.
The problem was that the the method was running in a separate thread so I had to use runOnUiThread() to change the UI.
只需将onCameraFrame方法更改为此
Just a matter of changing the onCameraFrame method to this
@Override
public Mat onCameraFrame(Mat inputFrame)
{
if (faceLocator != null) {
bool face_detected = faceLocator.detectFaces(inputFrame);
if (face_detected) {
runOnUiThread(new Runnable() {
@Override
public void run() {
myTextElement.setTextColor(Color.GREEN);
}
});
}
}
return inputFrame;
}
这篇关于安卓& OpenCV-用户界面更改时应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!