protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GO_CAMERA:
if (resultCode == RESULT_CANCELED) {
return;
}
File picture = null;
Bitmap bitmap = null;
Uri uri = null;
if (data == null) {
picture = new File(Environment.getExternalStorageDirectory() + File.separator +ImageName);
uri = Uri.fromFile(picture);
} else {
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap imagBit = bundle.getParcelable("data");
//先判断该当前的片保存的时候是否旋转的角度
//获得保存图片的路径
String imagPath = getImagPath(mContext, ImageName);
bitmap = BitmapUtil.getRotateBitmap(imagPath, imagBit);
}
}
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
//上传阿里云验证身份证信息
Uri finalUri = uri;
//拍照直接上传服务器加水印
String imageAbsolutePath = URIUtils.getImageAbsolutePath(ConsigneeIdCardInformationActivity.this, finalUri);
Bitmap smallBitmap = BitmapUtil.getSmallBitmap(imageAbsolutePath, 350, 350);
Bitmap rotateBitmap = BitmapUtil.getRotateBitmap(imageAbsolutePath, smallBitmap);
Logger.i("相机的拍照的图片 ",BitmapUtil.bitmaoToString(rotateBitmap) )
} catch (IOException e) {
e.printStackTrace();
}
break;
case GO_ALBUM:
String fileSrc;
String columnName = null;
if (null == data){
return;
}
//下边的代码是获取正确的路径
if ("file".equals(data.getData().getScheme())) {
// 有些低版本机型返回的Uri模式为file
fileSrc = data.getData().getPath();
} else {
// Uri模型为content
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = this.getContentResolver().query(data.getData(), proj,
null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
fileSrc = cursor.getString(idx);
cursor.close();
}
Bitmap smallBitmap = BitmapUtil.getSmallBitmap(fileSrc, 350, 350);
Logger.i("imagePath "+fileSrc);
Logger.e("smallBitmap "+smallBitmap);
Bitmap rotateBitmap = BitmapUtil.getRotateBitmap(fileSrc, smallBitmap);
//展示图片之前去获取加水印的图片
Logger.i("选择相册的IDTYE " + IDtype + "图片名字 " + columnName);
switchBitmap(rotateBitmap);
break;
}
}
/*
* 读取图片的旋转角度 并且旋转为正确的角度
* @param path 图片的绝对路径
* @return 当前图片旋转的角度
*/
public static Bitmap getRotateBitmap(String path,Bitmap bitmap){
int degree=0;
//从指定库路径下读取图片,平获取起EXIF信息
try {
ExifInterface exifInterface=new ExifInterface(path);
//获取图片的旋转信息
int attributeInt = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (attributeInt){
case ExifInterface.ORIENTATION_ROTATE_90:
degree=90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree=180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree=270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.reset();
matrix.postRotate(degree);
Bitmap newBm = Bitmap.createBitmap(bitmap, 0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if (bitmap.isRecycled()) {
bitmap.recycle();
}
return newBm;
}