Fixed some bugs on the image monitor class.

This commit is contained in:
2013-12-03 16:24:59 -04:30
parent 3696a9cfd4
commit ebbacf4feb
2 changed files with 10 additions and 9 deletions

View File

@@ -6,13 +6,15 @@ public class CameraImageMonitor{
private final String TAG = "CAM_MONITOR"; private final String TAG = "CAM_MONITOR";
private final String CLASS_NAME = CameraImageMonitor.class.getSimpleName(); private final String CLASS_NAME = CameraImageMonitor.class.getSimpleName();
private Object imageMonitor;
private byte[] image; private byte[] image;
private boolean imgProduced; private boolean imgProduced;
private boolean imgConsumed; private boolean imgConsumed;
private CameraImageMonitor(){ private CameraImageMonitor(){
imgProduced = false; imgProduced = false;
imgConsumed = false; imgConsumed = true;
imageMonitor = new Object();
} }
private static class SingletonHolder{ private static class SingletonHolder{
@@ -26,13 +28,13 @@ public class CameraImageMonitor{
public void setImageData(byte[] image){ public void setImageData(byte[] image){
if(imgConsumed){ if(imgConsumed){
Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Copying new image."); Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Copying new image.");
synchronized(this.image){ synchronized(this.imageMonitor){
//this.image = new byte[image.length]; //this.image = new byte[image.length];
//System.arraycopy(image, 0, this.image, 0, image.length); //System.arraycopy(image, 0, this.image, 0, image.length);
this.image = image; this.image = image;
imgProduced = true; imgProduced = true;
imgConsumed = false; imgConsumed = false;
this.image.notifyAll(); this.imageMonitor.notifyAll();
} }
Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Data copy finished."); Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Data copy finished.");
}else{ }else{
@@ -43,10 +45,10 @@ public class CameraImageMonitor{
public byte[] getImageData(){ public byte[] getImageData(){
byte[] returnImg; byte[] returnImg;
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Entry point."); Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Entry point.");
synchronized(image){ synchronized(imageMonitor){
while(!imgProduced){ while(!imgProduced){
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Waiting for new image."); Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Waiting for new image.");
try{ image.wait(); }catch(InterruptedException ie){ } try{ imageMonitor.wait(); }catch(InterruptedException ie){ }
} }
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Retrieving new image."); Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Retrieving new image.");
returnImg = image; returnImg = image;
@@ -58,6 +60,6 @@ public class CameraImageMonitor{
} }
public synchronized boolean hasChanged(){ public synchronized boolean hasChanged(){
return imgProduced; return imgConsumed;
} }
} }

View File

@@ -117,9 +117,8 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
@Override @Override
public void onPreviewFrame(byte[] data, Camera camera){ public void onPreviewFrame(byte[] data, Camera camera){
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received"); Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received");
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (imgMonitor.hasChanged() ? "" : " not") + " changed."); Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (imgMonitor.hasChanged() ? "" : " not") + " been consumed.");
if(!imgMonitor.hasChanged()) imgMonitor.setImageData(data);
imgMonitor.setImageData(data);
} }
public void removePreviewCallback(){ public void removePreviewCallback(){