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

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

发起TakePhoto

//多图
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实例进行相关操作。

重写以下方法获取结果

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

通过组装的方式

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

@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实例

/**
 * 获取TakePhoto实例
 *
 * @return
 */
public TakePhoto getTakePhoto() {
    if (mTakePhoto == null) {
        mTakePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
    }
    return mTakePhoto;
}

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

@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

@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)方法

@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;
}

参数配置

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();
    }

}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/android-photo-acquisition-framework-takephoto-usage-process/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android照片获取框架TakePhoto使用流程
GitHub:https://github.com/crazycodeboy/TakePhoto 发起TakePhoto //多图 takePhoto.onPickMultiple(limit); takePhoto.onPickMultipleWithCrop(limit, get……
<<上一篇
下一篇>>
文章目录
关闭
目 录