Update demo transformer app with text overlay demo effect.

Implements milestone 1.4.2 of the [overlays implementation plan](https://docs.google.com/document/d/1EcP2GN8k8N74hHZyD0KTqm9oQo5-W1dZMqIVyqVGtlo/edit#bookmark=id.76uzcie1dg9d)

PiperOrigin-RevId: 493324764
This commit is contained in:
tofunmi 2022-12-06 17:15:21 +00:00 committed by Ian Baker
parent c37317222c
commit 9010e8c368
4 changed files with 150 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
@ -89,6 +90,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
public static final String HSL_ADJUSTMENTS_LIGHTNESS = "hsl_adjustments_lightness";
public static final String BITMAP_OVERLAY_URI = "bitmap_overlay_uri";
public static final String BITMAP_OVERLAY_ALPHA = "bitmap_overlay_alpha";
public static final String TEXT_OVERLAY_TEXT = "text_overlay_text";
public static final String TEXT_OVERLAY_TEXT_COLOR = "text_overlay_text_color";
public static final String TEXT_OVERLAY_ALPHA = "text_overlay_alpha";
public static final int COLOR_FILTER_GRAYSCALE = 0;
public static final int COLOR_FILTER_INVERTED = 1;
public static final int COLOR_FILTER_SEPIA = 2;
@ -143,6 +147,7 @@ public final class ConfigurationActivity extends AppCompatActivity {
"Overlay logo & timer",
"Zoom in start",
"Custom Bitmap Overlay",
"Custom Text Overlay",
};
private static final ImmutableMap<String, @TransformationRequest.HdrMode Integer>
HDR_MODE_DESCRIPTIONS =
@ -153,6 +158,20 @@ public final class ConfigurationActivity extends AppCompatActivity {
"Force Interpret HDR as SDR",
TransformationRequest.HDR_MODE_EXPERIMENTAL_FORCE_INTERPRET_HDR_AS_SDR)
.build();
private static final ImmutableMap<String, Integer> OVERLAY_COLORS =
new ImmutableMap.Builder<String, Integer>()
.put("BLACK", Color.BLACK)
.put("BLUE", Color.BLUE)
.put("CYAN", Color.CYAN)
.put("DKGRAY", Color.DKGRAY)
.put("GRAY", Color.GRAY)
.put("GREEN", Color.GREEN)
.put("LTGRAY", Color.LTGRAY)
.put("MAGENTA", Color.MAGENTA)
.put("RED", Color.RED)
.put("WHITE", Color.WHITE)
.put("YELLOW", Color.YELLOW)
.build();
private static final String SAME_AS_INPUT_OPTION = "same as input";
private static final int COLOR_FILTERS_INDEX = 2;
private static final int RGB_ADJUSTMENTS_INDEX = 4;
@ -160,6 +179,7 @@ public final class ConfigurationActivity extends AppCompatActivity {
private static final int CONTRAST_INDEX = 6;
private static final int PERIODIC_VIGNETTE_INDEX = 7;
private static final int BITMAP_OVERLAY_INDEX = 11;
private static final int TEXT_OVERLAY_INDEX = 12;
private static final float HALF_DIAGONAL = 1f / (float) Math.sqrt(2);
private @MonotonicNonNull ActivityResultLauncher<Intent> localFilePickerLauncher;
@ -202,6 +222,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
private float periodicVignetteOuterRadius;
private @MonotonicNonNull String bitmapOverlayUri;
private float bitmapOverlayAlpha;
private @MonotonicNonNull String textOverlayText;
private int textOverlayTextColor;
private float textOverlayAlpha;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -407,6 +430,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
bundle.putFloat(PERIODIC_VIGNETTE_OUTER_RADIUS, periodicVignetteOuterRadius);
bundle.putString(BITMAP_OVERLAY_URI, bitmapOverlayUri);
bundle.putFloat(BITMAP_OVERLAY_ALPHA, bitmapOverlayAlpha);
bundle.putString(TEXT_OVERLAY_TEXT, textOverlayText);
bundle.putInt(TEXT_OVERLAY_TEXT_COLOR, textOverlayTextColor);
bundle.putFloat(TEXT_OVERLAY_ALPHA, textOverlayAlpha);
transformerIntent.putExtras(bundle);
@Nullable Uri intentUri;
@ -539,6 +565,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
case BITMAP_OVERLAY_INDEX:
controlBitmapOverlaySettings();
break;
case TEXT_OVERLAY_INDEX:
controlTextOverlaySettings();
break;
}
}
@ -659,6 +688,33 @@ public final class ConfigurationActivity extends AppCompatActivity {
.show();
}
private void controlTextOverlaySettings() {
View dialogView = getLayoutInflater().inflate(R.layout.text_overlay_options, /* root= */ null);
EditText textEditText = checkNotNull(dialogView.findViewById(R.id.text_overlay_text));
ArrayAdapter<String> textColorAdapter =
new ArrayAdapter<>(/* context= */ this, R.layout.spinner_item);
textColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner textColorSpinner = checkNotNull(dialogView.findViewById(R.id.text_overlay_text_color));
textColorSpinner.setAdapter(textColorAdapter);
textColorAdapter.addAll(OVERLAY_COLORS.keySet());
Slider alphaSlider = checkNotNull(dialogView.findViewById(R.id.text_overlay_alpha_slider));
new AlertDialog.Builder(/* context= */ this)
.setTitle(R.string.bitmap_overlay_settings)
.setView(dialogView)
.setPositiveButton(
android.R.string.ok,
(DialogInterface dialogInterface, int i) -> {
textOverlayText = textEditText.getText().toString();
String selectedTextColor = String.valueOf(textColorSpinner.getSelectedItem());
textOverlayTextColor = checkNotNull(OVERLAY_COLORS.get(selectedTextColor));
textOverlayAlpha = alphaSlider.getValue();
})
.create()
.show();
}
@RequiresNonNull({
"removeVideoCheckbox",
"forceSilentAudioCheckbox",

View File

@ -27,6 +27,9 @@ import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@ -55,6 +58,7 @@ import androidx.media3.effect.RgbAdjustment;
import androidx.media3.effect.RgbFilter;
import androidx.media3.effect.RgbMatrix;
import androidx.media3.effect.SingleColorLut;
import androidx.media3.effect.TextOverlay;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor;
import androidx.media3.exoplayer.audio.SonicAudioProcessor;
@ -501,13 +505,30 @@ public final class TransformerActivity extends AppCompatActivity {
bundle.getFloat(
ConfigurationActivity.BITMAP_OVERLAY_ALPHA, /* defaultValue= */ 1))
.build();
BitmapOverlay bitmapOverlay =
BitmapOverlay.createStaticBitmapOverlay(
Uri.parse(checkNotNull(bundle.getString(ConfigurationActivity.BITMAP_OVERLAY_URI))),
overlaySettings);
effects.add(new OverlayEffect(ImmutableList.of(bitmapOverlay)));
}
if (selectedEffects[12]) {
OverlaySettings overlaySettings =
new OverlaySettings.Builder()
.setAlpha(
bundle.getFloat(ConfigurationActivity.TEXT_OVERLAY_ALPHA, /* defaultValue= */ 1))
.build();
SpannableString overlayText =
new SpannableString(
checkNotNull(bundle.getString(ConfigurationActivity.TEXT_OVERLAY_TEXT)));
overlayText.setSpan(
new ForegroundColorSpan(bundle.getInt(ConfigurationActivity.TEXT_OVERLAY_TEXT_COLOR)),
/* start= */ 0,
overlayText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextOverlay textOverlay = TextOverlay.createStaticTextOverlay(overlayText, overlaySettings);
// TODO(227625365): use the same OverlayEffect object for bitmap and text overlays.
effects.add(new OverlayEffect(ImmutableList.of(textOverlay)));
}
return effects.build();
}

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2022 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".ConfigurationActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_marginTop="32dp"
android:measureWithLargestChild="true"
android:paddingLeft="24dp"
android:paddingRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/overlay_text" />
<EditText
android:id="@+id/text_overlay_text"
android:inputType="textMultiLine"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/overlay_text_color"/>
<Spinner
android:id="@+id/text_overlay_text_color"
android:layout_gravity="right|center_vertical"
android:gravity="right" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView android:text="@string/overlay_alpha" />
<com.google.android.material.slider.Slider
android:id="@+id/text_overlay_alpha_slider"
android:valueFrom="0"
android:value="1"
android:valueTo="1"
android:layout_gravity="right|center_vertical"/>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -70,4 +70,7 @@
<string name="overlay_alpha">Alpha</string>
<string name="overlay_uri">Uri</string>
<string name="bitmap_overlay_settings">Specify bitmap overlay settings</string>
<string name="overlay_text">Text</string>
<string name="overlay_text_color">Text color</string>
<string name="text_overlay_settings">Specify text overlay settings</string>
</resources>