项目地址:guoxiaoxing/phoenix
简介:The one-stop solution for image/video selection, editing and compression on the Android platform.
更多:作者 提 Bug
标签:
Android 平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方案。
图片/视频的选择,编辑和压缩是日常开发中的常见需求,Phoenix 完整的实现了这些功能,并提供了优雅的调用方式。Phoenix 的核心功能基于 Kotlin 实现,外层接口基于 Java 实现,方便 Kotlin 与 Java 双方的调用。
更多关于图像/视频编解码的文章
特点
功能
主题
//图片/视频选择、预览、编辑与拍照
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 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 等多种规格的压缩参数。