直接在 Activity 中打开相机而不点击按钮并且无意?

Open camera directly in the Activity without clicking on button and without intent?

我试图在不点击我的按钮的情况下打开我的相机,但它不起作用...

我按照此处的说明进行操作:http://developer.android.com/guide/topics/media/camera.html#custom-camera

这是我的结果:

当我按下按钮 "Capture" 时,它会激活摄像头,但我想在此之前激活摄像头,并且只在我单击按钮(如 snapchat)时进行录制。我想我在打开相机时错过了某个地方,但我没有发现我的错误...

这是我的 class :

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preview = (FrameLayout) findViewById(R.id.camera_preview);


    if (checkCameraHardware(this)) {
        mCamera = getCameraInstance();
        mPreview = new CameraPreview(this, mCamera);
        mCamera.setDisplayOrientation(90);

        initButton();

        preview.addView(mPreview);
        preview.removeAllViews();
        preview.addView(mPreview);
        preview.addView(captureButton);
    }
}

之后,我有了启动按钮的方法:

public void initButton () {
    captureButton = (Button) findViewById(R.id.button_capture);

    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isRecording) {
                // stop recording and release camera
                mMediaRecorder.stop();
                releaseMediaRecorder();
                mCamera.lock();

                // inform the user that recording has stopped
                captureButton.setText("Capture");
                isRecording = false;
            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                    mMediaRecorder.start();

                    captureButton.setText("Stop");
                    isRecording = true;
                } else {
                    releaseMediaRecorder();
                    Toast.makeText(MainActivity.this, "Camera doesn't work", Toast.LENGTH_LONG).show();
                }
            }
        }
    });

}

这是我获取相机实例的方法:

public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {
        Log.i("CAMERA INFO : >", "Camera doesn't exist");
    }
    return c;
}

这是准备记录的方法:

private boolean prepareVideoRecorder() {
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

    mMediaRecorder.setProfile(camcorderProfile);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(CAPTURE_VIDEO_FILE).toString());

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("PREPARE MEDIARECORDER : >", ": > > IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d("PREPARE MEDIARECORDER : >", " : > > IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

然后我尝试通过实例化在单击之前打开相机,但我不知道如何...如果有人有想法,将对我有用。

您需要在按下拍摄按钮之前设置来自相机的预览流。您的代码的 mPreview 参考中似乎已经有了预览 class。您需要确保它包含一个 Camera#startPreview() call when the preview surface is created in order to display live images from the camera. Another thing to keep in mind is that the preview class, which is an android View, needs to be visible in order to have its surface properly set up, meaning you have to make sure it is being properly created and its visibility 设置为 VISIBLE。

我发现了我的错误。我在 CameraPreview class.

onSurfaceCreated() 方法中调用了 mCamera.setPreview()

我需要在 onSurfaceChanged() 中调用 mCamera.setPreview()!现在可以使用了

问题已解决!