Android 平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方案。
phoenix
项目地址:guoxiaoxing/phoenix
简介:The one-stop solution for image/video selection, editing and compression on the Android platform.
标签:
功能介绍
Android 平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方案。
图片/视频的选择,编辑和压缩是日常开发中的常见需求,Phoenix 完整的实现了这些功能,并提供了优雅的调用方式。Phoenix 的核心功能基于 Kotlin 实现,外层接口基于 Java 实现,方便 Kotlin 与 Java 双方的调用。
更多关于图像/视频编解码的文章
特点
- 功能相互独立,各个功能的实现依赖于约定的接口,彼此互不依赖,开发者不必为了引入某一个功能而带入一堆依赖。
- 高度的 UI 定制性,内置四种配色方案,开发者也可以通过简单的 style 文件的简单配置来定制自己的 UI。
- 调用的便利性,开启某个功能只需要调用 enableXXX(true)方法,结果统一在 MediaEntity 里获取。
- 库体积轻便小巧,视频压缩基于系统的 MediaCodec 实现,压缩速度快,无额外的依赖。
- RxJava 良好的支持性,每个功能都提供了同步与异步两种实现,便于开发者利用 RxJava 进行功能的组合与嵌套。
- ImageLoader 可配置,定制项目需要的图片加载方案。
- 良好的版本兼容性,运行时权限等内容都做了兼容性处理。
功能
- 拍照
- 录制视频
- 图片选择
- 图片预览
- 图片压缩
- 图片标记、贴图、涂抹与裁剪
- 视频选择
- 视频预览
- 视频压缩
主题
- 默认主题
- 橙色主图
- 红色主题
- 蓝色主题
快递开始
添加依赖
//图片/视频选择、预览、编辑与拍照
implementation 'com.github.guoxiaoxing:phoenix:1.0.15'
//选填 - 图片压缩,开启功能:Phoenix.with().enableCompress(true),获取结果:MediaEntity.getCompressPath()
implementation 'com.github.guoxiaoxing:phoenix-compress-picture:1.0.15'
//选填 - 视频压缩,开启功能:Phoenix.with().enableCompress(true),获取结果:MediaEntity.getCompressPath()
implementation 'com.github.guoxiaoxing:phoenix-compress-video:1.0.15'
调用功能
初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Phoenix.config()
.imageLoader(new ImageLoader() {
@Override
public void loadImage(Context mContext, ImageView imageView
, String imagePath, int type) {
Glide.with(mContext)
.load(imagePath)
.into(imageView);
}
});
}
}
开启功能
Phoenix.with()
.theme(PhoenixOption.THEME_DEFAULT)// 主题
.fileType(MimeType.ofAll())//显示的文件类型图片、视频、图片和视频
.maxPickNumber(10)// 最大选择数量
.minPickNumber(0)// 最小选择数量
.spanCount(4)// 每行显示个数
.enablePreview(true)// 是否开启预览
.enableCamera(true)// 是否开启拍照
.enableAnimation(true)// 选择界面图片点击效果
.enableCompress(true)// 是否开启压缩
.compressPictureFilterSize(1024)//多少 kb 以下的图片不压缩
.compressVideoFilterSize(2018)//多少 kb 以下的视频不压缩
.thumbnailHeight(160)// 选择界面图片高度
.thumbnailWidth(160)// 选择界面图片宽度
.enableClickSound(false)// 是否开启点击声音
.pickedMediaList(mMediaAdapter.getData())// 已选图片数据
.videoFilterTime(0)//显示多少秒以内的视频
.mediaFilterSize(10000)//显示多少 kb 以下的图片/视频,默认为 0,表示不限制
//如果是在 Activity 里使用就传 Activity,如果是在 Fragment 里使用就传 Fragment
.start(MainActivity.this, PhoenixOption.TYPE_PICK_MEDIA, REQUEST_CODE);
获取结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//返回的数据
List<MediaEntity> result = Phoenix.result(data);
mMediaAdapter.setData(result);
}
}
另外,Phoenix 内置了图片压缩库与视频压缩库,这两个功能都可以单独调用。
图片压缩
同步方法
File file = new File(localPath);
try {
File compressFIle = PictureCompressor.with(mContext)
.savePath(mContext.getCacheDir().getAbsolutePath())
.load(file)
.get();
if (compressFIle != null) {
String compressPath = compressFIle.getAbsolutePath();
}
} catch (IOException e) {
e.printStackTrace();
}
异步方法
File file = new File(localPath);
PictureCompressor.with(mContext)
.load(file)
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
}
@Override
public void onSuccess(File file) {
String compressPath = file.getAbsolutePath();
}
@Override
public void onError(Throwable e) {
}
}).launch();
视频压缩
同步方法
final File compressFile;
try {
File compressCachePath = new File(mContext.getCacheDir(), "outputs");
compressCachePath.mkdir();
compressFile = File.createTempFile("compress", ".mp4", compressCachePath);
} catch (IOException e) {
Toast.makeText(mContext, "Failed to create temporary file.", Toast.LENGTH_LONG).show();
return null;
}
try {
String compressPath = VideoCompressor.with().syncTranscodeVideo(mediaEntity.getLocalPath(), compressFile.getAbsolutePath(),
MediaFormatStrategyPresets.createAndroid480pFormatStrategy());
} catch (IOException e) {
e.printStackTrace();
}
异步方法
final File compressFile;
try {
File compressCachePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "phoenix");
compressCachePath.mkdir();
compressFile = File.createTempFile("compress", ".mp4", compressCachePath);
} catch (IOException e) {
Toast.makeText(mContext, "Failed to create temporary file.", Toast.LENGTH_LONG).show();
return;
}
VideoCompressor.Listener listener = new VideoCompressor.Listener() {
@Override
public void onTranscodeProgress(double progress) {
}
@Override
public void onTranscodeCompleted() {
String compressPath = compressFile.getAbsolutePath();
}
@Override
public void onTranscodeCanceled() {
}
@Override
public void onTranscodeFailed(Exception exception) {
}
};
try {
VideoCompressor.with().asyncTranscodeVideo(mediaEntity.getLocalPath(), compressFile.getAbsolutePath(),
MediaFormatStrategyPresets.createAndroid480pFormatStrategy(), listener);
} catch (IOException e) {
e.printStackTrace();
}
视频压缩的测试数据
手机型号 | 源视频时长 | 源视频大小 | 压缩视频大小 | 压缩耗时 |
---|---|---|---|---|
小米 4 | 60s | 55.67MB | 5.56MB | 20.17s |
荣耀 7 | 60s | 68.37MB | 6.22MB | 18.83s |
oppo r9 | 60s | 55.57MB | 5.35MB | 38.27s |
可以看到对于 60s 的视频,压缩时间控制在了 18s~22s 之间,性能好的手机用时短一些,另外,压缩提供了 480p、720p 等多种规格的压缩参数。