Android照片获取框架TakePhoto使用流程

GitHub:https://github.com/crazycodeboy/TakePhoto

发起TakePhoto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//多图
takePhoto.onPickMultiple(limit);
takePhoto.onPickMultipleWithCrop(limit, getCropOptions());

//文件
takePhoto.onPickFromDocuments();
takePhoto.onPickFromDocumentsWithCrop(imageUri, getCropOptions());

//相册
takePhoto.onPickFromGallery();
takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());

//相机
takePhoto.onPickFromCapture(imageUri);
takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions());

回调TakePhoto

通过继承的方式

继承TakePhotoActivityTakePhotoFragmentActivityTakePhotoFragment三者之一,通过getTakePhoto()获取TakePhoto实例进行相关操作。

重写以下方法获取结果

1
2
3
void takeSuccess(TResult result);
void takeFail(TResult result,String msg);
void takeCancel();

通过组装的方式

Activity或Fragment实现TakePhoto.TakeResultListener, InvokeListener接口

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
@Override
public void takeSuccess(TResult result) {
NLog.i(TAG, "takeSuccess: " + result.getImage().getOriginalPath());
List<TImage> imageList = result.getImages();
if (imageList != null && imageList.size() > 0) {
for (int i = 0; i < imageList.size(); i++) {
TImage tImage = imageList.get(i);
String originalPath = tImage.getOriginalPath();
String compressPath = tImage.getCompressPath();
NLog.i(TAG, "originalPath: " + originalPath + ", compressPath: " + compressPath);

}
} else {
NLog.i(TAG, "takeSuccess: Image list empty");
}
}

@Override
public void takeFail(TResult result, String msg) {
NLog.i(TAG, "takeFail:" + msg);
}

@Override
public void takeCancel() {
NLog.i(TAG, "takeCancel");
}

添加代码获取TakePhoto实例

1
2
3
4
5
6
7
8
9
10
11
/**
* 获取TakePhoto实例
*
* @return
*/
public TakePhoto getTakePhoto() {
if (mTakePhoto == null) {
mTakePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
}
return mTakePhoto;
}

onCreate,onActivityResult,onSaveInstanceState方法中调用TakePhoto对应的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
getTakePhoto().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);

TakePhotoHelper.configTakePhotoOptions(mTakePhoto);
TakePhotoHelper.configCompressOptions(mTakePhoto);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
getTakePhoto().onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onSaveInstanceState(Bundle outState) {
getTakePhoto().onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}

重写onRequestPermissionsResult

1
2
3
4
5
6
7
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//以下代码为处理Android6.0、7.0动态权限所需
PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.handlePermissionsResult(mActivity, type, mInvokeParam, this);
}

重写TPermissionType invoke(InvokeParam invokeParam)方法

1
2
3
4
5
6
7
8
@Override
public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
if (PermissionManager.TPermissionType.WAIT.equals(type)) {
mInvokeParam = invokeParam;
}
return type;
}

参数配置

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
public class TakePhotoHelper {

public static void configTakePhotoOptions(TakePhoto takePhoto) {
TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
builder.setWithOwnGallery(true); //使用系统自带相册或系统相册组件
builder.setCorrectImage(true); //是否纠正拍照的照片旋转角度
takePhoto.setTakePhotoOptions(builder.create());
}

/**
* 配置压缩参数
* @param takePhoto
*/
public static void configCompressOptions(TakePhoto takePhoto) {
int maxSize = 100 * 1024;
int width = 128; //指定宽
int height = 128; //指定高
boolean showProgressBar = true; //显示进度
boolean enableRawFile = true; //是否保存原图
CompressConfig config = new CompressConfig.Builder()
.setMaxSize(maxSize)
.setMaxPixel(width >= height ? width : height)
.enableReserveRaw(enableRawFile)
.create();
takePhoto.onEnableCompress(config, showProgressBar);
}

/**
* 配置裁剪参数
* @return
*/
public static CropOptions getCropOptions(boolean scale, int width, int height) {
boolean withSelfCrop = false; //自带/第三方方式裁剪
//boolean aspect = true; //尺寸/比例

CropOptions.Builder builder = new CropOptions.Builder();
if (scale) {
builder.setAspectX(width).setAspectY(height); //比例
}
builder.setOutputX(width).setOutputY(height); //尺寸
builder.setWithOwnCrop(withSelfCrop);
return builder.create();
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :