When we retrieve a frame from FrameGrabber, we get a reference to the frame. The frame is added to a list. However, due to the fact that we are only referencing to the frame, all the objects in the list points to the same reference.
Since I don't have access to neither object Frame, or FrameGrabber I cannot make 'em cloneable or serializable. Hence no copying.
I believe that since we create a new object Frame each round, we get a new reference, rigth?
Frame frame = new Frame();
However, the object frame itself is a reference to:
frame = mFrameGrabber.grabFrame();
So the culprit is the mFrameGrabber, which returns the same reference every time.. Help me solve this.
Short: Capture a frame (object) and store the content (not reference) in an arraylist of type object.
private FrameGrabber mFrameGrabber;
List<Frame> frameBuffer = new ArrayList<Frame>();
public void run() {
// Run forever
while ( true )
{
Frame frame = new Frame();
// Time to capture a frame ?
if ( captureTimer.hasTimerElapsed() == true )
{
try
{
// Grab a frame
frame = mFrameGrabber.grabFrame();
if ( frameWindow.isVisible() && frame != null)
{
frameBuffer.add(frame);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Classes are implemented from javaCV, from a jar file.
Edit: Okey. The frame object has objects itselfs. I've noticed that the only object I need to copy is a object of type buffer[]. Call it image.
I can clone image, gives me a new reference. However, the content within image seems still to be the same lol. So this is still not a deep copy .. Will try serialize aswell.
private Frame copyFrame(Frame frame)
{
// Frame that will hold the copy
Frame cFrame = new Frame(640, 480, 8, 3);
cFrame.image = frame.image.clone();
return cFrame;
}