美文网首页
第三方开源库之 TakePhoto

第三方开源库之 TakePhoto

作者: Kevin_小飞象 | 来源:发表于2021-06-08 14:59 被阅读0次

    TakePhoto 是一款用于在 Android 设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本 4.0.3。

    • 使用自带的相机 APP 拍照
    • 系统相册选择照片
    • 系统相机或相册获取的照片裁剪

    效果图

    01.png 02.png 03.png

    使用

    1. 添加依赖
        // 申请权限
        implementation 'com.tbruyelle.rxpermissions2:rxpermissions:+'
        implementation 'com.jph.takephoto:takephoto_library:4.0.3'
        //glide
        implementation 'com.github.bumptech.glide:glide:4.12.0'
        annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    
    1. 清单文件中添加权限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    
    1. popup_take_photo.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_bg_normal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_avatar_photograph"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textSize="16sp"
            android:gravity="center"
            android:textColor="@color/text_color"
            android:text="拍照"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"/>
    
        <TextView
            android:id="@+id/tv_avatar_photo"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textSize="16sp"
            android:gravity="center"
            android:textColor="@color/text_color"
            android:text="相册"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"/>
    
        <TextView
            android:id="@+id/tv_avatar_cancel"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textSize="16sp"
            android:gravity="center"
            android:textColor="@color/text_color"
            android:text="取消"/>
    
    </LinearLayout>
    
    1. 代码逻辑
      (1) 继承 TakePhotoActivity
      (2) 重写 takeSuccess,takeFail,takeCancel 方法
      (3) 设置动态权限,适配6.0以上设备
    public class PersonalInfoActivity extends TakePhotoActivity {
        @BindView(R.id.iv_head)
        ImageView headImg;
    
        private CommonPopupWindow popupWindow;
        private TakePhoto takePhoto;
        private CropOptions cropOptions;    //裁剪参数
        private CompressConfig compressConfig;  //压缩参数
        private Uri imageUri;
    
        private static final String[] permissionsGroup = new String[]{
                Manifest.permission.CALL_PHONE,
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_personal_info);
            ButterKnife.bind(this);
            initView();
            initData();
        }
    
        private void initData() {
            takePhoto = getTakePhoto();
    
            //设置裁剪参数
            cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(false).create();
    
            //设置压缩参数
            compressConfig = new CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create();
            takePhoto.onEnableCompress(compressConfig, true);    //设置为需要压缩
        }
    
        private void initView() {
            checkPermissionRequest();
        }
    
        @OnClick({R.id.rl_info_phone,R.id.rl_info_head})
        public void onClicked(View view) {
            switch (view.getId()) {
                case R.id.rl_info_head :
                    showPopupWindow();
                    break;
    
                case R.id.rl_info_phone :
    
                    break;
    
                default:
                    break;
            }
        }
    
        @Override
        public void takeSuccess(TResult result) {
            super.takeSuccess(result);
            String iconPath = result.getImage().getOriginalPath();
            Glide.with(this).load(iconPath).into(headImg);
        }
    
        @Override
        public void takeFail(TResult result, String msg) {
            super.takeFail(result, msg);
        }
    
        @Override
        public void takeCancel() {
            super.takeCancel();
        }
    
        private void showPopupWindow() {
            if (popupWindow != null && popupWindow.isShowing()) {
                return;
            }
    
            View popView = View.inflate(this,R.layout.popup_take_photo,null);
    
            popupWindow = new CommonPopupWindow.Builder(this)
                    .setView(R.layout.popup_take_photo)
                    .setAnimationStyle(R.style.AnimUp)
                    .setBackGroundLevel(0.5f)
                    .setWidthAndHeight(980,400)
                    .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {
                            TextView photograph = view.findViewById(R.id.tv_avatar_photograph);
                            TextView selPhoto = view.findViewById(R.id.tv_avatar_photo);
                            TextView cancel = view.findViewById(R.id.tv_avatar_cancel);
    
                            photograph.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (popupWindow != null) {
                                        popupWindow.dismiss();
                                    }
    
                                    imageUri = getImageCropUri();
                                    //仅仅拍照不裁剪
    //                                takePhoto.onPickFromCapture(imageUri);
    
                                    //拍照并裁剪
                                    takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
                                }
                            });
    
                            selPhoto.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (popupWindow != null) {
                                        popupWindow.dismiss();
                                    }
                                    imageUri = getImageCropUri();
    
                                    //从相册中选取不裁剪
    //                                takePhoto.onPickFromGallery();
                                    //从相册中选取图片并裁剪
                                    takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
                                }
                            });
    
                            cancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (popupWindow != null) {
                                        popupWindow.dismiss();
                                    }
                                }
                            });
    
                        }
                    })
                    .setOutsideTouchable(true)
                    .create();
    
            popupWindow.showAtLocation(popView, Gravity.BOTTOM,0,50);
        }
    
        //获得照片的输出保存Uri
        private Uri getImageCropUri() {
            File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
            if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
            return Uri.fromFile(file);
        }
    
         public void checkPermissionRequest() {
            new RxPermissions(this)
                    .request(permissionsGroup)
                    .subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
    
                        }
    
                        @Override
                        public void onNext(Boolean aBoolean) {
                            Log.d("amy", "onNext: "+aBoolean);
                        }
    
                        @Override
                        public void onError(Throwable e) {
    
                        }
    
                        @Override
                        public void onComplete() {
    
                        }
                    });
        }
    
    }
    

    相关文章

      网友评论

          本文标题:第三方开源库之 TakePhoto

          本文链接:https://www.haomeiwen.com/subject/wsxjeltx.html