-
Notifications
You must be signed in to change notification settings - Fork 212
/
FillModeCustomActivity.java
283 lines (240 loc) · 10.6 KB
/
FillModeCustomActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.daasuu.sample;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Size;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.daasuu.mp4compose.FillMode;
import com.daasuu.mp4compose.FillModeCustomItem;
import com.daasuu.mp4compose.composer.Mp4Composer;
import com.daasuu.mp4compose.filter.GlFilter;
import com.daasuu.sample.widget.GesturePlayerTextureView;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FillModeCustomActivity extends AppCompatActivity {
private final static String PATH_ARG = "PATH_ARG";
private static final String TAG = "SAMPLE";
private String srcPath;
private float baseWidthSize;
private GesturePlayerTextureView playerTextureView;
private Button btnRotate;
private Button btnCodec;
private Button btnClose;
private Button btnStartPlayMovie;
private Button btnColorChange;
private RelativeLayout layoutCodec;
private FrameLayout layoutCropChange;
private AlertDialog clearColorDialog;
private SceneCropColor sceneCropColor = SceneCropColor.BLACK;
public static void startActivity(Context context, String path) {
Intent intent = new Intent(context, FillModeCustomActivity.class);
intent.putExtra(PATH_ARG, path);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_mode_custom);
if (getIntent() == null || getIntent().getStringExtra(PATH_ARG) == null) {
finish();
return;
}
srcPath = getIntent().getStringExtra(PATH_ARG);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnRotate = findViewById(R.id.btn_rotate);
btnRotate.setOnClickListener((v) -> {
playerTextureView.updateRotate();
});
btnCodec = findViewById(R.id.btn_codec);
btnCodec.setOnClickListener((v) -> {
codec();
});
layoutCodec = findViewById(R.id.layout_codec);
btnClose = findViewById(R.id.close);
btnStartPlayMovie = findViewById(R.id.start_play_movie);
btnColorChange = findViewById(R.id.btn_color_change);
layoutCropChange = findViewById(R.id.layout_crop_change);
btnColorChange.setOnClickListener((v) -> {
if (clearColorDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Choose a background color");
builder.setOnDismissListener(dialog -> {
clearColorDialog = null;
});
final SceneCropColor[] items = SceneCropColor.values();
CharSequence[] charList = new CharSequence[items.length];
for (int i = 0, n = items.length; i < n; i++) {
charList[i] = items[i].name();
}
builder.setItems(charList, (dialog, item) -> {
sceneCropColor = items[item];
layoutCropChange.setBackgroundColor(ContextCompat.getColor(FillModeCustomActivity.this, sceneCropColor.getColorRes()));
});
clearColorDialog = builder.show();
} else {
clearColorDialog.dismiss();
}
});
initPlayer();
}
@Override
protected void onResume() {
super.onResume();
if (playerTextureView != null) {
playerTextureView.play();
}
}
@Override
protected void onPause() {
super.onPause();
if (playerTextureView != null) {
playerTextureView.pause();
}
}
private void initPlayer() {
FrameLayout frameLayout = findViewById(R.id.layout_crop_change);
playerTextureView = new GesturePlayerTextureView(getApplicationContext(), srcPath);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
playerTextureView.setLayoutParams(layoutParams);
baseWidthSize = (getWindowHeight(this) - dp2px(192, this)) / 16f * 9;
playerTextureView.setBaseWidthSize(baseWidthSize);
frameLayout.addView(playerTextureView, 1);
}
private void codec() {
layoutCodec.setVisibility(View.VISIBLE);
btnCodec.setEnabled(false);
btnRotate.setEnabled(false);
btnColorChange.setEnabled(false);
final ProgressBar progressBar = findViewById(R.id.progress_bar);
progressBar.setMax(100);
Size resolution = getVideoResolution(srcPath);
FillModeCustomItem fillModeCustomItem = new FillModeCustomItem(
playerTextureView.getScaleX(),
playerTextureView.getRotation(),
playerTextureView.getTranslationX() / baseWidthSize * 2f,
playerTextureView.getTranslationY() / (baseWidthSize / 9f * 16) * 2f,
resolution.getWidth(),
resolution.getHeight()
);
final String videoPath = getVideoFilePath();
btnStartPlayMovie.setEnabled(false);
btnStartPlayMovie.setOnClickListener((v) -> {
Uri uri = Uri.parse(videoPath);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
});
GlFilter glFilter = new GlFilter();
ClearColorItem clearColorItem = sceneCropColor.getClearColorItem();
glFilter.setClearColor(clearColorItem.getRed(), clearColorItem.getGreen(), clearColorItem.getBlue(), clearColorItem.getAlpha());
new Mp4Composer(srcPath, videoPath)
.size(720, 1280)
.filter(glFilter)
.fillMode(FillMode.CUSTOM)
.customFillMode(fillModeCustomItem)
.listener(new Mp4Composer.Listener() {
@Override
public void onProgress(double progress) {
Log.d(TAG, "onProgress = " + progress);
runOnUiThread(() -> progressBar.setProgress((int) (progress * 100)));
}
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted()");
exportMp4ToGallery(getApplicationContext(), videoPath);
runOnUiThread(() -> {
progressBar.setProgress(100);
btnStartPlayMovie.setEnabled(true);
btnClose.setVisibility(View.VISIBLE);
btnClose.setOnClickListener((v) -> {
layoutCodec.setVisibility(View.GONE);
btnCodec.setEnabled(true);
btnRotate.setEnabled(true);
btnColorChange.setEnabled(true);
});
Toast.makeText(FillModeCustomActivity.this, "codec complete path =" + videoPath, Toast.LENGTH_SHORT).show();
});
}
@Override
public void onCanceled() {
}
@Override
public void onCurrentWrittenVideoTime(long timeUs) {
}
@Override
public void onFailed(Exception exception) {
Log.d(TAG, "onFailed()");
}
})
.start();
}
public static int getWindowHeight(Context context) {
Display disp = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
return size.y;
}
public static int dp2px(float dpValue, final Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public File getAndroidMoviesFolder() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
}
public String getVideoFilePath() {
return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".mp4";
}
public Size getVideoResolution(String path) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
int width = Integer.valueOf(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
);
int height = Integer.valueOf(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
);
retriever.release();
int rotation = getVideoRotation(path);
if (rotation == 90 || rotation == 270) {
return new Size(height, width);
}
return new Size(width, height);
}
public int getVideoRotation(String videoFilePath) {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(videoFilePath);
String orientation = mediaMetadataRetriever.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION
);
return Integer.valueOf(orientation);
}
public static void exportMp4ToGallery(Context context, String filePath) {
final ContentValues values = new ContentValues(2);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, filePath);
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
values);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + filePath)));
}
}