From 4c0d5796368dd81c065daa393ae4c82ac58052a0 Mon Sep 17 00:00:00 2001 From: Evgeniy Isaev Date: Sat, 11 Mar 2023 16:42:38 +0200 Subject: [PATCH] MainService: crop image if padding is present --- README.md | 1 + .../christianbeier/droidvnc_ng/Constants.java | 1 + .../christianbeier/droidvnc_ng/Defaults.kt | 5 ++++ .../droidvnc_ng/MainActivity.java | 8 ++++++ .../droidvnc_ng/MainService.java | 24 ++++++++++++++-- app/src/main/res/layout/activity_main.xml | 28 +++++++++++++++++++ app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 67 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff3ed739..c76fe5bf 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ An example `defaults.json` with completely new defaults (not all entries need to "portRepeater": 5556, "scaling": 0.7, "password": "supersecure" + "removePadding": true } ``` diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/Constants.java b/app/src/main/java/net/christianbeier/droidvnc_ng/Constants.java index fd1940f3..91905559 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/Constants.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/Constants.java @@ -26,6 +26,7 @@ public class Constants { public static final String PREFS_KEY_SETTINGS_PASSWORD = "settings_password" ; public static final String PREFS_KEY_SETTINGS_START_ON_BOOT = "settings_start_on_boot" ; public static final String PREFS_KEY_SETTINGS_SCALING = "settings_scaling" ; + public static final String PREFS_KEY_SETTINGS_REMOVE_PADDING = "settings_remove_padding"; public static final String PREFS_KEY_REVERSE_VNC_LAST_HOST = "reverse_vnc_last_host" ; public static final String PREFS_KEY_REPEATER_VNC_LAST_HOST = "repeater_vnc_last_host" ; public static final String PREFS_KEY_REPEATER_VNC_LAST_ID = "repeater_vnc_last_id" ; diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt b/app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt index 8af5bd4c..c92f0b7e 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt @@ -54,6 +54,10 @@ class Defaults { @EncodeDefault var password = "" private set + + @EncodeDefault + var removePadding = false + private set /* NB if adding fields here, don't forget to add their copying in the constructor as well! */ @@ -68,6 +72,7 @@ class Defaults { this.portRepeater = readDefault.portRepeater this.scaling = readDefault.scaling this.password = readDefault.password + this.removePadding = readDefault.removePadding // add here! } catch (e: Exception) { Log.w(TAG, "${e.message}") diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java b/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java index 52daa5ad..29b7bb23 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java @@ -287,6 +287,14 @@ public void afterTextChanged(Editable editable) { ed.apply(); }); + final SwitchMaterial removePadding = findViewById(R.id.settings_remove_padding); + removePadding.setChecked(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_REMOVE_PADDING, mDefaults.getRemovePadding())); + removePadding.setOnCheckedChangeListener((compoundButton, b) -> { + SharedPreferences.Editor ed = prefs.edit(); + ed.putBoolean(Constants.PREFS_KEY_SETTINGS_REMOVE_PADDING, b); + ed.apply(); + }); + TextView about = findViewById(R.id.about); about.setText(getString(R.string.main_activity_about, BuildConfig.VERSION_NAME)); diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java b/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java index de538a2e..5c7c2ac9 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java @@ -335,6 +335,9 @@ private void startScreenCapture() { int scaledWidth = (int) (metrics.widthPixels * scaling); int scaledHeight = (int) (metrics.heightPixels * scaling); + // remove padding + boolean removePadding = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFS_KEY_SETTINGS_REMOVE_PADDING, mDefaults.getRemovePadding()); + // only set this by detecting quirky hardware if the user has not set manually if(!mHasPortraitInLandscapeWorkaroundSet && Build.FINGERPRINT.contains("rk3288") && metrics.widthPixels > 800) { Log.w(TAG, "detected >10in rk3288 applying workaround for portrait-in-landscape quirk"); @@ -419,11 +422,28 @@ private void startScreenCapture() { int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * scaledWidth; - int w = scaledWidth + rowPadding / pixelStride; + int w = scaledWidth; + + // Crop + if (removePadding && rowPadding > 0) { + int rowPaddingPx = rowPadding / pixelStride; + Bitmap dest = Bitmap.createBitmap(scaledWidth + rowPaddingPx, scaledHeight, Bitmap.Config.ARGB_8888); + + buffer.rewind(); + dest.copyPixelsFromBuffer(buffer); + + Bitmap croppedDest = Bitmap.createBitmap(dest, 0, 0, scaledWidth, scaledHeight); + + buffer.rewind(); + croppedDest.copyPixelsToBuffer(buffer); + } else { + w += rowPadding / pixelStride; + } // if needed, setup a new VNC framebuffer that matches the image plane's parameters - if (w != vncGetFramebufferWidth() || scaledHeight != vncGetFramebufferHeight()) + if (w != vncGetFramebufferWidth() || scaledHeight != vncGetFramebufferHeight()) { vncNewFramebuffer(w, scaledHeight); + } buffer.rewind(); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b2b95819..d7e07d0e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -142,6 +142,34 @@ + + + + + + + + + + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 0a70fae2..022bba21 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -6,6 +6,7 @@ 密码 开机启动 缩放 + 删除填充 权限仪表盘 截屏 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9708578a..1f06e17b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ Password Start on Boot Scaling + Remove padding Permissions Dashboard : Screen Capturing