【问题标题】:Open camera directly in the Activity without clicking on button and without intent?直接在 Activity 中打开相机而不点击按钮且无意图?
【发布时间】:2015-04-09 18:38:26
【问题描述】:

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

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

这是我的结果:

当我按下“捕获”按钮时,它会激活相机,但我想在此之前激活相机并在我点击像 snapchat 这样的按钮时进行录制。我想当我打开相机时我错过了某个地方,但我没有发现我的错误......

这是我的课:

    @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;
}

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

【问题讨论】:

    标签: android camera android-camera


    【解决方案1】:

    我发现了我的错误。我在 CameraPreview 类的 onSurfaceCreated() 方法中调用了 mCamera.setPreview()

    我需要在onSurfaceChanged() 中致电mCamera.setPreview()!现在可以了

    问题解决了!

    【讨论】:

      【解决方案2】:

      您需要在按下捕捉按钮之前设置来自相机的预览流。您的代码的 mPreview 参考中似乎已经有一个预览类。您需要确保它包含Camera#startPreview() call when the preview surface is created 才能显示来自摄像机的实时图像。要记住的另一件事是,预览类是一个 android View,需要可见才能正确设置其表面,这意味着您必须确保它被正确创建并且其 visibility 已设置可见。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多