一、简介
TakePhoto 是一款用于在 Android 设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本 4.0.3。
- 使用自带的相机 APP 拍照
- 系统相册选择照片
- 系统相机或相册获取的照片裁剪
二、效果图
三、使用
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'
2. 清单文件中添加权限
<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"/>
3. popup_take_photo.xml
<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>
4. 代码逻辑
(1) 继承 TakePhotoActivity
(2) 重写 takeSuccess,takeFail,takeCancel 方法
(3) 设置动态权限,适配6.0以上设备
public class PersonalInfoActivity extends TakePhotoActivity {
(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};
public void onCreate( 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();
}
({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;
}
}
public void takeSuccess(TResult result) {
super.takeSuccess(result);
String iconPath = result.getImage().getOriginalPath();
Glide.with(this).load(iconPath).into(headImg);
}
public void takeFail(TResult result, String msg) {
super.takeFail(result, msg);
}
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() {
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() {
public void onClick(View v) {
if (popupWindow != null) {
popupWindow.dismiss();
}
imageUri = getImageCropUri();
//仅仅拍照不裁剪
// takePhoto.onPickFromCapture(imageUri);
//拍照并裁剪
takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
}
});
selPhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (popupWindow != null) {
popupWindow.dismiss();
}
imageUri = getImageCropUri();
//从相册中选取不裁剪
// takePhoto.onPickFromGallery();
//从相册中选取图片并裁剪
takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
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>() {
public void onSubscribe(Disposable d) {
}
public void onNext(Boolean aBoolean) {
Log.d("amy", "onNext: "+aBoolean);
}
public void onError(Throwable e) {
}
public void onComplete() {
}
});
}
}