一 依赖
compile 'com.jph.takephoto:takephoto_library:4.0.3'
compile 'com.yanzhenjie:permission:1.0.5'
二 权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
三 实现
(一) activity中
public class MainActivity extends TakePhotoActivity {
//UIs
private Button turnBtn; //跳转AnotherActivity Button
private Button takeFromCameraBtn, takeFromGalleyBtn; //拍照以及从相册中选取Button
private ImageView imageView; //图片展示ImageView
//TakePhoto
private TakePhoto takePhoto;
private CropOptions cropOptions; //裁剪参数
private CompressConfig compressConfig; //压缩参数
private Uri imageUri; //图片保存路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//申请相关权限
initPermission();
initData(); //设置压缩、裁剪参数
turnBtn = (Button) findViewById(R.id.turn_to_another_activity);
turnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AnotherActivity.class));
}
});
takeFromCameraBtn = (Button) findViewById(R.id.take_from_camera);
takeFromCameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageUri = getImageCropUri();
//拍照并裁剪
takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
//仅仅拍照不裁剪
//takePhoto.onPickFromCapture(imageUri);
}
});
takeFromGalleyBtn = (Button) findViewById(R.id.take_from_galley);
takeFromGalleyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageUri = getImageCropUri();
//从相册中选取图片并裁剪
takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
//从相册中选取不裁剪
//takePhoto.onPickFromGallery();
}
});
imageView = (ImageView) findViewById(R.id.image_view);
}
@Override
public void takeSuccess(TResult result) {
super.takeSuccess(result);
String iconPath = result.getImage().getOriginalPath();
//Toast显示图片路径
Toast.makeText(this, "imagePath:" + iconPath, Toast.LENGTH_SHORT).show();
//Google Glide库 用于加载图片资源
Glide.with(this).load(iconPath).into(imageView);
}
@Override
public void takeFail(TResult result, String msg) {
super.takeFail(result, msg);
Toast.makeText(MainActivity.this, "Error:" + msg, Toast.LENGTH_SHORT).show();
}
@Override
public void takeCancel() {
super.takeCancel();
}
private void initPermission() {
// 申请权限。
AndPermission.with(this)
.requestCode(100)
.permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.send();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// 只需要调用这一句,其它的交给AndPermission吧,最后一个参数是PermissionListener。
AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
}
//权限申请回调接口
private PermissionListener listener = new PermissionListener() {
@Override
public void onSucceed(int requestCode, List<String> grantedPermissions) {
// 权限申请成功回调。
if(requestCode == 100) {
// TODO 相应代码。
//do nothing
}
}
@Override
public void onFailed(int requestCode, List<String> deniedPermissions) {
// 权限申请失败回调。
// 用户否勾选了不再提示并且拒绝了权限,那么提示用户到设置中授权。
if (AndPermission.hasAlwaysDeniedPermission(MainActivity.this, deniedPermissions)) {
// 用自定义的提示语
AndPermission.defaultSettingDialog(MainActivity.this, 103)
.setTitle("权限申请失败")
.setMessage("我们需要的一些权限被您拒绝或者系统发生错误申请失败,请您到设置页面手动授权,否则功能无法正常使用!")
.setPositiveButton("好,去设置")
.show();
}
}
};
private void initData() {
获取TakePhoto实例
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); //设置为需要压缩
}
//获得照片的输出保存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);
}
}
(二) Fragment 中实现
public class FragmentTakePhoto extends TakePhotoFragment {
private View view;
//UIs
private Button takeFromCameraBtn, takeFromGalleyBtn;
private ImageView imageView;
//TakePhoto
private TakePhoto takePhoto;
private CropOptions cropOptions; //裁剪参数
private CompressConfig compressConfig; //压缩参数
private Uri imageUri; //图片保存路径
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_takephoto,container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData(); //设置压缩、裁剪参数
takeFromCameraBtn = (Button) view.findViewById(R.id.take_from_camera);
takeFromCameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageUri = getImageCropUri();
//拍照并裁剪
takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
//仅仅拍照不裁剪
//takePhoto.onPickFromCapture(imageUri);
}
});
takeFromGalleyBtn = (Button) view.findViewById(R.id.take_from_galley);
takeFromGalleyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageUri = getImageCropUri();
//从相册中选取图片并裁剪
takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
//从相册中选取不裁剪
//takePhoto.onPickFromGallery();
}
});
imageView = (ImageView) view.findViewById(R.id.image_view);
}
@Override
public void takeSuccess(TResult result) {
super.takeSuccess(result);
String iconPath = result.getImage().getOriginalPath();
//Toast显示图片路径
Toast.makeText(getActivity(), "imagePath:" + iconPath, Toast.LENGTH_SHORT).show();
//Google Glide库 用于加载图片资源
Glide.with(this).load(iconPath).into(imageView);
}
@Override
public void takeFail(TResult result, String msg) {
super.takeFail(result, msg);
Toast.makeText(getActivity(), "Error:" + msg, Toast.LENGTH_SHORT).show();
}
@Override
public void takeCancel() {
super.takeCancel();
}
private void initData() {
获取TakePhoto实例
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); //设置为需要压缩
}
//获得照片的输出保存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);
}
}