Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

java - Android & OpenCV - App crashes on UI change

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...