Android Video Recording
preview
Camera and SurfaceView combine to implement preview
public CameraHepler{
/* * Turn on the default camera */
public static Camera getDefaultCameraInstance(a){
return Camera.open();
}
/* * Get the front camera */
public static Camera getDefaultFrontFacingCameraInstance(a)
{
return getDefaultCamera(Camera.Info.CAMERA_FACING_FRONT);
}
/* * Get the rear camera */
public static Camera getDefaultFrontBackCameraInstance(a)
{
return getDefaultCamera(Camera.Info.CAMERA_FACING_FRONT);
}
/* * Return null to indicate that no camera is available */
public static Camera getDefaultCamera(int position){
int numberOfCameras = Camera.getNumberOfCameras();
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for(int i = 0; i < numberOfCameras; i++){
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == position){
returnCamera.open(i); }}return null;
}
/* * Get the most appropriate preview size */
public static Camera.Size getOptimalPreviewSize(List<Camera.size> sizes, int w, int h){
// Take a very small value
final double ASPECT_TOLERANCE = 0.1;
double targetRadio = (double)w/h;
if(w==0 && h==0) {return null;
}
int targetHeight = h ;
Camera.size optimalSize = null;
double minDiff = Double.Max_VALUE;
for(Camera.size size: sizes){
double radio = (double)w/h;
if(Math.abs(radio - targetRadio) > ASPECT_TOLERANCE){
continue;
}
if(Math.abs(size.height - targetHeight) < minDiff){ minDiff = Math.abs(size.height - targetHeight); optimalSize = size; }}if(optimalSize == null) {for(Camera.size size : sizes){
minDiff = Double.Max_VALUE;
if(Math.abs(size.height - targetHeight)<minDiff){ minDiff = Math.abs(size.height - targetHeight); optimalSize = size; }}}returnoptimalSize; }}Copy the code
After obtaining the available camera, configure the preview data, note ⚠️, which will be called after the SurfaceView has loaded
private boolean prepareCamera(SurfaceHoler holder){
// Get the camera
Camera mCamera = CameraHepler.getDefaultFrontFacingCameraInstance();
if(mCamera == null) {return false;
}
Camera.Parameters parameters = mCamera.getParameters();
// Get the size supported by the Camera preview
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
// Find the most appropriate preview size from the supported sizes
Camera.Size previewSize = CameraHelper.getOptimalPreviewSize(sizes, surfaceView.getWidth, surfaceView.getHeight);
mCamera.setDisplayOrientation(90);
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(parameters)
try{
It is best to call surfaceCread(SurfaceHolder Holer) to ensure that holer is available
//Camera displays the data container
mCamera.setPreviewDisplay(holder);
mCamera.setPreviewCallback(null);
// Start preview
mCamera.startPreview();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
Copy the code
After the success of the Camera configuration must be loaded at SurfaceHolder to set mCamera. SetPreviewDisplay (holder), so make sure, however, has been loading the SurfaceHolder?
SurfaceHolder surfaceHolder = surfaceView.getHolder();
// To display other views in the preview screen, you need to do the following
surfaceView.setZOrderOnTop(false);
surfaceHoler.addCallBack(new SurfaceHoler.Callback{
@override
public void surfaceCreated(SurfaceHolder holder){
prepareCamera(holder);
}
@override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}@override
public void surfaceDestory(SurfaceHolder holder){
// Todo releases resources}});Copy the code
MeidaRecorder records the video
Before recording the video, call Camera unlock
mCamera.unlock();
// Set the path to store the video
String path = "";
File videoFile = new File(path);
if(! videoFile.exists()){ video.getPartentFile().mikrs(); }// Create the MediaRecorder instance
MediaRecorder mediaRecorder = new MediaRecorder();
// Set the audio source
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Set the video source
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOrientation(270);
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
// Set the quality of the video to save
CamCorderProfile camcorderProfile =
CamCorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_LOW);
mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
// Must be called before starting
mediaRecorder.prepare();
// It is best to start on the worker thread
mediaRecorder.start();
Copy the code