Uri uri = getIntent().getData()
System.out.println("图片URI地址" + uri.toString())
Point point = DisplayUtil.getScreenPoint(this)
Bitmap bitmap = ImageUtils.decodeBitmapWithOrientationMax(ImageUtils.getFilePathByFileUri(this, uri), point.x, point.y, isPortrait)
//得到图片旋转角度 设置旋转角度
String path= ImageUtils.getFilePathByFileUri(this,uri)
int degrees=ImageUtils.getImageDegrees(path)
Bitmap bitmap1=ImageUtils.getRotateBitmap(bitmap,degrees)
System.out.println("图片path地址" + path)
System.out.println("图片角度" + degrees)
imageView.setImageBitmap(bitmap1)
/**
* 图片简单处理工具类
*/
public class ImageUtils {
/**
* 屏幕宽
*
* @param context
* @return
*/
public static int getWidth(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return dm.widthPixels;
}
/**
* 屏幕高
*
* @param context
* @return
*/
public static int getHeight(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return dm.heightPixels;
}
/**
* 根据文件Uri获取路径
*
* @param context
* @param uri
* @return
*/
public static String getFilePathByFileUri(Context context, Uri uri) {
String filePath = null;
Cursor cursor = context.getContentResolver().query(uri, null, null,
null, null);
if (cursor!=null && cursor.moveToFirst()) {
filePath = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}else{
filePath=uri.getPath();
}
return filePath;
}
/**
* 根据图片原始路径获取图片缩略图
*
* @param imagePath 图片原始路径
* @param width 缩略图宽度
* @param height 缩略图高度
* @return
*/
public static Bitmap getImageThumbnail(String imagePath, int width,
int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(imagePath, options);
if(bitmap == null){
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int rate = 1;
if (beWidth < beHeight) {
rate = beWidth;
} else {
rate = beHeight;
}
if (rate <= 0) {
rate = 1;
}
options.inSampleSize = rate;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imagePath, options);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
/**
* ��תBitmap
* @param b
* @param rotateDegree
* @return
*/
public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree){
Matrix matrix = new Matrix();
matrix.postRotate((float)rotateDegree);
Bitmap rotaBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, false);
return rotaBitmap;
}
public static Bitmap decodeBitmapWithOrientationMax(String pathName, int width, int height, boolean isPortrait) {
return decodeBitmapWithSize(pathName, width, height, true,isPortrait);
}
private static Bitmap decodeBitmapWithSize(String pathName, int width, int height,
boolean useBigger, boolean isPortrait) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inInputShareable = true;
options.inPurgeable = true;
BitmapFactory.decodeFile(pathName, options);
int targetDensity = UiUtils.getInstance().getContext().getResources().getDisplayMetrics().densityDpi;
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager=(WindowManager)UiUtils.getInstance().
getContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
int x=600;
int y=800;
options.inSampleSize = calculateInSampleSize(options, x, y);
double xSScale = ((double)options.outWidth) / ((double)x);
double ySScale = ((double)options.outHeight) / ((double)y);
double startScale = xSScale > ySScale ? xSScale : ySScale;
Log.d("MainActivity", "startScale:" + startScale);
options.inScaled = true;
if(options.outHeight*options.outWidth<x*y){
options.inDensity = (int) (targetDensity);
}else{
if(isPortrait){
options.inDensity = (int) (targetDensity*2);
}else{
options.inDensity = (int) (targetDensity*1.5);
}
}
options.inTargetDensity = targetDensity;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}
/**
* 计算采样率的大小
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
int height;
int width;
if(options.outWidth>options.outHeight){
height = options.outWidth;
width = options.outHeight;
}else{
height = options.outHeight;
width = options.outWidth;
}
int inSampleSize = 1;
System.out.println("ImageUtils calculateInSampleSize height:"+height+"..width:"+width+"...reqHeight:"+reqHeight+"..reqWidth:"+reqWidth);
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
if(inSampleSize==1){
inSampleSize=2;
}
}
Log.d("MainActivity", "inSampleSize:" + inSampleSize);
return inSampleSize;
}
public static int getImageDegrees(String pathName) {
int degrees = 0;
try {
ExifInterface exifInterface = new ExifInterface(pathName);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degrees = 270;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return degrees;
}
public static Bitmap imageWithFixedRotation(Bitmap bm, int degrees) {
if (bm == null || bm.isRecycled())
return null;
if (degrees == 0)
return bm;
final Matrix matrix = new Matrix();
matrix.postRotate(degrees);
Bitmap result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (result != bm)
bm.recycle();
return result;
}
}