Android & OpenCV - App crashes on UI change

天涯浪子 提交于 2019-11-28 10:51:34

问题


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.

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;
    }
}

Can anyone help?

Here's the stack trace...

        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)

回答1:


Just solved it after two days... The stack trace pushed me in the right direction.

The problem was that the the method was running in a separate thread so I had to use runOnUiThread() to change the UI.

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;
}


来源:https://stackoverflow.com/questions/31828704/android-opencv-app-crashes-on-ui-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!