Bug in Android samples [includes fix]. (Patch #2366)
Description
I came up with a bug with the Android samples, in particular the "Tutorial 1 - Add Opencv" but it should be the same for similar samples.
It happens when the sample activity goes to the background, for example when a call is received.
It does not happen when the home button is pressed.
What happens is that, when the activity gets focus again, the camera is frozen in the last frame.
This was tested with opencv 2.4.1 but I think it remains like this in 2.4.2.
The activity cycle when the home button is pressed is the following.
Cycle down:
onPause > releaseCamera > Surface destroyed > releaseCamera > onStop
Cycle up:
onRestart > onResume > openCamera > releaseCamera > surface created > surface changed > setup camera > "starting processing thread"
The activity cycle when it goes to the background is the following.
Cycle down:
onPause > releaseCamera
Cycle up:
onResume > openCamera > releaseCamera
Somehow the thread is not running properly and/or the camera is not setup properly.
I was able to fix it like this, it is not very elegant I think someone might came up with a better solution =)
Sample file DemoJava.java:
onResume() { // Add this at the end: // I have defined processingThread from DemoViewBase.java as protected so I access it here. if(null != mView && null != mView.processingThread) { //if(!mView.processingThread.isAlive()) // I tried to check this but it also freezes when thread isAlive, so I removed it. Log.d(TAG, "onResume: thread state: "+mView.processingThread.getState()); // outputs either WAITING or TERMINATED mView.forceThreadRestart(); mView.setupCamera(mView.surfaceWidth, mView.surfaceHeight); } }
Associated revisions
Merge pull request #2366 from yashdv:mat_copyto_doc_bug
History
Updated by Rui Marques over 12 years ago
I forgot to mention the forceThreadRestart code:
protected void forceThreadRestart() { if(processingThread!=null) { processingThread.interrupt(); //Seems to be the only non-deprecated way to somehow stop a thread. processingThread = null; } processingThread = new Thread(this); processingThread.start(); }
Updated by Andrey Pavlenko over 12 years ago
- Category changed from samples to android
- Assignee set to Andrey Pavlenko
- Start date deleted (
2012-09-18)
Updated by Kirill Kornyakov over 12 years ago
- Tracker changed from Bug to Patch
- Target version set to 2.4.3
Updated by Kirill Kornyakov over 12 years ago
- Target version deleted (
2.4.3) - Assignee changed from Andrey Pavlenko to Alexander Smorkalov
Updated by Alexander Smorkalov over 12 years ago
- Status changed from Open to Done
Updated by Andrey Kamaev about 12 years ago
- Target version set to 2.4.3