前脸摄像头:录制视频上的划痕[三星Galaxy S3]

时间:2014-11-19 22:27:19

标签: android camera

最近,我成功使用我的三星galaxy S3在Android下打开并录制了一个视频,但是当我尝试使用前置摄像头时,我只是成功模糊,除了使用默认应用程序我看不到任何东西可以录制720p的视频,所以我的配置有问题你能看到哪里?

package com.example.magnificationvideo;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class MagnificationVideo extends Activity {
    private boolean isRecording = false;
    private Camera mCamera;
    private SurfaceView mPreview;
    private MediaRecorder mMediaRecorder;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    private String TAG = "";
    private Button captureButton;

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            int nbCameras = Camera.getNumberOfCameras();
            System.out.println(nbCameras);
            if(nbCameras > 1) {
                c = Camera.open(CameraInfo.CAMERA_FACING_FRONT);    
            } else {
                c = Camera.open();
            }
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
        return c; 
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magnification_video);

        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);

        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.video_preview);
        preview.addView(mPreview);

        captureButton = (Button) findViewById(R.id.mybutton);
        captureButton.setOnClickListener(recListener);
    }

    private OnClickListener recListener = new View.OnClickListener() {
         @Override
            public void onClick(View v) {
                if (isRecording) {
                    mMediaRecorder.stop();  
                    releaseMediaRecorder();
                    mCamera.lock();  
                    captureButton.setText("Capture");
                    isRecording = false;
                } else {
                    releaseCamera();
                    if (prepareVideoRecorder()) {
                        mMediaRecorder.start();

                        captureButton.setText("Stop");
                        isRecording = true;
                    } else {
                        releaseMediaRecorder();
                    }
                }
            }
    };

    private static Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }

    private static File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");

        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaRecorder(); 
        releaseCamera();
    }

    private void releaseMediaRecorder(){
        if (mMediaRecorder != null) {
            mMediaRecorder.reset();
            mMediaRecorder.release(); 
            mMediaRecorder = null;
            mCamera.lock(); 
        }
    }

    private void releaseCamera(){
        if (mCamera != null){
            mCamera.release(); 
            mCamera = null;
        }
    }

    private boolean prepareVideoRecorder(){
        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);
        mMediaRecorder = new MediaRecorder();

        mCamera.unlock();
        mMediaRecorder.setCamera(mCamera);

        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

        mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

        mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
        mMediaRecorder.setOrientationHint(90);

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        }
        return true;
    }

}

预览:

package com.example.magnificationvideo;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private String TAG = "";

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            Camera.Parameters parameters = mCamera.getParameters();

            parameters.setRotation(90);
            parameters.set( "cam_mode", 1 );   
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);

            mCamera.setParameters(parameters);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        if (mHolder.getSurface() == null){
          return;
        }

        try {
            mCamera.stopPreview();
        } catch (Exception e){
            e.getMessage();
        }

        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

这是结果的一个例子:

enter image description here

祝你好运, Zed13

1 个答案:

答案 0 :(得分:0)

mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P)); mrec.setVideoFrameRate(10);

编辑质量和帧率,应该是固定的