Best way/place to get timestamp on Android?

asked 2013-12-05 10:35:35 -0500

scorpeeon gravatar image

updated 2013-12-05 11:12:37 -0500

So I have an Android application that is doing real-time image processing using native OpenCV through JNI. Getting very precise timestamps is critical in my application because the app is tracking an object and the data (position and timestamp) from multiple devices running the app could be used to triangulate the 3d position of the tracked object. So there is an OpenCV method which can be used to get a quite precise timestamp, it's called Core.getTickCount(). So the main question is when to call this getTickCount() method.

When taking a picture, the OnShutter() method of the Android camera would be a proper way to do this (as far as i understand), but as far as i understand this is not available on preview images, only after taking pictures. Since the app is doing fast real-time object tracking, using the preview images would be the preferable way to go (though I'm considering taking photos every time just to be able to use the onShutter() event for timestamp, but that would probably slow down the application quite a bit - I'll experiment with what later and see how it goes).

In OpenCV for Android (as far as I know) the preferred way to get the camera preview image (to OpenCV) is through the onCameraFrame(CvCameraViewFrame inputFrame) method of the CvCameraViewListener2 interface. The problem is that this is called only after Android receives the preview in onPreviewFrame(byte[] frame, Camera arg1) method (in PreviewCallback) (which is implemented in OpenCV in the JavaCameraView) which then notifies a CameraWorker running in a separate thread which can take same time adding further to the delay.

And even the onPreviewFrame method is said to be "Called as preview frames are displayed." so it might have a significant delay compared to the real actual time of the preview picture being taken - though it's probably much closer to the real time of the image then the onCameraFrame method. Also if I get the image in onCameraFrame method, I can't really seem to be able to use even the onPreviewFrame method to get the timestamp because it is not guaranteed that the onCameraFrame method belongs to the same preview image as the last called onPreviewFrame, beacause as I already mentioned that is running in a separate thread which might not always get notified (more details here).

So what do you think I should do to get the most precise timestamps for my preview images? Is there any other simple way to do this that I'm missing? Any help is appreciated.

edit retag flag offensive close merge delete