Adding driver based in nokhwa library

This commit is contained in:
Eduardo Ramos 2024-11-25 13:35:35 +01:00
parent 06df6a1856
commit 01a9eb028d
3 changed files with 21 additions and 16 deletions

View File

@ -251,32 +251,37 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
} }
private void updateBuffer() { private void updateBuffer() {
final int hasFrameResult = LibNokhwa.INSTANCE.cnokhwa_has_new_frame(deviceIndex); if (imgBuffer == null) {
final int hasFrameResult = LibNokhwa.INSTANCE.cnokhwa_has_new_frame(deviceIndex);
if (hasFrameResult == RESULT_YES) { if (hasFrameResult == RESULT_YES) {
if (imgBuffer == null) {
// Init buffer if still not initialized: // Init buffer if still not initialized:
this.bytesPerRow = LibNokhwa.INSTANCE.cnokhwa_frame_bytes_per_row(deviceIndex); this.bytesPerRow = LibNokhwa.INSTANCE.cnokhwa_frame_bytes_per_row(deviceIndex);
final var bufferSizeBytes = bytesPerRow * resolution.height; final var bufferSizeBytes = bytesPerRow * resolution.height;
this.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes); this.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes);
this.arrayByteBuffer = new byte[imgBuffer.capacity()]; this.arrayByteBuffer = new byte[imgBuffer.capacity()];
}
final int grabResult = LibNokhwa.INSTANCE.cnokhwa_grab_frame( doGrab();
deviceIndex,
Native.getDirectBufferPointer(imgBuffer), imgBuffer.capacity()
);
if (grabResult == RESULT_OK) {
lastFrameTimestamp = System.currentTimeMillis();
} else { } else {
LOG.error("Error grabbing frame = {}", grabResult); if (hasFrameResult != RESULT_NO) {
LOG.error("Error checking for new frame = {}", hasFrameResult);
}
} }
} else { } else {
if (hasFrameResult != RESULT_NO) { doGrab();
LOG.error("Error checking for new frame = {}", hasFrameResult); }
} }
private void doGrab() {
final int grabResult = LibNokhwa.INSTANCE.cnokhwa_grab_frame(
deviceIndex,
Native.getDirectBufferPointer(imgBuffer), imgBuffer.capacity()
);
if (grabResult == RESULT_OK) {
lastFrameTimestamp = System.currentTimeMillis();
} else if (grabResult != ERROR_NO_FRAME_AVAILABLE){
LOG.error("Error grabbing frame = {}", grabResult);
} }
} }