Android Open Source - rsmonitor-heartrate Capture Camera






From Project

Back to project page rsmonitor-heartrate.

License

The source code is released under:

GNU General Public License

If you think the Android project rsmonitor-heartrate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.renaultsport.heartrate.utils;
/*  www.j  a v a 2 s . c  om*/
import java.io.File;
import java.io.IOException;
import java.util.Date;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;




public class CaptureCamera implements SurfaceHolder.Callback, Camera.PreviewCallback
{
  private static CaptureCamera m_Instance = null;

  private boolean      m_bIsPreview    = false;
  private boolean      m_bIsRecording    = false;
  private String      m_szPathVideo    = "";

  private Handler      m_Worker      = null;
  private MediaRecorder  m_MediaRecorder    = null;
  private Camera      m_Camera      = null;
  private SurfaceView    m_SurfaceView    = null;
  private SurfaceHolder  m_SurfaceHolder    = null;




  public static CaptureCamera getInstance (Handler handler, SurfaceView view, int orientation)
  {
    synchronized (CaptureCamera.class)
    {
      if (m_Instance == null) { m_Instance = new CaptureCamera (); }

      m_Instance.setWorker    (handler);
      m_Instance.setSurfaceView  (view);

      return m_Instance;
    }
  }

  private CaptureCamera () { }




  @Override
  public void onPreviewFrame(byte[] data, Camera camera) { }

  @Override
  public void surfaceDestroyed (SurfaceHolder surfaceHolder)
  {
    if (m_Camera != null)
    {
      m_Camera.stopPreview ();
      m_Camera.setPreviewCallback (null);

      m_Camera.release ();
      m_Camera = null;
    }

    m_bIsPreview = false;

    if (m_Worker != null) { m_Worker.sendEmptyMessage (Constants.PREVIEW_DESTROYED); }
  }

  @Override
  public void surfaceChanged (SurfaceHolder surfaceHolder, int format, int width, int height)
  {
    Camera.Parameters parameters  = null;


    parameters = m_Camera.getParameters ();
    m_Camera.setParameters (parameters);

    if (m_bIsPreview)
    {
      m_Camera.stopPreview ();
      m_bIsPreview = false;
    }

    if (m_Camera != null)
    {
      try { m_Camera.setPreviewDisplay (surfaceHolder); }
      catch (IOException exception) { }

      m_bIsPreview = true;
    }
  }

  @Override
  public void surfaceCreated (SurfaceHolder surfaceHolder)
  {
    if (m_Camera == null) { m_Camera = Camera.open (); }

    try
    {
      m_Camera.setPreviewDisplay  (surfaceHolder);
      m_Camera.setPreviewCallback  (this);
    }
    catch (IOException exception)
    {
      m_Camera.release ();
      m_Camera = null;
    }
  }




  public void cameraOn ()
  {
    if (m_Camera == null)
    {
      m_Camera = Camera.open ();
      try
      {
        m_Camera.setPreviewDisplay  (m_SurfaceHolder);
        m_Camera.setPreviewCallback  (this);

        m_bIsPreview  = true;
        m_bIsRecording  = false;
      }
      catch (IOException exception)
      {
        m_Camera.release();
        m_Camera = null;
      }
    }

    m_SurfaceView.setVisibility (View.VISIBLE);

    if (m_Worker != null) { m_Worker.sendEmptyMessage (Constants.CAMERA_ON); }
  }

  public void cameraOff ()
  {
    if (m_Camera != null)
    {
      m_Camera.stopPreview ();
      m_Camera.setPreviewCallback (null);

      m_Camera.release ();
      m_Camera = null;
    }

    m_bIsPreview  = false;
    m_bIsRecording  = false;

    m_SurfaceView.setVisibility (View.GONE);

    if (m_Worker != null) { m_Worker.sendEmptyMessage (Constants.CAMERA_OFF); }
  }

  public void startRecording () throws IllegalStateException, IOException
  {
    Parameters  parameters  = null;
    Size    previewSize  = null;
  

    m_szPathVideo  = getOutputMediaFile    ();
    m_MediaRecorder  = new MediaRecorder      ();
    parameters    = m_Camera.getParameters  ();
  
    previewSize    = parameters.getPreviewSize ();
  
    m_Camera.unlock ();
  
    m_MediaRecorder.setCamera      (m_Camera);
    m_MediaRecorder.setVideoSource    (MediaRecorder.VideoSource.DEFAULT);
    m_MediaRecorder.setOutputFormat    (MediaRecorder.OutputFormat.DEFAULT);
    m_MediaRecorder.setVideoEncoder    (MediaRecorder.VideoEncoder.DEFAULT);
    m_MediaRecorder.setVideoSize    (previewSize.width, previewSize.height);
    m_MediaRecorder.setPreviewDisplay  (m_SurfaceHolder.getSurface ());
    m_MediaRecorder.setOutputFile    (m_szPathVideo);
    m_MediaRecorder.prepare        ();
    m_MediaRecorder.start        ();

    m_bIsRecording  = true;

    if (m_Worker != null) { m_Worker.sendEmptyMessage (Constants.CAMERA_RECORD_START); }
  }

