From 5d8bf86ea583fbc61ed331f10e075fc1c0d7ec96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Ramos=20Ib=C3=A1=C3=B1ez?= Date: Wed, 5 Jul 2023 22:14:33 +0200 Subject: [PATCH] Use video stride correctly in windows --- .../CaptureManagerFrameGrabberSession.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/eduramiba/webcamcapture/drivers/capturemanager/CaptureManagerFrameGrabberSession.java b/src/main/java/com/github/eduramiba/webcamcapture/drivers/capturemanager/CaptureManagerFrameGrabberSession.java index c22c511..1a5ad76 100644 --- a/src/main/java/com/github/eduramiba/webcamcapture/drivers/capturemanager/CaptureManagerFrameGrabberSession.java +++ b/src/main/java/com/github/eduramiba/webcamcapture/drivers/capturemanager/CaptureManagerFrameGrabberSession.java @@ -36,6 +36,7 @@ public class CaptureManagerFrameGrabberSession { private ISession session = null; private int videoWidth = -1; private int videoHeight = -1; + private int bytesPerRow = -1; private int bufferSizeBytes = -1; private long lastFrameTimestamp = -1; @@ -126,12 +127,12 @@ public class CaptureManagerFrameGrabberSession { final String videoFormat = MFVideoFormat_RGB32; - final int stride = CaptureManager.getInstance().getICaptureManagerControl().getStrideForBitmapInfoHeader( + this.bytesPerRow = Math.abs(CaptureManager.getInstance().getICaptureManagerControl().getStrideForBitmapInfoHeader( videoFormat, videoWidth - ); + )); - bufferSizeBytes = Math.abs(stride) * videoWidth; + bufferSizeBytes = bytesPerRow * videoHeight; directBuffer = ByteBuffer.allocateDirect(bufferSizeBytes); arrayByteBuffer = new byte[bufferSizeBytes]; @@ -139,7 +140,7 @@ public class CaptureManagerFrameGrabberSession { bufferedImage = new BufferedImage(videoWidth, videoHeight, BufferedImage.TYPE_INT_BGR); sampleModel = new ComponentSampleModel( - DataBuffer.TYPE_BYTE, videoWidth, videoHeight, 4, videoWidth * 4, + DataBuffer.TYPE_BYTE, videoWidth, videoHeight, 4, bytesPerRow, new int[]{2, 1, 0} // Try {1,2,3}, {3,2,1}, {0,1,2} ); @@ -279,7 +280,7 @@ public class CaptureManagerFrameGrabberSession { byteBuffer.mark(); byteBuffer.position(0); - pw.setPixels(0, 0, videoWidth, videoHeight, PixelFormat.getByteBgraPreInstance(), byteBuffer, 4 * videoWidth); + pw.setPixels(0, 0, videoWidth, videoHeight, PixelFormat.getByteBgraPreInstance(), byteBuffer, bytesPerRow); byteBuffer.reset(); }