Skip to content

Commit

Permalink
Add settings for max camera preview resolution width
Browse files Browse the repository at this point in the history
  • Loading branch information
morckx committed Sep 26, 2020
1 parent b8adc1f commit f255584
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 17 deletions.
20 changes: 19 additions & 1 deletion app/src/main/java/de/visorapp/visor/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package de.visorapp.visor
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.DropDownPreference
import androidx.preference.PreferenceFragmentCompat
import kotlin.math.max


class SettingsActivity : AppCompatActivity() {

Expand All @@ -18,7 +21,7 @@ class SettingsActivity : AppCompatActivity() {
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.getItemId()) {
when (item.itemId) {
android.R.id.home -> {
super.onBackPressed()
return true
Expand All @@ -30,6 +33,21 @@ class SettingsActivity : AppCompatActivity() {
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
initPreviewResolutionWidth()
}

private fun initPreviewResolutionWidth() {
val visorSurface = VisorSurface.getInstance()
val availablePreviewWidths: Array<CharSequence>? = visorSurface.availablePreviewWidths
val previewResolutionPreference = findPreference<DropDownPreference>(resources.getString(R.string.key_preference_preview_resolution))
if (previewResolutionPreference != null && availablePreviewWidths != null) {
previewResolutionPreference.entries = availablePreviewWidths
previewResolutionPreference.entryValues = availablePreviewWidths
val currentPreviewWidth = visorSurface.cameraPreviewWidth
val currentIndex = max(0, availablePreviewWidths.indexOf(currentPreviewWidth.toString()))
previewResolutionPreference.setValueIndex(currentIndex)
}
}

}
}
43 changes: 29 additions & 14 deletions app/src/main/java/de/visorapp/visor/VisorSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import androidx.preference.PreferenceManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -78,15 +79,18 @@ public class VisorSurface extends SurfaceView implements SurfaceHolder.Callback,
public static final int STATE_PREVIEW = 2;

/**
* Max width for the camera preview to avoid performance and ram/cache issues.
* TODO should be configurable by a settings-activity! (feature)
* Max initial width for the camera preview to avoid performance and ram/cache issues.
* Afterwards the preview width can be selected from the available sizes in the settings activity.
*/

private static final int MAX_CAMERA_PREVIEW_RESOLUTION_WIDTH = 1024;
private static final int MAX_INITIAL_PREVIEW_RESOLUTION_WIDTH = 1280;
private final SharedPreferences mSharedPreferences;

private MediaActionSound mSound = null;

private static VisorSurface mInstance;

public CharSequence[] availablePreviewWidths;

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// super.onLayout(changed, left, top, right, bottom);
Expand Down Expand Up @@ -192,7 +196,7 @@ private void keepCameraAspectRatioInView(View child, int left, int top, int righ
/**
* stores the value of the devices max zoom level of the camera.
*/
private int mCameraMaxZoomLevel;
private int mCameraMaxZoomLevel = 1;

/**
* the width of the view.
Expand Down Expand Up @@ -331,11 +335,10 @@ public void onPreviewFrame(final byte[] data, Camera camera) {
*/
public VisorSurface(Context context) {
super(context);

mInstance = this;
Log.d(TAG, "VisorSurface instantiated");

mCameraCurrentZoomLevel = 0;
mCameraMaxZoomLevel = 0;
mCurrentColorFilterIndex = 0;

mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Expand Down Expand Up @@ -373,6 +376,10 @@ public VisorSurface(Context context) {
mHolder.addCallback(this);
}

public static VisorSurface getInstance() {
return mInstance;
}

/**
* open and return a camera instance.
*
Expand Down Expand Up @@ -453,6 +460,8 @@ public void surfaceDestroyed(SurfaceHolder holder) {
*/
private Camera.Size getBestPreviewSize(Camera.Parameters parameters) {
Camera.Size result = null;
final int UNINITIALIZED_WIDTH = -1;
int preferredPreviewWidth = Integer.parseInt(mSharedPreferences.getString(getResources().getString(R.string.key_preference_preview_resolution), Integer.toString(UNINITIALIZED_WIDTH)));

List<Camera.Size> size = parameters.getSupportedPreviewSizes();
Collections.sort(size, new Comparator<Camera.Size>() {
Expand All @@ -466,19 +475,21 @@ public int compare(Camera.Size lhs, Camera.Size rhs) {

if (size.size() <= 0) return null;

for (int i = (size.size() - 1); i >= 0; i--) {
ArrayList<String> availablePreviewWidths = new ArrayList<>();
for (int i = 0; i < size.size(); i++) {
Log.d(TAG, "Size: " + Integer.toString(size.get(i).width) + " * " + Integer.toString(size.get(i).height));

final int currentWidth = size.get(i).width;
if (currentWidth <= MAX_CAMERA_PREVIEW_RESOLUTION_WIDTH) {
if (i == 0 || currentWidth != size.get(i-1).width )
availablePreviewWidths.add(Integer.toString(currentWidth));
if (currentWidth == preferredPreviewWidth
|| (preferredPreviewWidth == UNINITIALIZED_WIDTH && currentWidth <= MAX_INITIAL_PREVIEW_RESOLUTION_WIDTH && (result == null || currentWidth > result.width))) {
result = size.get(i);
break;
}
}

// just use the last one, if there are only a few supported sizes.
if (result == null) return size.get(size.size() - 1);

if (result == null)
result = size.get(size.size() - 1);
this.availablePreviewWidths = availablePreviewWidths.toArray(new CharSequence[availablePreviewWidths.size()]);
Log.d(TAG, "got maximum preview size of " + Integer.toString(result.width) + "*" + Integer.toString(result.height));
return result;
}
Expand Down Expand Up @@ -1042,4 +1053,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
public void pausePreviewIfReady() {
mPauseOnReady = true;
}

public int getCameraPreviewWidth() {
return mCameraPreviewWidth;
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_settings_applications_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM17.25,12c0,0.23 -0.02,0.46 -0.05,0.68l1.48,1.16c0.13,0.11 0.17,0.3 0.08,0.45l-1.4,2.42c-0.09,0.15 -0.27,0.21 -0.43,0.15l-1.74,-0.7c-0.36,0.28 -0.76,0.51 -1.18,0.69l-0.26,1.85c-0.03,0.17 -0.18,0.3 -0.35,0.3h-2.8c-0.17,0 -0.32,-0.13 -0.35,-0.29l-0.26,-1.85c-0.43,-0.18 -0.82,-0.41 -1.18,-0.69l-1.74,0.7c-0.16,0.06 -0.34,0 -0.43,-0.15l-1.4,-2.42c-0.09,-0.15 -0.05,-0.34 0.08,-0.45l1.48,-1.16c-0.03,-0.23 -0.05,-0.46 -0.05,-0.69 0,-0.23 0.02,-0.46 0.05,-0.68l-1.48,-1.16c-0.13,-0.11 -0.17,-0.3 -0.08,-0.45l1.4,-2.42c0.09,-0.15 0.27,-0.21 0.43,-0.15l1.74,0.7c0.36,-0.28 0.76,-0.51 1.18,-0.69l0.26,-1.85c0.03,-0.17 0.18,-0.3 0.35,-0.3h2.8c0.17,0 0.32,0.13 0.35,0.29l0.26,1.85c0.43,0.18 0.82,0.41 1.18,0.69l1.74,-0.7c0.16,-0.06 0.34,0 0.43,0.15l1.4,2.42c0.09,0.15 0.05,0.34 -0.08,0.45l-1.48,1.16c0.03,0.23 0.05,0.46 0.05,0.69z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
<string name="focus_sound_title">Nach Autofokus</string>
<string name="shutter_sound_title">Beim Screenshot</string>

<!-- Lighting Preferences -->
<string name="max_brightness_title">Maximale Displayhelligkeit</string>
<string name="auto_torch_title">Lampe beim Start einschalten</string>

<!-- Advanced Preferences -->
<string name="advanced_header">Erweitert</string>
<string name="preview_resolution_width">X-Auflösung der Vorschau</string>

</resources>
17 changes: 17 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="preview_resolution_entries">
<item>320</item>
<item>640</item>
<item>1024</item>
<item>1280</item>
</string-array>

<string-array name="preview_resolution_entry_values">
<item>320</item>
<item>640</item>
<item>1024</item>
<item>1280</item>
</string-array>

</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<string name="text_image_stored">Image stored in path </string>

<!-- Key names (equal in all languages) -->
<string name="visor_shared_preference_name" translatable="false">VisorAndroidSharedPreference</string>
<string name="key_preference_zoom_level" translatable="false">VisorAndroidKeyPreferenceZoomLevel</string>
<string name="key_preference_color_mode" translatable="false">VisorAndroidKeyPreferenceColorMode</string>
<string name="key_preference_autofocus_mode" translatable="false">VisorAndroidKeyPreferenceAutofocusMode</string>
Expand All @@ -19,6 +18,7 @@
<string name="key_preference_shutter_sound" translatable="false">VisorAndroidKeyPreferenceShutterSound</string>
<string name="key_preference_max_brightness" translatable="false">VisorAndroidKeyPreferenceMaxBrightness</string>
<string name="key_preference_auto_torch" translatable="false">VisorAndroidKeyPreferenceAutoTorch</string>
<string name="key_preference_preview_resolution" translatable="false">VisorAndroidKeyPreferencePreviewResolution</string>


<!-- Preference Titles -->
Expand All @@ -33,4 +33,8 @@
<!-- Lighting Preferences -->
<string name="max_brightness_title">Maximum display brightness</string>
<string name="auto_torch_title">Turn on flashlight on start</string>

<!-- Advanced Preferences -->
<string name="advanced_header">Advanced</string>
<string name="preview_resolution_width">Preview resolution width</string>
</resources>
12 changes: 11 additions & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,15 @@

</PreferenceCategory>

<PreferenceCategory app:title="@string/advanced_header"
app:icon="@drawable/ic_baseline_settings_applications_24">
<DropDownPreference
app:defaultValue="3"
app:entries="@array/preview_resolution_entries"
app:entryValues="@array/preview_resolution_entry_values"
app:key="@string/key_preference_preview_resolution"
app:title="@string/preview_resolution_width"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

</PreferenceScreen>
</PreferenceScreen>

0 comments on commit f255584

Please sign in to comment.