近来,发现登入后台管理系统,发现上传上来的认证视频太模糊了,于是乎,发现没有给MediaRecorder设置帧频率。
// 设置帧频率,录制视频会更加清晰
mRecorder.setVideoEncodingBitRate(5*1024*1024);
1.直接开撸,先打开相机,来获取想要的bitmap,关于为什么用到YuvImage:https://blog.csdn.net/illidantao/article/details/51366047
try {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
} catch (Exception e) {
e.printStackTrace();
}
try {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
} catch (Exception e) {
e.printStackTrace();
}
}
camera.setDisplayOrientation(90);
camera.startPreview();
}
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(final byte[] data, Camera camera) {
Camera.Size size = camera.getParameters().getPreviewSize();
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,
size.height, null);
if (image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, size.width, size.height),
80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(
stream.toByteArray(), 0, stream.size());
Matrix matrix = new Matrix();
matrix.reset();
matrix.setRotate(-90);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
Bitmap bitmap = bmp.copy(Bitmap.Config.RGB_565, true);
findFace(bitmap);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
安卓原生SDK中有个FaceDetector类,传入一个Bitmap 格式的数据;B,只能识别双眼距离大于20 像素的人脸像(当然,这个可在framework层中修改);C,只能检测出人脸的位置(双眼的中心点及距离),不能对人脸进行匹配(查找指定的脸谱)。下面的坐标实现具体的获取人脸的中心点坐标,通过FaceDetector.Face[1]传入要抓取的个数,如果想抓去多个的话Face[*]就可以了,findFaces(bitmap,face)得到一个int值,如果其大于1则代表成功那么就开始接下来的操作了。
public void findFace(Bitmap bitmap) {
FaceDetector faceDetector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 1);
FaceDetector.Face[] face = new FaceDetector.Face[1];
int faces = faceDetector.findFaces(bitmap, face);
if (faces > 0) {
camera.stopPreview();
preview = false;
camera.setPreviewCallback(null);
camera.release();
initMediaRecorder();
}
}
2.当获取验证成功以后,实现6秒录制视频。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.activity_uservideo);
surfaceview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceview.getHolder().setFixedSize(200, 200);
surfaceview.getHolder().addCallback(new SurfaceViewCallback());
}
public void initMediaRecorder() {
if (mRecorder == null) {
mRecorder = new MediaRecorder(); // Create MediaRecorder
}
try {
initCamera();
// 这两项需要放在setOutputFormat之前
mRecorder.setCamera(videocamera);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Set output file format
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// 这两项需要放在setOutputFormat之后
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// mRecorder.setOrientationHint(90);
// mRecorder.setVideoSize(320, 240);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
mRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
// 设置帧频率,录制视频会更加清晰
mRecorder.setVideoEncodingBitRate(5*1024*1024);
// mRecorder.setVideoFrameRate(20);
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
file = Constants.FOLDER_PHOTO + System.currentTimeMillis() + ".mp4";
mRecorder.setOutputFile(file);
mRecorder.prepare();
mRecorder.start(); // Recording is now started
setCountBack();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void initCamera() {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras(); // get cameras number
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo); // get camerainfo
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// 代表摄像头的方位,目前有定义值两个分别为CAMERA_FACING_FRONT前置和CAMERA_FACING_BACK后置
try {
videocamera = Camera.open(camIdx);
videocamera.setDisplayOrientation(90);
videocamera.cancelAutoFocus();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
videocamera.unlock();
}
void setCountBack() {
timeCount = 5;
timetext.setText(timeCount + "秒");
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (timeCount > 0) {
timetext.setText(--timeCount + "秒");
handler.postDelayed(this, 1000);
return;
}
timetext.setText("");
if (mRecorder == null) {
return;
}
try {
mRecorder.stop();
//上传视频
uploadVideo();
} catch (Exception e) {
ToastUtil.showToast(AuthRecordingVideoActivity.this, getString(R.string.error_recoder));
}
}
}, 1000);
}
关于SurfaceView预览Camera拉伸问题:https://blog.csdn.net/illidantao/article/details/51366047