android 拍照后通过Intent传参数控制照片方向自动旋转


第一种方案,核心代码:

1
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public void takePhoto() {

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File folder = new File(Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.app_name));

if (!folder.exists()) {
folder.mkdir();
}
final Calendar c = Calendar.getInstance();
String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-" + ((c.get(Calendar.MONTH)) + 1) + "-" + c.get(Calendar.YEAR) + " "
+ c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
imageUrl = String.format(Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.app_name) + "/%s.png", "Accident_Info(" + new_Date + ")");
File photo = new File(imageUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", "false");
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

AccidentInfo.this.startActivityForResult(intent, CAMERA_PIC_REQUEST);
}


private Bitmap callTempMethod(String tempPath) {
if (tempPath != null) {
Uri selectedImage = Uri.parse(tempPath);
if (!TextUtils.isEmpty(tempPath)) {
LogMessage.i(TAG, "LLLpath::" + tempPath);
File file = new File(tempPath);
if (selectedImage.toString().startsWith("content")) {
return DealershipApplication.decodeStrem(file, selectedImage, mContext);
} else {
if (!file.isDirectory()) {
return DealershipApplication.decodeFile(file, 10, 0);
} else {
return null;
}
}
} else
return null;
} else {
return null;
}
}


public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight)
{

// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}


public static Bitmap getSampleBitmapFromFile(String bitmapFilePath,
int reqWidth, int reqHeight) throws FileNotFoundException
{

// calculating image size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream( new FileInputStream(new File(bitmapFilePath)), null, options);

int scale = calculateInSampleSize(options, reqWidth, reqHeight);

BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;

return BitmapFactory.decodeStream(new FileInputStream(new File( bitmapFilePath)), null, o2);
}

第二种老的方案是通过以下3步完成:

获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转
*/

int degree = ImageDispose.readPictureDegree(file.getAbsolutePath());
把图片旋转为正的方向

/**
* 把图片旋转为正的方向
*/

bitmap = ImageDispose.rotaingImageView(degree, bitmap);
旋转图片

/**
* 旋转图片
* @param angle
* @param bitmap
* @return Bitmap
*/

public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();;
matrix.postRotate(angle);
System.out.println("angle2=" + angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}

总的说来第一种方式更方便,直接通过intent传递,让系统自动完成操作。

文章目录