本项目中图片生成视频用的时JavaCV
来实现的,JavaCV的使用方法 请参照
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
MediaConstant.copyFilesFassets(this, "audio", MediaConstant.createAssetsPath());
- 其中
audio
即本地存放音频文件的文件夹的原文件路径 可以随意修改 Constant.createAssetsPath()
即复制后在SD卡上的路径 可以随意修改
AudioAndVideoCompounder compounder =
AudioAndVideoCompounder.createCompounder(String videoPath, String audioPath, String dstPath);
createCompounder
方法参数说明:
- videoPath: 原视频路径
- audioPath:原音频路径
- dstPath :合成后音视频路径
然后调用 AudioAndVideoCompounder
的start()
方法即可 最好通过异步方式来调用
如果需要,记得合成玩将原视频删除
File file = new File(saveMp4Name);
if (file.exists()) file.delete();
###3、通过选择本地图片(编辑或者其他对图片进行涂鸦后),合成GIF或者合成无声的视频
String videoPath =
CreateVideoUtil.createVideo(String saveMp4Name, List<String> imageList, int width, int height);
createVideo
该方法有多个重载方法,注意参数就好了
- saveMp4Name 生成视频的路径
- imageList 用于生成视频的图片合集(可以是涂鸦过的,也可以时原图)
- width 生成视频的宽
- height 生成视频的高 宽高可以这样获取:
WindowManager manager = activity.getWindowManager();
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
int mWidth = outMetrics.widthPixels;
int mHeight = outMetrics.heightPixels;
CreateGifUtil.createGif(String filename, List<String> paths, Activity activity, ZCreateListener listener);
其中涉及一个回调函数 ZCreateListener
,很简单就一个方法
void onComplete(String path);
参数 String path
就是生成的GIf的路径
图片选择选用的是:
implementation('com.github.LuckSiege.PictureSelector:picture_library:v2.1.7')
图片涂鸦选择的是:
implementation ('com.hzw.graffiti:graffiti:4.3.1')
用法:必须用RecyclerView来展示图片,
mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));其中 GridLayoutManager 已经实现,可以直接拿来使用
至于Adapter
GridImageAdapter(Context context, onAddPicClickListener mOnAddPicClickListener);也是内部实现好的,可以直接拿来用
mAdapter = new GridImageAdapter(this, () -> {
//这里就是起吊相册,选择图片
GridImageConfig.intoAlbum(mContext, 30, selectList);
});
mAdapter.setOnItemClickListener((position, v) -> {
if (selectList.size() > 0) {
LocalMedia media = selectList.get(position);
//TODO 这里就是跳转涂鸦页面
Graffiti.startActivityForResult(ActivityImageList.this
, media.getCompressPath()
, position);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//涂鸦回调
for (int a = 0; a < mAdapter.getItemCount(); a++) {
if (requestCode == a)
if (resultCode == GraffitiActivity.RESULT_OK) {
// 获取涂鸦后的图片
String path = data.getStringExtra(GraffitiActivity.KEY_IMAGE_PATH);
mImageList.remove(a);
mImageList.add(a, path);
LocalMedia media = selectList.get(a);
media.setCompressPath(path);
mAdapter.notifyDataSetChanged();
} else if (resultCode == GraffitiActivity.RESULT_ERROR) {
toast("制作失败");
}
}
//++++++++++++++++++++++++
//相册选取回调
if (requestCode == PictureConfig.CHOOSE_REQUEST)
if (resultCode == RESULT_OK) {
// 图片选择结果回调
selectList = PictureSelector.obtainMultipleResult(data);
GridImageAdapter adapter = (GridImageAdapter) mRecyclerView.getAdapter();
adapter.setList(selectList);
adapter.notifyDataSetChanged();
for (int a = 0; a < selectList.size(); a++) {
mImageList.add(a, selectList.get(a).getCompressPath());
}
}
Log.d("mImageList", mImageList.size() + "");
for (String path : mImageList) {
Log.d("mImageList", path);
}
}