Skip to content

Commit

Permalink
feat: Add FillWithCrop option, refactor camera preview aspect ratio m…
Browse files Browse the repository at this point in the history
…ode in the demo-app and core library
  • Loading branch information
VincentKobz committed Jun 6, 2024
1 parent 044e344 commit fbebe3e
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,44 @@ public class SettingsActivity extends AppCompatActivity {
protected MaterialToolbar topAppBar;

/**
* Select all segmented button
* Select all segmented providers button
*/
protected MaterialButton bt_all;

/**
* Select specific segmented button
* Select specific segmented providers button
*/
protected MaterialButton bt_spec;

/**
* Select none segmented button
* Select none segmented providers button
*/
protected MaterialButton bt_none;

/**
* SegmentedButtons state
* SegmentedButtons providers state
*/
protected int segmentedButtonState = 0;
protected int segmentedButtonProvidersState = 0;

/**
* Select stretch segmented aspect ratio mode button
*/
protected MaterialButton bt_stretch;

/**
* Select black bars segmented aspect ratio mode button
*/
protected MaterialButton bt_black_bars;

/**
* Select crop segmented aspect ratio mode button
*/
protected MaterialButton bt_crop;

/**
* SegmentedButtons aspect ratio mode state
*/
protected int aspectRatioMode = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -95,13 +115,19 @@ protected void onCreate(Bundle savedInstanceState) {
buttonSave = findViewById(R.id.button_save);
buttonSave.setOnClickListener(this::onClickSave);

// Segmented toggle buttons
// Segmented providers toggle buttons
bt_all = findViewById(R.id.button_all);
bt_spec = findViewById(R.id.button_specific);
bt_none = findViewById(R.id.button_none);

// Segmented aspect ratio mode toggle buttons
bt_stretch = findViewById(R.id.button_fill_stretch);
bt_black_bars = findViewById(R.id.button_fill_black_bars);
bt_crop = findViewById(R.id.button_fill_crop);

// Add listener to segmented toggle buttons
bindToggleButton();
bindToggleButtonProviders();
bindToggleButtonAspectRatioMode();

final SharedPreferences preferences = this.getSharedPreferences("ScannerSearchPreferences", MODE_PRIVATE);

Expand All @@ -115,12 +141,12 @@ protected void onCreate(Bundle savedInstanceState) {
((MaterialSwitch) findViewById(R.id.switchIntentDevices)).setChecked(preferences.getBoolean(ScannerServiceApi.EXTRA_SEARCH_ALLOW_INTENT_BOOLEAN, options.allowIntentDevices));
((MaterialSwitch) findViewById(R.id.switchEnableLogging)).setChecked(preferences.getBoolean(ENABLE_LOGGING_KEY, false));
((MaterialSwitch) findViewById(R.id.switchAllowCameraFallback)).setChecked(preferences.getBoolean(ALLOW_CAMERA_FALLBACK_KEY, false));
((MaterialSwitch) findViewById(R.id.switchEnableKeepAspectRatio)).setChecked(preferences.getBoolean(ENABLE_KEEP_ASPECT_RATIO_KEY, false));

final Set<String> allowedProviderKeys = preferences.getStringSet(ScannerServiceApi.EXTRA_SEARCH_ALLOWED_PROVIDERS_STRING_ARRAY, Collections.emptySet());

// Set the state of the segmented button
segmentedButtonState = preferences.getInt(PREFS_KEY, 0);
segmentedButtonProvidersState = preferences.getInt(PREFS_KEY, 0);
aspectRatioMode = preferences.getInt(ENABLE_KEEP_ASPECT_RATIO_KEY, 0);

final Set<BarcodeType> symbologySelection = new HashSet<>();
for(String symbology: preferences.getStringSet(ScannerServiceApi.EXTRA_SYMBOLOGY_SELECTION, ScannerService.defaultSymbologyByName())) {
Expand Down Expand Up @@ -169,7 +195,8 @@ protected void onCreate(Bundle savedInstanceState) {
}

// Set the state of the segmented button (default is all)
setSegmentedButtonState();
setSegmentedButtonState(true);
setSegmentedButtonState(false);
}

private void updateConstraintLayout(int topViewId) {
Expand Down Expand Up @@ -250,7 +277,6 @@ public void onClickSave(View v) {
editor.putBoolean(ScannerServiceApi.EXTRA_SEARCH_ALLOW_INTENT_BOOLEAN, ((MaterialSwitch) findViewById(R.id.switchIntentDevices)).isChecked());
editor.putBoolean(ENABLE_LOGGING_KEY, ((MaterialSwitch) findViewById(R.id.switchEnableLogging)).isChecked());
editor.putBoolean(ALLOW_CAMERA_FALLBACK_KEY, ((MaterialSwitch) findViewById(R.id.switchAllowCameraFallback)).isChecked());
editor.putBoolean(ENABLE_KEEP_ASPECT_RATIO_KEY, ((MaterialSwitch) findViewById(R.id.switchEnableKeepAspectRatio)).isChecked());

final Set<String> allowedProviderKeys = new HashSet<>();
final Set<String> excludedProviderKeys = new HashSet<>();
Expand Down Expand Up @@ -278,7 +304,8 @@ public void onClickSave(View v) {
editor.putStringSet(ScannerServiceApi.EXTRA_SEARCH_ALLOWED_PROVIDERS_STRING_ARRAY, allowedProviderKeys);
editor.putStringSet(ScannerServiceApi.EXTRA_SEARCH_EXCLUDED_PROVIDERS_STRING_ARRAY, excludedProviderKeys);

editor.putInt(PREFS_KEY, segmentedButtonState);
editor.putInt(PREFS_KEY, segmentedButtonProvidersState);
editor.putInt(ENABLE_KEEP_ASPECT_RATIO_KEY, aspectRatioMode);

final Set<String> symbologySelection = new HashSet<>();
if (((CheckBox) findViewById(R.id.checkSelectCode128)).isChecked()) { symbologySelection.add(BarcodeType.CODE128.name()); }
Expand All @@ -298,7 +325,7 @@ public void onClickSave(View v) {
/**
* Bind toggle buttons
*/
private void bindToggleButton() {
private void bindToggleButtonProviders() {
bt_all.setOnClickListener(v -> {
if (bt_all.isChecked()) {
bt_all.setIcon(ContextCompat.getDrawable(this, R.drawable.check_all));
Expand All @@ -311,7 +338,7 @@ private void bindToggleButton() {
findViewById(providerViewId).setEnabled(false);
}

segmentedButtonState = 0;
segmentedButtonProvidersState = 0;
}
});
bt_spec.setOnClickListener(v -> {
Expand All @@ -325,7 +352,7 @@ private void bindToggleButton() {
findViewById(providerViewId).setEnabled(true);
}

segmentedButtonState = 1;
segmentedButtonProvidersState = 1;
}
});
bt_none.setOnClickListener(v -> {
Expand All @@ -340,30 +367,71 @@ private void bindToggleButton() {
findViewById(providerViewId).setEnabled(false);
}

segmentedButtonState = 2;
segmentedButtonProvidersState = 2;
}
});
}

/**
* Bind toggle button aspect ratio mode
*/

private void bindToggleButtonAspectRatioMode() {
bt_stretch.setOnClickListener(v -> {
if (bt_stretch.isChecked()) {
bt_stretch.setText(R.string.fill_stretch);
bt_black_bars.setText(null);
bt_crop.setText(null);

aspectRatioMode = 0;
}
});
bt_black_bars.setOnClickListener(v -> {
if (bt_black_bars.isChecked()) {
bt_black_bars.setText(R.string.fill_black_bars);
bt_stretch.setText(null);
bt_crop.setText(null);

aspectRatioMode = 1;
}
});
bt_crop.setOnClickListener(v -> {
if (bt_crop.isChecked()) {
bt_crop.setText(R.string.fill_crop);
bt_stretch.setText(null);
bt_black_bars.setText(null);

aspectRatioMode = 2;
}
});
}


/**
* Set the state of segmented button
*/
private void setSegmentedButtonState() {
switch (segmentedButtonState) {
private void setSegmentedButtonState(boolean isProvider) {
MaterialButton bt_1 = isProvider ? bt_all : bt_stretch;
MaterialButton bt_2 = isProvider ? bt_spec : bt_black_bars;
MaterialButton bt_3 = isProvider ? bt_none : bt_crop;

int buttonState = isProvider ? segmentedButtonProvidersState : aspectRatioMode;

switch (buttonState) {
case 0:
bt_all.performClick();
bt_spec.setChecked(false);
bt_none.setChecked(false);
bt_1.performClick();
bt_2.setChecked(false);
bt_3.setChecked(false);
break;
case 1:
bt_all.setChecked(false);
bt_spec.performClick();
bt_none.setChecked(false);
bt_1.setChecked(false);
bt_2.performClick();
bt_3.setChecked(false);
break;
case 2:
bt_all.setChecked(false);
bt_spec.setChecked(false);
bt_none.performClick();
bt_1.setChecked(false);
bt_2.setChecked(false);
bt_3.performClick();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void onClickBt1(View v) {
// add allow camera fallback intent extra
intent.putExtra(SettingsActivity.ALLOW_CAMERA_FALLBACK_KEY, preferences.getBoolean(SettingsActivity.ALLOW_CAMERA_FALLBACK_KEY, false));
// add enable keep aspect ratio intent extra
intent.putExtra(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, preferences.getBoolean(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, false) ? 1 : 0);
intent.putExtra(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, preferences.getInt(SettingsActivity.ENABLE_KEEP_ASPECT_RATIO_KEY, 0));

startActivity(intent);
}
Expand Down
12 changes: 12 additions & 0 deletions demoscannerapp/src/main/res/drawable/black_bars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<path
android:pathData="M14,2a1,1 0,0 1,1 1v10a1,1 0,0 1,-1 1H2a1,1 0,0 1,-1 -1V3a1,1 0,0 1,1 -1zM2,1a2,2 0,0 0,-2 2v10a2,2 0,0 0,2 2h12a2,2 0,0 0,2 -2V3a2,2 0,0 0,-2 -2z"
android:fillColor="@android:color/black"/>
<path
android:pathData="M3,4a1,1 0,0 1,1 -1h2a1,1 0,0 1,1 1v8a1,1 0,0 1,-1 1H4a1,1 0,0 1,-1 -1z"
android:fillColor="@android:color/black"/>
</vector>
9 changes: 9 additions & 0 deletions demoscannerapp/src/main/res/drawable/crop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<path
android:pathData="M3.5,0.5A0.5,0.5 0,0 1,4 1v13h13a0.5,0.5 0,0 1,0 1h-2v2a0.5,0.5 0,0 1,-1 0v-2H3.5a0.5,0.5 0,0 1,-0.5 -0.5V4H1a0.5,0.5 0,0 1,0 -1h2V1a0.5,0.5 0,0 1,0.5 -0.5m2.5,3a0.5,0.5 0,0 1,0.5 -0.5h8a0.5,0.5 0,0 1,0.5 0.5v8a0.5,0.5 0,0 1,-1 0V4H6.5a0.5,0.5 0,0 1,-0.5 -0.5"
android:fillColor="@android:color/black"/>
</vector>
10 changes: 10 additions & 0 deletions demoscannerapp/src/main/res/drawable/stretch.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="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<path
android:pathData="M5.828,10.172a0.5,0.5 0,0 0,-0.707 0l-4.096,4.096V11.5a0.5,0.5 0,0 0,-1 0v3.975a0.5,0.5 0,0 0,0.5 0.5H4.5a0.5,0.5 0,0 0,0 -1H1.732l4.096,-4.096a0.5,0.5 0,0 0,0 -0.707m4.344,-4.344a0.5,0.5 0,0 0,0.707 0l4.096,-4.096V4.5a0.5,0.5 0,1 0,1 0V0.525a0.5,0.5 0,0 0,-0.5 -0.5H11.5a0.5,0.5 0,0 0,0 1h2.768l-4.096,4.096a0.5,0.5 0,0 0,0 0.707"
android:fillColor="@android:color/black"
android:fillType="evenOdd"/>
</vector>
70 changes: 46 additions & 24 deletions demoscannerapp/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,37 +342,59 @@
android:layout_marginEnd="16dp"/>
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayoutEnableKeepAspectRatio"
<TextView
android:id="@+id/textViewAspectRatioMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="9"
android:orientation="horizontal"
android:gravity="center_vertical"
android:text="@string/choose_aspect_ratio_mode"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="@color/defaultTextColor"
android:layout_marginTop="10dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayoutAllowCameraFallback">
app:layout_constraintTop_toBottomOf="@+id/linearLayoutAllowCameraFallback"/>

<TextView
android:id="@+id/textViewEnableKeepAspectRatio"
android:layout_width="0dp"
android:layout_weight="9"
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButtonAspectRatio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:singleSelection="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAspectRatioMode">

<com.google.android.material.button.MaterialButton
android:id="@+id/button_fill_stretch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable_keep_aspect_ratio"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="@color/defaultTextColor"
android:layout_marginStart="16dp"/>
android:textAppearance="?attr/textAppearanceCaption"
app:iconGravity="textStart"
app:icon="@drawable/stretch"
style="@style/Widget.Material3.Button.OutlinedButton"/>

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_fill_black_bars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceCaption"
app:iconGravity="textStart"
app:icon="@drawable/black_bars"
style="@style/Widget.Material3.Button.OutlinedButton"/>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switchEnableKeepAspectRatio"
<com.google.android.material.button.MaterialButton
android:id="@+id/button_fill_crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"/>
</LinearLayout>
android:textAppearance="?attr/textAppearanceCaption"
app:iconGravity="textStart"
app:icon="@drawable/crop"
style="@style/Widget.Material3.Button.OutlinedButton"/>

</com.google.android.material.button.MaterialButtonToggleGroup>

<com.google.android.material.divider.MaterialDivider
android:id="@+id/dividerSettingsAllowedProvider"
Expand All @@ -382,7 +404,7 @@
app:layout_constraintWidth_percent="0.90"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayoutEnableKeepAspectRatio"/>
app:layout_constraintTop_toBottomOf="@+id/toggleButtonAspectRatio"/>

<!-- ALLOWED PROVIDERS -->

Expand Down
5 changes: 4 additions & 1 deletion demoscannerapp/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
<string name="allow_intent_devices">Autoriser les scanners fonctionnant par Intent</string>
<string name="enable_log">Activation de l\'historique des scans</string>
<string name="allow_camera_fallback">Activation par défaut de l\'appareil photo si aucun scanneur n\'est trouvé</string>
<string name="enable_keep_aspect_ratio">Conserver le rapport hauteur/largeur de la caméra (remplir l\'espace disponible par défaut)</string>
<string name="choose_aspect_ratio_mode">Choisissez le mode de rapport hauteur/largeur de la caméra. Préservez les proportions en ajoutant des barres noires ou en recadrant la vue. Remplissez tout l\'écran en étirant l\'aperçu.</string>
<string name="fill_stretch">extensible</string>
<string name="fill_black_bars">barres-noires</string>
<string name="fill_crop">recadrage</string>
<string name="allowed_providers">Providers autorisés</string>
<string name="excluded_providers">Providers exclus</string>
<string name="symbology_selection">Sélection de la symbologie</string>
Expand Down
5 changes: 4 additions & 1 deletion demoscannerapp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<string name="allow_intent_devices">Allow intent devices</string>
<string name="enable_log">Enable scan history</string>
<string name="allow_camera_fallback">Default to camera if no scanner is found</string>
<string name="enable_keep_aspect_ratio">Maintain camera aspect ratio (fill all available space by default)</string>
<string name="choose_aspect_ratio_mode">Choose the camera aspect ratio mode. Preserve the proportions by adding black bars or cropping the view. Fill the entire screen by stretching the preview.</string>
<string name="fill_stretch">stretch</string>
<string name="fill_black_bars">black bars</string>
<string name="fill_crop">crop</string>
<string name="allowed_providers">Allowed Providers</string>
<string name="excluded_providers">Excluded Providers</string>
<string name="symbology_selection">Allowed Symbology</string>
Expand Down
5 changes: 3 additions & 2 deletions enioka_scan/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<attr name="targetStrokeWidth" format="integer" />
<attr name="targetIsFixed" format="boolean" />
<attr name="previewRatioMode" format="enum">
<enum name="fillAvailableSpace" value="0" />
<enum name="fitToPicture" value="1" />
<enum name="fillWithStretch" value="0" />
<enum name="fillWithBlackBars" value="1" />
<enum name="fillWithCrop" value="2" />
</attr>
<attr name="useAdaptiveResolution" format="boolean" />
<attr name="storePreferredResolution" format="boolean" />
Expand Down
Loading

0 comments on commit fbebe3e

Please sign in to comment.