我可以在没有预览的情况下使用Android Camera吗?

时间:2015-03-16 15:44:32

标签: android android-service

我在Android中创建了一个使用相机的应用程序,我可以测量用户面部与手机屏幕之间的距离。

问题描述:

现在我想让它运行后台,以便在我使用其他应用程序时可以使用该功能。这意味着我应该在服务中打开相机而不预览,并在服务中处理它

我做了什么:

我在这里提到了一些问题

How to record video from background of application : Android

How to use Android Camera in Background?

Taking picture from camera without preview

API等级16

我的服务文件

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class RecorderService extends Service {
private static final String TAG = "RecorderService";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private static Camera mServiceCamera;
private boolean mRecordingStatus;
private MediaRecorder mMediaRecorder;
File path = android.os.Environment.getExternalStorageDirectory();


@Override
public void onCreate() {
    Log.i(TAG,"onCreate");
    mRecordingStatus = false;
    //mServiceCamera = CameraRecorder.mCamera;
    mServiceCamera = Camera.open(1);
    mSurfaceView = MainActivity.mSurfaceView;
    mSurfaceHolder = MainActivity.mSurfaceHolder;

    super.onCreate();
    if (mRecordingStatus == false)
        startRecording();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
    Log.i(TAG,"onDestroy");

    stopRecording();
    mRecordingStatus = false;

    super.onDestroy();
}

public boolean startRecording(){
    Log.i(TAG,"startRecording");

    try {
        Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();

        //mServiceCamera = Camera.open();
        Camera.Parameters params = mServiceCamera.getParameters();
        mServiceCamera.setParameters(params);
        Camera.Parameters p = mServiceCamera.getParameters();

        final List<Size> listSize = p.getSupportedPreviewSizes();
        Size mPreviewSize = listSize.get(2);
        Log.v(TAG, "use: width = " + mPreviewSize.width
                + " height = " + mPreviewSize.height);
        p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        mServiceCamera.setParameters(p);

        try {
            mServiceCamera.setPreviewDisplay(mSurfaceHolder);
            mServiceCamera.startPreview();
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }

        mServiceCamera.unlock();

        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        mMediaRecorder.setOutputFile(path+"/outputVideo.mp4");
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

        mMediaRecorder.prepare();
        mMediaRecorder.start();

        mRecordingStatus = true;

        return true;
    } catch (IllegalStateException e) {
        Log.d(TAG, e.getMessage());
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        e.printStackTrace();
        return false;
    }
}

public void stopRecording() {
    Log.i(TAG,"stopRecording");

    Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
    try {
        mServiceCamera.reconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try{
        mMediaRecorder.stop();
    }catch (Exception ignored)
    {

    }
    mMediaRecorder.reset();
    mMediaRecorder.release();
    mServiceCamera.stopPreview();
    mServiceCamera.release();
    mServiceCamera = null;
}
}

1 个答案:

答案 0 :(得分:7)

您可以参考此Question,其中讨论如何在服务中进行视频记录。捕获图像的步骤与它相同。

为了达到您的要求,您可能需要:

  1. 在您的服务中获取相机实例。请查看此Official Guideline
  2. 设置相机参数。使用Camera.getParameters()Camera.setParameters()Camera.Parameters.setPictureSize(int with, int height)Camera.Parameters.setPictureFormat(int format)等API来执行此操作。
  3. 准备用于存储图像的文件。您需要实施Camera.PictureCallback才能这样做。
  4. 在拍摄照片之前致电Camera.startPreview()。如果在拍照之前没有调用此功能,则会出现异常。 (注意:您不需要先Camera.setPreviewdisplay(SurfaceHolder display)。)
  5. 在您的服务中致电camera.takePicture()。然后,您可以将捕获的图像存储在您指定的文件中。
  6. 在工作之后,不要忘记维护生命周期中的资源以正确获取/释放相机。

    这是我的sample code on Github,评论中也提到了这一点。