package com.hdl.widget.cropimage;
|
|
|
import android.content.ContentResolver;
|
import android.content.Intent;
|
import android.database.Cursor;
|
import android.graphics.Bitmap;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.os.Environment;
|
import android.provider.MediaStore;
|
import android.support.v4.content.FileProvider;
|
import android.text.TextUtils;
|
import android.text.format.DateFormat;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import com.hdl.widget.HDLUtlisXM;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Calendar;
|
import java.util.Locale;
|
|
|
|
|
public class HDLCropImageActivity extends CheckPermissionsCropImageActivity {
|
|
|
private static final int ACTION_TYPE_PHOTO = 0;
|
private final int CAMERA = 101;
|
private final int ALBUM = 102;
|
private final int CUPREQUEST = 103;
|
|
private String mPicturePath = "hdl_headPicture";
|
private File mOutImage;
|
private Uri mImageUri;
|
private Uri mUritempFile;
|
// private Bitmap imageBitmap;
|
private String mCropPicturePath;
|
private String mPictureName = "hdl_headPicture";
|
private int mRATIO_X = 1;
|
private int mRATIO_Y = 1;
|
private int mOutputY = 200;
|
|
|
public interface OnCropImageeCallback {
|
void OnSaveCallback(String picturePath);
|
}
|
/**
|
* 2.接口实现回调
|
*/
|
public static OnCropImageeCallback mOnCropImageeCallback;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
// setContentView(R.layout.hdl_widget_activity_crop_image);
|
|
// Bundle mExtras = getIntent().getExtras();
|
// if(mExtras != null) {
|
// CropImageBean mCropImageBean = (CropImageBean) getIntent().getSerializableExtra("CropImageBean");
|
// if(mCropImageBean != null){
|
// type = mCropImageBean.getTypeID();
|
// String mmPictureName = mCropImageBean.getPictureName();
|
// if(!TextUtils.isEmpty(mmPictureName)){
|
// mPictureName = mmPictureName;
|
// }
|
// mRATIO_X = mCropImageBean.getRATIO_X();
|
// mRATIO_Y = mCropImageBean.getRATIO_Y();
|
// mOutputY = mCropImageBean.getOutputY();
|
// if(mRATIO_X <= 0){
|
// mRATIO_X = 1;
|
// }
|
// if(mRATIO_Y <= 0){
|
// mRATIO_Y = 1;
|
// }
|
// if(mOutputY <= 0 || mOutputY > 400){
|
// mRATIO_Y = 300;
|
// }
|
//
|
// }
|
// }
|
|
|
|
Bundle extras = getIntent().getExtras();
|
int type = 0;
|
if(extras != null){
|
type = extras.getInt(HDLUtlisXM.CROP_TYPE_KEY);
|
String mmPictureName = extras.getString(HDLUtlisXM.CROP_NAME_KEY);
|
if(!TextUtils.isEmpty(mmPictureName)){
|
mPictureName = mmPictureName;
|
}
|
mRATIO_X = extras.getInt(HDLUtlisXM.CROP_RATIO_X_KEY);
|
mRATIO_Y = extras.getInt(HDLUtlisXM.CROP_RATIO_Y_KEY);
|
mOutputY = extras.getInt(HDLUtlisXM.CROP_OUTPUT_Y_KEY);
|
if(mRATIO_X <= 0){
|
mRATIO_X = 1;
|
}
|
if(mRATIO_Y <= 0){
|
mRATIO_Y = 1;
|
}
|
if(mOutputY <= 0 || mOutputY > 200){
|
mOutputY = 200;
|
}
|
|
}
|
|
|
|
|
if(type == CAMERA){
|
setMobilePhones();
|
}else {
|
setLocalPhoto();
|
}
|
|
|
|
}
|
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
if (null != mOnCropImageeCallback) {
|
mOnCropImageeCallback = null;
|
}
|
}
|
|
/**
|
* 相册
|
*/
|
private void setLocalPhoto() {
|
Intent albumIntent = new Intent(Intent.ACTION_PICK, null);
|
albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
|
startActivityForResult(albumIntent, ALBUM);
|
}
|
|
/**
|
* 拍照
|
*/
|
private void setMobilePhones() {
|
//获得项目缓存路径
|
String sdPath = getExternalCacheDir().getPath();
|
//根据时间随机生成图片名
|
String photoName = new DateFormat().format("yyyyMMddhhmmss",
|
Calendar.getInstance(Locale.CHINA)) + ".jpg";
|
mPicturePath = sdPath + "/" + photoName;
|
mOutImage = new File(mPicturePath);
|
//如果是7.0以上 那么就把uir包装
|
if (Build.VERSION.SDK_INT >= 24) {
|
mImageUri = FileProvider.getUriForFile(this, HDLUtlisXM.MAuthority_NAME , mOutImage);
|
} else {
|
//否则就用老系统的默认模式
|
mImageUri = Uri.fromFile(mOutImage);
|
}
|
//启动相机
|
Intent intent = new Intent();
|
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
|
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
|
startActivityForResult(intent, CAMERA);
|
}
|
|
|
/**
|
* 选择图片后的回调
|
*
|
* @param requestCode
|
* @param resultCode
|
* @param data
|
*/
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
if (resultCode != RESULT_OK) {
|
finishActivity();
|
return;
|
}
|
switch (requestCode) {
|
|
// 裁剪相机照片
|
case CAMERA:
|
setCropPhoto();
|
break;
|
//裁剪本地相册
|
case ALBUM:
|
Uri data1 = data.getData();
|
if (data1 != null) {
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
|
mPicturePath = data1.toString();
|
} else {
|
mPicturePath = data1.getPath();
|
}
|
// Uri --> Path
|
if (mPicturePath.contains("content://")) {
|
mPicturePath = getFilePathFromContentUri(data1, getContentResolver());
|
}
|
mOutImage = new File(mPicturePath);
|
setCropPhoto();
|
}else {
|
finishActivity();
|
}
|
break;
|
|
//裁剪完成
|
case CUPREQUEST:
|
if (data == null) {
|
finishActivity();
|
return;
|
}
|
Bundle extras = data.getExtras();
|
|
if (Build.MANUFACTURER.contains("Xiaomi")) {
|
if (mUritempFile != null) {
|
mCropPicturePath = mUritempFile.getPath();
|
} else {
|
mCropPicturePath = "";
|
}
|
} else {
|
if (extras != null) {
|
Bitmap photo = extras.getParcelable("data");
|
mCropPicturePath = saveImage(mPictureName ,photo);
|
} else {
|
mCropPicturePath = "";
|
}
|
}
|
|
finishActivity();
|
// if (!TextUtils.isEmpty(mCropPicturePath)) {
|
// imageBitmap = BitmapFactory.decodeFile(s);
|
// }
|
break;
|
default:
|
finishActivity();
|
break;
|
}
|
}
|
|
private void setCropPhoto() {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
//如果是7.0剪裁图片 同理 需要把uri包装
|
//通过FileProvider创建一个content类型的Uri
|
Uri inputUri = FileProvider.getUriForFile(this,
|
HDLUtlisXM.MAuthority_NAME , mOutImage);
|
|
startPhotoZoom(inputUri);//设置输入类型
|
} else {
|
Uri inputUri = Uri.fromFile(mOutImage);
|
startPhotoZoom(inputUri);
|
}
|
}
|
|
//裁剪
|
private void startPhotoZoom(Uri uri) {
|
Intent intent = new Intent("com.android.camera.action.CROP");
|
intent.setDataAndType(uri, "image/*");
|
//sdk>=24
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
//去除默认的人脸识别,否则和剪裁匡重叠
|
intent.putExtra("noFaceDetection", false);
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
|
}
|
// 设置裁剪
|
intent.putExtra("crop", "true");
|
// aspectX aspectY 宽高的比例
|
//华为特殊处理 不然会显示圆
|
if (Build.MANUFACTURER.contains("HUAWEI")) {
|
if(mRATIO_X == mRATIO_Y) {
|
intent.putExtra("aspectX", 9998);
|
intent.putExtra("aspectY", 9999);
|
}else {
|
intent.putExtra("aspectX", mRATIO_X);
|
intent.putExtra("aspectY", mRATIO_Y);
|
}
|
} else {
|
intent.putExtra("aspectX", mRATIO_X);
|
intent.putExtra("aspectY", mRATIO_Y);
|
}
|
// outputX outputY 是裁剪图片宽高
|
float mRATIO = ((float)mRATIO_X) /((float)mRATIO_Y);
|
int outputX = (int)((float)mOutputY * mRATIO);
|
intent.putExtra("outputX",outputX);
|
intent.putExtra("outputY", mOutputY);
|
//miui系统 特殊处理 return-data的方式只适用于小图。
|
if (Build.MANUFACTURER.contains("Xiaomi")) { //裁剪后的图片Uri路径,mUritempFile为Uri类变量
|
mUritempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + mPictureName + ".jpg");
|
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUritempFile);
|
} else {
|
intent.putExtra("return-data", true);
|
}
|
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
|
startActivityForResult(intent, CUPREQUEST);
|
|
|
//
|
// Intent intent = new Intent("com.android.camera.action.CROP");
|
// intent.setDataAndType(uri, "image/*");
|
// intent.putExtra("crop", "true");
|
// intent.putExtra("aspectX", 1);
|
// intent.putExtra("aspectY", 1);
|
// intent.putExtra("outputX", 300);
|
// intent.putExtra("outputY", 300);
|
// intent.putExtra("scale", true);
|
// // intent.putExtra("return-data", false);
|
// intent.putExtra("return-data", true);
|
// intent.putExtra(MediaStore.EXTRA_OUTPUT, mUritempFile);
|
// intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
|
// intent.putExtra("noFaceDetection", true); // no face detection
|
// startActivityForResult(intent, CROP_REQUEST_CODE);
|
}
|
|
/**
|
* Gets the corresponding path to a file from the given content:// URI
|
* @param selectedVideoUri The content:// URI to find the file path from
|
* @param contentResolver The content resolver to use to perform the query.
|
* @return the file path as a string
|
*/
|
public static String getFilePathFromContentUri(Uri selectedVideoUri,
|
ContentResolver contentResolver) {
|
String filePath;
|
String[] filePathColumn = {MediaStore.MediaColumns.DATA};
|
|
Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
|
// 也可用下面的方法拿到cursor
|
// Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);
|
|
cursor.moveToFirst();
|
|
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
|
filePath = cursor.getString(columnIndex);
|
cursor.close();
|
return filePath;
|
}
|
|
|
public String saveImage(String name, Bitmap bmp) {
|
File appDir = new File(Environment.getExternalStorageDirectory().getPath());
|
if (!appDir.exists()) {
|
appDir.mkdir();
|
}
|
String fileName = name + ".png";
|
File file = new File(appDir, fileName);
|
// uploadHeadPic(file);
|
|
try {
|
FileOutputStream fos = new FileOutputStream(file);
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
fos.flush();
|
fos.close();
|
|
// uploadHeadPic(file);
|
return file.getAbsolutePath();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
|
private void finishActivity(){
|
if (null != mOnCropImageeCallback) {
|
mOnCropImageeCallback.OnSaveCallback(mCropPicturePath);
|
}
|
finish();
|
}
|
|
}
|