自定义相机Android - 加载占位符

时间:2014-06-09 10:45:21

标签: android camera android-camera surfaceview mediarecorder

我已按照android developers的指南在Android中实施了自定义相机应用程序。我从另一个活动开始摄像机。从单击开始相机按钮到我的自定义相机UI显示(前一个活动UI仍然显示)会有很长的延迟(几秒钟)。自定义相机UI并不复杂,它主要只是相机预览。在相机加载时还有其他一些东西正在进行,但我已将其中的大部分移动到另一个线程。

我使用以下方法获取Camera对象:

/** A safe way to get an instance of the Camera object. */
public Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
        if (ConfigData.debugLog) Log.i(TAG, "* getCameraInstance Exception");
        e.printStackTrace();
    }
    return c; // returns null if camera is unavailable
}

我使用以下方法将其链接到MediaRecorder和SurfaceView:

mMediaRecorder = new MediaRecorder();

// Preview
SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
    } catch (Throwable t) {
        Log.e(TAG, "Exception in setPreviewDisplay()", t);
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    params = mCamera.getParameters();

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes!=null) {
        // set the focus mode
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);

        } else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);

        }
    }

    isZoomSupported = params.isZoomSupported();

    mCamera.setParameters(params);

    try {
        mCamera.unlock();
        startRecording();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/** Start recording video */
private void startRecording() throws IOException {
    try {
        mMediaRecorder = new MediaRecorder();

        mMediaRecorder.setCamera(mCamera);

        mMediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

        setMediaProfile();

        mMediaRecorder.setOutputFile(fileName);

        mMediaRecorder.prepare();

        mMediaRecorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
}

从我的实验,评论各个部分,滞后似乎来自我的“startRecording”功能。在评论该功能时,UI会立即加载,但在这种情况下预览不会加载。

我的主要优先事项是尽快显示相机用户界面,无论预览是否已加载。我能做些什么来让新活动加载更快?我可以将相机预览加载到单独的线程吗?什么阻止UI冻结上一个活动?

0 个答案:

没有答案