  public void stopRecording ()
  {
    m_MediaRecorder.stop  ();
    m_MediaRecorder.release  ();

    m_Camera.lock ();

    m_bIsRecording  = false;

    if (m_Worker != null) { m_Worker.sendEmptyMessage (Constants.CAMERA_RECORD_STOP); }
  }




  private String getOutputMediaFile ()
  {
    File directory  = null;
    File videoFile  = null;


    directory = new File (Environment.getExternalStorageDirectory ().getAbsolutePath () + File.separator + Constants.SDCARD_PATH);
    if (! directory.exists ()) { directory.mkdirs (); }

    videoFile = new File (directory.getPath () + File.separator + Constants.SDCARD_VIDEO_FILE + new Date ().getTime () + Constants.SDCARD_VIDEO_EXTENSION);

    return videoFile.getAbsolutePath ();
  }




  public void setSurfaceView (SurfaceView view)
  {
    m_SurfaceView = view;

    m_SurfaceHolder = view.getHolder ();
    m_SurfaceHolder.addCallback (this);
    m_SurfaceHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }




  // Generated getters / setters

  public boolean isPreview() {
    return m_bIsPreview;
  }

  public void setPreview(boolean value) {
    this.m_bIsPreview = value;
  }

  public boolean isRecording() {
    return m_bIsRecording;
  }

  public void setRecording(boolean value) {
    this.m_bIsRecording = value;
  }

  public String getPathVideo() {
    return m_szPathVideo;
  }

  public void setPathVideo(String value) {
    this.m_szPathVideo = value;
  }

  public Handler getWorker() {
    return m_Worker;
  }

  public void setWorker(Handler m_Worker) {
    this.m_Worker = m_Worker;
  }
}




Java Source Code List

com.facebook.android.AsyncFacebookRunner.java
com.facebook.android.BaseDialogListener.java
com.facebook.android.BaseRequestListener.java
com.facebook.android.DialogError.java
com.facebook.android.FQLQuery.java
com.facebook.android.FacebookError.java
com.facebook.android.FacebookFunctions.java
com.facebook.android.Facebook.java
com.facebook.android.FbDialog.java
com.facebook.android.FieldsConnectionsDialog.java
com.facebook.android.FriendsGetProfilePics.java
com.facebook.android.FriendsList.java
com.facebook.android.GraphExplorer.java
com.facebook.android.IntentUriHandler.java
com.facebook.android.LoginButton.java
com.facebook.android.PermissionsDialog.java
com.facebook.android.Places.java
com.facebook.android.SessionEvents.java
com.facebook.android.SessionStore.java
com.facebook.android.TokenRefreshDialog.java
com.facebook.android.UpdateStatusResultDialog.java
com.facebook.android.UploadPhotoResultDialog.java
com.facebook.android.Util.java
com.facebook.android.Utility.java
com.googlecode.android.widgets.DateSlider.AlternativeDateSlider.java
com.googlecode.android.widgets.DateSlider.CustomDateSlider.java
com.googlecode.android.widgets.DateSlider.DateSlider.java
com.googlecode.android.widgets.DateSlider.DateTimeSlider.java
com.googlecode.android.widgets.DateSlider.DefaultDateSlider.java
com.googlecode.android.widgets.DateSlider.MonthYearDateSlider.java
com.googlecode.android.widgets.DateSlider.ScrollLayout.java
com.googlecode.android.widgets.DateSlider.TimeSlider.java
com.googlecode.android.widgets.DateSlider.TimeView.java
com.renaultsport.heartrate.ActivityMain.java
com.renaultsport.heartrate.ActivityRecord.java
com.renaultsport.heartrate.ActivitySplash.java
com.renaultsport.heartrate.ActivityUpload.java
com.renaultsport.heartrate.ActivityVideoReader.java
com.renaultsport.heartrate.ActivityWarning.java
com.renaultsport.heartrate.MainApplication.java
com.renaultsport.heartrate.utils.CaptureCamera.java
com.renaultsport.heartrate.utils.ClientThread.java
com.renaultsport.heartrate.utils.Constants.java
com.renaultsport.heartrate.utils.RunEncoder.java
team.stride.tabs.TabGroupActivity.java