Skip to content

Commit

Permalink
MainService: crop image if padding is present
Browse files Browse the repository at this point in the history
  • Loading branch information
eisaev authored and bk138 committed Jun 11, 2023
1 parent 63b79c8 commit 4c0d579
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ;
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();

Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:padding="10dp"
android:text="@string/main_activity_settings_remove_padding" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:padding="10dp"
android:text="@string/main_activity_colon" />

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/settings_remove_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="" />

</TableRow>

</TableLayout>


Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="main_activity_settings_password">密码</string>
<string name="main_activity_settings_start_on_boot">开机启动</string>
<string name="main_activity_settings_scaling">缩放</string>
<string name="main_activity_settings_remove_padding">删除填充</string>
<string name="main_activity_permissions_dashboard">权限仪表盘</string>
<string name="main_activity_colon">:</string>
<string name="main_activity_screen_capturing">截屏</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="main_activity_settings_password">Password</string>
<string name="main_activity_settings_start_on_boot">Start on Boot</string>
<string name="main_activity_settings_scaling">Scaling</string>
<string name="main_activity_settings_remove_padding">Remove padding</string>
<string name="main_activity_permissions_dashboard">Permissions Dashboard</string>
<string name="main_activity_colon">:</string>
<string name="main_activity_screen_capturing">Screen Capturing</string>
Expand Down

0 comments on commit 4c0d579

Please sign in to comment.