Home>

As the title suggests, I want to start the camera for SurfaceView at the same time as starting the application and always display the preview, but I am using MediaRecorder to have the recording function.
I've been using Android for a little over two months, so I don't know anything, but as a feeling,
I feel that there is a difference between using the Camera API and using the Media Recorder API. With the current code, there are several points that we would like to improve, so please help us to improve it.

Points to be improved
(1) I want to always display the camera image on the Surface View from the time of startup (even after recording ends)
(2) I want to acquire the image acquired by the camera from the in-camera (sub-camera?)
③ I want to implement an autofocus function and always use autofocus.

(2) and (3) have found multiple samples that depend largely on the Camera API, but I would like to improve them somehow. Thank you.

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
static MediaRecorder mRecorder;
static Camera mCamera;
static SurfaceHolder mHolder;
Button button1;
TextView text1;
SurfaceView mSurfaceView1;
static boolean isRecording= false;
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    mRecorder= new MediaRecorder ();
    setContentView (R.layout.activity_main);
    initRecorder ();
    mSurfaceView1= (SurfaceView) findViewById (R.id.surfaceView1);
    button1= (Button) findViewById (R.id.button1);
    text1= (TextView) findViewById (R.id.textView1);
    text1.setText ("Now Standing By ...");
    mHolder= mSurfaceView1.getHolder ();
    mHolder.addCallback (this);
    button1.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick (View v) {
            if (isRecording) {
                mRecorder.stop ();
                isRecording= false;
                initRecorder ();
                prepareRecorder ();
                text1.setText ("Stop Recording");
                button1.setText ("Rec ●");
            } else {
                text1.setText ("Now Recording");
                isRecording= true;
                mRecorder.start ();
                button1.setText ("Stop Rec");
            }
        }
    });
}
public Camera getCameraInstance () {
    mCamera= null;
    try {
        mCamera= Camera.open (1);
    } catch (Exception e) {
        e.printStackTrace ();
    }
    return mCamera;
}
private void initRecorder () {
    mRecorder.setAudioSource (MediaRecorder.AudioSource.CAMCORDER);
    mRecorder.setVideoSource (MediaRecorder.VideoSource.CAMERA);
    mRecorder.setOutputFormat (MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AAC);
    mRecorder.setVideoEncoder (MediaRecorder.VideoEncoder.H264);
          //recorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH));
    String timeStamp= new SimpleDateFormat ("yyyyMMdd_HHmmss"). format (new Date ());
    File pathExternalPublicDir= Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DCIM + File.separator + "VID_" + timeStamp + ".mp4");
    String dir= pathExternalPublicDir.getPath ();
    mRecorder.setOutputFile (dir);
    mRecorder.setMaxDuration (60 * 1000);
    mRecorder.setMaxFileSize (50 * 1000 * 1000);
}
private void prepareRecorder () {
    mRecorder.setPreviewDisplay (mHolder.getSurface ());
    try {
        mRecorder.prepare ();
    } catch (IllegalStateException e) {
        e.printStackTrace ();
        finish ();
    } catch (IOException e) {
        e.printStackTrace ();
        finish ();
    }
}
@Override
public void surfaceCreated (SurfaceHolder holder) {
    prepareRecorder ();
    try {
        Camera.Parameters params= mCamera.getParameters ();
        List <
String >
 focusList= params.getSupportedFocusModes ();
        for (int i= 0; i <
 focusList.size (); i ++) {
            Log.d ("CameraFocusMode:", focusList.get (i));
        }params.setFocusMode (Camera.Parameters.FOCUS_MODE_INFINITY); mCamera.setParameters (params);
        mCamera.setPreviewDisplay (holder);
        mCamera.startPreview ();
        mCamera.autoFocus (mAutoFocusListener);
    } catch (Exception e) {e.printStackTrace ();}
}
private Camera.AutoFocusCallback mAutoFocusListener= new Camera.AutoFocusCallback () {
    public void onAutoFocus (boolean success, Camera camera) {
        camera.takePicture (null, null, null);
    }
};;
@Override
public void surfaceChanged (SurfaceHolder holder, int format, int w, int h) {
}
@Override
public void surfaceDestroyed (SurfaceHolder holder) {
    if (isRecording) {
        mRecorder.stop ();
        isRecording= false;
    }
    mRecorder.release ();
    finish ();
}

(partial excerpt)

  • Answer # 1

    It's a fairly old question, but I'd like to give some hints to those who come here.

    With the current Android X version, you can easily do various things with Camera X, preview is one of them, see the docs:

    https://developer.android.com/training/camerax/preview