ImageUtils.java
5.21 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.huaheng.wms.facedete;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.IOException;
/**
* 作者:created by meixi
* 邮箱:13164716840@163.com
* 日期:2018/8/29 15
*/
public class ImageUtils {
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 halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
//Get Path Image file
public final static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
//Rotate Bitmap
public final static Bitmap rotate(Bitmap b, float degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2,
(float) b.getHeight() / 2);
Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
}
return b;
}
public static Bitmap getBitmap(String filePath, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = ImageUtils.calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
if (bitmap != null) {
try {
ExifInterface ei = new ExifInterface(filePath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = ImageUtils.rotate(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = ImageUtils.rotate(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = ImageUtils.rotate(bitmap, 270);
break;
// etc.
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}
public static Bitmap cropBitmap(Bitmap bitmap, Rect rect) {
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
Bitmap ret = Bitmap.createBitmap(w, h, bitmap.getConfig());
Canvas canvas = new Canvas(ret);
canvas.drawBitmap(bitmap, -rect.left, -rect.top, null);
bitmap.recycle();
return ret;
}
public static Bitmap cropFace(FaceResult face, Bitmap bitmap, int rotate) {
Bitmap bmp;
float eyesDis = face.eyesDistance();
PointF mid = new PointF();
face.getMidPoint(mid);
Rect rect = new Rect(
(int) (mid.x - eyesDis * 1.20f),
(int) (mid.y - eyesDis * 0.55f),
(int) (mid.x + eyesDis * 1.20f),
(int) (mid.y + eyesDis * 1.85f));
Bitmap.Config config = Bitmap.Config.RGB_565;
if (bitmap.getConfig() != null) config = bitmap.getConfig();
bmp = bitmap.copy(config, true);
switch (rotate) {
case 90:
bmp = ImageUtils.rotate(bmp, 90);
break;
case 180:
bmp = ImageUtils.rotate(bmp, 180);
break;
case 270:
bmp = ImageUtils.rotate(bmp, 270);
break;
}
bmp = ImageUtils.cropBitmap(bmp, rect);
return bmp;
}
}