From e635275ca5a56d3597efbb3332ebe368cad6597b Mon Sep 17 00:00:00 2001 From: Zjaun Date: Tue, 3 Oct 2023 18:47:09 +0800 Subject: [PATCH 1/2] Enhancement for setFormat() --- .../javacv/VideoInputFrameGrabber.java | 75 +++++++++++++------ 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java b/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java index e42f8885..91e56a8e 100644 --- a/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java +++ b/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java @@ -105,6 +105,58 @@ public void start() throws Exception { } public void start(int connection) throws Exception { myVideoInput = new videoInput(); + if (format != null && format.length() > 0) { + String formats[] = {"PAL_B", "PAL_D", "PAL_G", "PAL_H", "PAL_I", "PAL_M", "PAL_N", "PAL_NC", + "SECAM_B", "SECAM_D", "SECAM_G", "SECAM_H", "SECAM_K", "SECAM_K1", "SECAM_L", + "NTSC_M", "NTSC_M_J", "NTSC_433", }; + String subTypes[] = {"RGB24", "RGB32", "RGB555", "RGB565", "YUY2", "YVYU", "YUYV", "IYUV", "UYVY", + "YV12", "YVU9", "Y411", "Y41P", "Y211", "AYUV", "Y800", "Y8", "GREY", "MJPG"}; + boolean settingFormat = Arrays.asList(formats).contains(format); + boolean settingSubType = Arrays.asList(subTypes).contains(format); + if (settingFormat) { + int f = format.equals("NTSC_M") ? VI_NTSC_M : + format.equals("PAL_B") ? VI_PAL_B : + format.equals("PAL_D") ? VI_PAL_D : + format.equals("PAL_G") ? VI_PAL_G : + format.equals("PAL_H") ? VI_PAL_H : + format.equals("PAL_I") ? VI_PAL_I : + format.equals("PAL_M") ? VI_PAL_M : + format.equals("PAL_N") ? VI_PAL_N : + format.equals("PAL_NC") ? VI_PAL_NC : + format.equals("SECAM_B") ? VI_SECAM_B : + format.equals("SECAM_D") ? VI_SECAM_D : + format.equals("SECAM_G") ? VI_SECAM_G : + format.equals("SECAM_H") ? VI_SECAM_H : + format.equals("SECAM_K") ? VI_SECAM_K : + format.equals("SECAM_K1") ? VI_SECAM_K1 : + format.equals("SECAM_L") ? VI_SECAM_L : + format.equals("NTSC_M_J") ? VI_NTSC_M_J : + format.equals("NTSC_433") ? VI_NTSC_433 : -1; + myVideoInput.setFormat(deviceNumber, f); + if (f >= 0 && !myVideoInput.setFormat(deviceNumber, f)) { + throw new Exception("videoInput.setFormat() Error: Could not set format " + format + "."); + } + } else if (settingSubType) { + int s = format.equals("RGB24") ? VI_MEDIASUBTYPE_RGB24 : + format.equals("RGB32") ? VI_MEDIASUBTYPE_RGB32 : + format.equals("RGB555") ? VI_MEDIASUBTYPE_RGB555 : + format.equals("YUY2") ? VI_MEDIASUBTYPE_YUY2 : + format.equals("YVYU") ? VI_MEDIASUBTYPE_YVYU : + format.equals("YUYV") ? VI_MEDIASUBTYPE_YUYV : + format.equals("IYUV") ? VI_MEDIASUBTYPE_IYUV : + format.equals("UYVY") ? VI_MEDIASUBTYPE_UYVY : + format.equals("YV12") ? VI_MEDIASUBTYPE_YV12 : + format.equals("YVU9") ? VI_MEDIASUBTYPE_YVU9 : + format.equals("Y411") ? VI_MEDIASUBTYPE_Y411 : + format.equals("Y41P") ? VI_MEDIASUBTYPE_Y41P : + format.equals("Y211") ? VI_MEDIASUBTYPE_Y211 : + format.equals("AYUV") ? VI_MEDIASUBTYPE_AYUV : + format.equals("Y800") ? VI_MEDIASUBTYPE_Y800 : + format.equals("Y8") ? VI_MEDIASUBTYPE_Y8 : + format.equals("GREY") ? VI_MEDIASUBTYPE_GREY : + format.equals("MJPG") ? VI_MEDIASUBTYPE_MJPG : -1; + myVideoInput.setRequestedMediaSubType(s); + } if (frameRate > 0) { myVideoInput.setIdealFramerate(deviceNumber, (int)frameRate); } @@ -113,29 +165,6 @@ public void start(int connection) throws Exception { myVideoInput = null; throw new Exception("videoInput.setupDevice() Error: Could not setup device."); } - if (format != null && format.length() > 0) { - int f = format.equals("VI_NTSC_M") ? VI_NTSC_M : - format.equals("VI_PAL_B") ? VI_PAL_B : - format.equals("VI_PAL_D") ? VI_PAL_D : - format.equals("VI_PAL_G") ? VI_PAL_G : - format.equals("VI_PAL_H") ? VI_PAL_H : - format.equals("VI_PAL_I") ? VI_PAL_I : - format.equals("VI_PAL_M") ? VI_PAL_M : - format.equals("VI_PAL_N") ? VI_PAL_N : - format.equals("VI_PAL_NC") ? VI_PAL_NC : - format.equals("VI_SECAM_B") ? VI_SECAM_B : - format.equals("VI_SECAM_D") ? VI_SECAM_D : - format.equals("VI_SECAM_G") ? VI_SECAM_G : - format.equals("VI_SECAM_H") ? VI_SECAM_H : - format.equals("VI_SECAM_K") ? VI_SECAM_K : - format.equals("VI_SECAM_K1") ? VI_SECAM_K1 : - format.equals("VI_SECAM_L") ? VI_SECAM_L : - format.equals("VI_NTSC_M_J") ? VI_NTSC_M_J : - format.equals("VI_NTSC_433") ? VI_NTSC_433 : -1; - if (f >= 0 && !myVideoInput.setFormat(deviceNumber, f)) { - throw new Exception("videoInput.setFormat() Error: Could not set format " + format + "."); - } - } } public void stop() throws Exception { From 5ca3699ecd9147eb8a56eaaf5ddab5469f0e8ce8 Mon Sep 17 00:00:00 2001 From: Zjaun <91415509+Zjaun@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:54:09 +0800 Subject: [PATCH 2/2] Update VideoInputFrameGrabber.java --- .../javacv/VideoInputFrameGrabber.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java b/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java index 91e56a8e..7a86f618 100644 --- a/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java +++ b/src/main/java/org/bytedeco/javacv/VideoInputFrameGrabber.java @@ -106,22 +106,22 @@ public void start() throws Exception { public void start(int connection) throws Exception { myVideoInput = new videoInput(); if (format != null && format.length() > 0) { - String formats[] = {"PAL_B", "PAL_D", "PAL_G", "PAL_H", "PAL_I", "PAL_M", "PAL_N", "PAL_NC", - "SECAM_B", "SECAM_D", "SECAM_G", "SECAM_H", "SECAM_K", "SECAM_K1", "SECAM_L", - "NTSC_M", "NTSC_M_J", "NTSC_433", }; - String subTypes[] = {"RGB24", "RGB32", "RGB555", "RGB565", "YUY2", "YVYU", "YUYV", "IYUV", "UYVY", - "YV12", "YVU9", "Y411", "Y41P", "Y211", "AYUV", "Y800", "Y8", "GREY", "MJPG"}; - boolean settingFormat = Arrays.asList(formats).contains(format); - boolean settingSubType = Arrays.asList(subTypes).contains(format); - if (settingFormat) { - int f = format.equals("NTSC_M") ? VI_NTSC_M : - format.equals("PAL_B") ? VI_PAL_B : - format.equals("PAL_D") ? VI_PAL_D : - format.equals("PAL_G") ? VI_PAL_G : - format.equals("PAL_H") ? VI_PAL_H : - format.equals("PAL_I") ? VI_PAL_I : - format.equals("PAL_M") ? VI_PAL_M : - format.equals("PAL_N") ? VI_PAL_N : + String formats[] = {"PAL_B", "PAL_D", "PAL_G", "PAL_H", "PAL_I", "PAL_M", "PAL_N", "PAL_NC", + "SECAM_B", "SECAM_D", "SECAM_G", "SECAM_H", "SECAM_K", "SECAM_K1", "SECAM_L", + "NTSC_M", "NTSC_M_J", "NTSC_433", }; + String subTypes[] = {"RGB24", "RGB32", "RGB555", "RGB565", "YUY2", "YVYU", "YUYV", "IYUV", "UYVY", + "YV12", "YVU9", "Y411", "Y41P", "Y211", "AYUV", "Y800", "Y8", "GREY", "MJPG"}; + boolean settingFormat = Arrays.asList(formats).contains(format); + boolean settingSubType = Arrays.asList(subTypes).contains(format); + if (settingFormat) { + int f = format.equals("NTSC_M") ? VI_NTSC_M : + format.equals("PAL_B") ? VI_PAL_B : + format.equals("PAL_D") ? VI_PAL_D : + format.equals("PAL_G") ? VI_PAL_G : + format.equals("PAL_H") ? VI_PAL_H : + format.equals("PAL_I") ? VI_PAL_I : + format.equals("PAL_M") ? VI_PAL_M : + format.equals("PAL_N") ? VI_PAL_N : format.equals("PAL_NC") ? VI_PAL_NC : format.equals("SECAM_B") ? VI_SECAM_B : format.equals("SECAM_D") ? VI_SECAM_D : @@ -133,8 +133,8 @@ public void start(int connection) throws Exception { format.equals("NTSC_M_J") ? VI_NTSC_M_J : format.equals("NTSC_433") ? VI_NTSC_433 : -1; myVideoInput.setFormat(deviceNumber, f); - if (f >= 0 && !myVideoInput.setFormat(deviceNumber, f)) { - throw new Exception("videoInput.setFormat() Error: Could not set format " + format + "."); + if (f >= 0 && !myVideoInput.setFormat(deviceNumber, f)) { + throw new Exception("videoInput.setFormat() Error: Could not set format " + format + "."); } } else if (settingSubType) { int s = format.equals("RGB24") ? VI_MEDIASUBTYPE_RGB24 : @@ -157,6 +157,7 @@ public void start(int connection) throws Exception { format.equals("MJPG") ? VI_MEDIASUBTYPE_MJPG : -1; myVideoInput.setRequestedMediaSubType(s); } + } if (frameRate > 0) { myVideoInput.setIdealFramerate(deviceNumber, (int)frameRate); }