Reputation: 1026
In my Android project with camera2 interface, I used AImage_getTimestamp()
to get the timestamp of each picture. But this timestamp is different from my system time got with clock_gettime()
. But I truly need to use these two time together to get some work.
I could understand these two times are within different architectures, but can I convert them to each other? Or to say, can I get the system time when a camera picture's timestamp is created?
For example, camera timestamps are discrete as ts0, ts1, ts2, and ts3. In my code, I want to get a real-time timestamp ts0' at the time some code is executed, which is not exactly ts0 but ts0 with a time margin added. How can I get this ts0'?
Upvotes: 1
Views: 1356
Reputation: 51
Your device supports REALTIME camera timestamps. As stated before, your camera timestamps are in the same coordinate system as the elapsed realtime since boot. In your ImageReader.OnImageAvailableListener you can use the following code:
long imageReaderTime = SystemClock.elapsedRealtimeNanos();
Image image = reader.acquireLatestImage();
long cameraTime = image.getTimestamp();
Both timestamps can be compared, and 'imageReaderTime' (the one you are interested in) is slightly larger than 'cameraTime', because the onImageAvailable callback takes a small amount of time to inform you about the new image. This also introduces a slight variance.
Eddy Talvala's answer should be accepted.
Upvotes: 1
Reputation: 18137
In general, depending on the value of TIMESTAMP_SOURCE, the camera timestamps are either based on CLOCK_MONOTONIC or CLOCK_BOOTTIME.
MONOTONIC is used when SOURCE is UNKNOWN; but there's no guarantee on the quality of synchronization between the camera timestamps and the rest of the system; that is, the actual start of exposure time may be off by quite a few milliseconds.
REALTIME means the timestamps are in the CLOCK_BOOTTIME time base (in the Java SDK, that matches to SystemClock#elapsedRealtimeNanos. REALTIME timestamps are going to be closely synchronized, so that they can be combined with inertial sensor data (gyros, etc) accurately.
I don't know which kinds of timestamps you're reading from clock_gettime, but the easiest option would be to match the camera's source. Otherwise, you need to determine the offset between the time you're using and what the camera is using. The offset won't change during a single camera session, so you can just measure it once at start of camera use.
Upvotes: 0