FaceCamera.java
9.84 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.megvii.idcardlib.util;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Build;
import android.view.Surface;
import android.widget.RelativeLayout;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 照相机工具类
*/
public class FaceCamera {
public Camera mCamera;
public int cameraWidth;
public int cameraHeight;
public int cameraId = 1;//前置摄像头
public FaceCamera() {
}
/**
* 打开相机
*/
public Camera openCamera(Activity activity,int cameraId) {
try {
this.cameraId=cameraId;
mCamera = Camera.open(cameraId);
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
Camera.Parameters params = mCamera.getParameters();
Camera.Size bestPreviewSize = calBestPreviewSize(
mCamera.getParameters(), 640, 480);
cameraWidth = bestPreviewSize.width;
cameraHeight = bestPreviewSize.height;
params.setPreviewSize(cameraWidth, cameraHeight);
mCamera.setDisplayOrientation(getCameraAngle(activity));
mCamera.setParameters(params);
return mCamera;
} catch (Exception e) {
return null;
}
}
/**
* 通过屏幕参数、相机预览尺寸计算布局参数
*/
public RelativeLayout.LayoutParams getLayoutParam() {
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
float scale = Math.min(Screen.mWidth * 1.0f / previewSize.height,
Screen.mHeight * 1.0f / previewSize.width);
int layout_width = (int) (scale * previewSize.height);
int layout_height = (int) (scale * previewSize.width);
RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
layout_width, layout_height);
return layout_params;
}
/**
* 如果需要全屏预览,打开下列注释,并注释掉上方getLayoutParam() 方法,openCamera()中调节分辨率到1280 X 720,
* 在部分华为机型(存在底部虚拟键的状态栏)需要设置 LivenessActivity 的Theme 为:
* android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor" 才可以预览正常。
*/
// public RelativeLayout.LayoutParams getLayoutParam() {
// float scale = cameraWidth * 1.0f / cameraHeight;
// int layout_width = Screen.mWidth;
// int layout_height = (int) (layout_width * scale);
// if (Screen.mWidth >= Screen.mHeight) {
// layout_height = Screen.mHeight;
// layout_width = (int) (layout_height / scale);
// }
//
// RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
// layout_width, layout_height);
// layout_params.addRule(RelativeLayout.CENTER_HORIZONTAL);// 设置照相机水平居中
// return layout_params;
// }
/**
* 开始检测脸
*
*/
public void actionDetect(Camera.PreviewCallback mActivity) {
try {
if (mCamera != null) {
mCamera.setPreviewCallback(mActivity);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Start preview.
*
* @param surfaceTexture the surface texture
*/
public void startPreview(SurfaceTexture surfaceTexture) {
try {
if (mCamera != null) {
try {
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Close camera.
*/
public void closeCamera() {
try {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通过传入的宽高算出最接近于宽高值的相机大小
*/
private Camera.Size calBestPreviewSize(Camera.Parameters camPara,
final int width, final int height) {
List<Camera.Size> allSupportedSize = camPara.getSupportedPreviewSizes();
ArrayList<Camera.Size> widthLargerSize = new ArrayList<Camera.Size>();
for (Camera.Size tmpSize : allSupportedSize) {
if (tmpSize.width > tmpSize.height) {
widthLargerSize.add(tmpSize);
}
}
Collections.sort(widthLargerSize, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size lhs, Camera.Size rhs) {
int off_one = Math.abs(lhs.width * lhs.height - width * height);
int off_two = Math.abs(rhs.width * rhs.height - width * height);
return off_one - off_two;
}
});
return widthLargerSize.get(0);
}
public Camera getCameraSafely(int cameraId) {
Camera camera = null;
try {
camera = Camera.open(cameraId);
} catch (Exception e) {
camera = null;
}
return camera;
}
public RelativeLayout.LayoutParams getParams(Camera camera) {
Camera.Parameters camPara = camera.getParameters();
Camera.Size bestPreviewSize = calBestPreviewSize(camPara,
Screen.mWidth, Screen.mHeight);
cameraWidth = bestPreviewSize.width;
cameraHeight = bestPreviewSize.height;
camPara.setPreviewSize(cameraWidth, cameraHeight);
camera.setParameters(camPara);
float scale = bestPreviewSize.width / bestPreviewSize.height;
RelativeLayout.LayoutParams layoutPara = new RelativeLayout.LayoutParams(
(int) (bestPreviewSize.width),
(int) (bestPreviewSize.width / scale));
layoutPara.addRule(RelativeLayout.CENTER_HORIZONTAL);// 设置照相机水平居中
return layoutPara;
}
public Bitmap getBitMap(byte[] data, Camera camera, boolean mIsFrontalCamera) {
int width = camera.getParameters().getPreviewSize().width;
int height = camera.getParameters().getPreviewSize().height;
YuvImage yuvImage = new YuvImage(data, camera.getParameters()
.getPreviewFormat(), width, height, null);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 80,
byteArrayOutputStream);
byte[] jpegData = byteArrayOutputStream.toByteArray();
// 获取照相后的bitmap
Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegData, 0,
jpegData.length);
Matrix matrix = new Matrix();
matrix.reset();
if (mIsFrontalCamera) {
matrix.setRotate(-90);
} else {
matrix.setRotate(90);
}
tmpBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(),
tmpBitmap.getHeight(), matrix, true);
tmpBitmap = tmpBitmap.copy(Bitmap.Config.ARGB_8888, true);
int hight = tmpBitmap.getHeight() > tmpBitmap.getWidth() ? tmpBitmap
.getHeight() : tmpBitmap.getWidth();
float scale = hight / 800.0f;
if (scale > 1) {
tmpBitmap = Bitmap.createScaledBitmap(tmpBitmap,
(int) (tmpBitmap.getWidth() / scale),
(int) (tmpBitmap.getHeight() / scale), false);
}
return tmpBitmap;
}
/**
* 获取照相机旋转角度
*/
public int getCameraAngle(Activity activity) {
int rotateAngle = 90;
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotateAngle = (info.orientation + degrees) % 360;
rotateAngle = (360 - rotateAngle) % 360; // compensate the mirror
} else { // back-facing
rotateAngle = (info.orientation - degrees + 360) % 360;
}
return rotateAngle;
}
public static boolean hasFrontFacingCamera() {
final int CAMERA_FACING_BACK = 1;
return checkCameraFacing(CAMERA_FACING_BACK);
}
private static boolean checkCameraFacing(final int facing) {
if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
return false;
}
final int cameraCount = Camera.getNumberOfCameras();
CameraInfo info = new CameraInfo();
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, info);
if (facing == info.facing) {
return true;
}
}
return false;
}
/**
* Gets sdk version.
*/
public static int getSdkVersion() {
return Build.VERSION.SDK_INT;
}
}