构建Android自己的相机:定义正确的方向

时间:2012-12-11 21:38:30

标签: android video camera android-camera video-capture

我正在实施相机活动并遇到定位问题。

虽然我成功定义了预览的方向,但最终影片的方向错误始终为90度。

我已经尝试了所有的建议,我在这里找到的建议,它只定义了正确的预览,而不是录制的电影。

我正在使用下一种方法开始录制:

private void startRecording() {
        mRecordingButton.setBackgroundResource(R.drawable.rec_stop);
        mSwitchCameraButton.setVisibility(View.INVISIBLE);
        mSendButton.setVisibility(View.INVISIBLE);
        mChronometer.setVisibility(View.VISIBLE);

        try {
            mMediaRecorder = new MediaRecorder();

            mCamera.unlock();
            mMediaRecorder.setCamera(mCamera);

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

            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setAudioEncoder(0x00000003);
            mMediaRecorder.setVideoEncoder(VideoEncoder.H264);

            mMediaRecorder.setOutputFile(getFile().getPath());
            mMediaRecorder.setVideoSize(480, 720);
            mMediaRecorder.setVideoEncodingBitRate(BPS_VIDEO);
            mMediaRecorder.setVideoFrameRate(24);
            mMediaRecorder.setAudioChannels(2);
            mMediaRecorder.setAudioEncodingBitRate(BPS_AUDIO);
            // mMediaRecorder.setOrientationHint(90);

            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

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

            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.start();

            mIsRecording = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是为了定义方向:

public void surfaceCreated(SurfaceHolder arg0) {
        mCamera = Camera.open(0);
        Camera.Parameters params = mCamera.getParameters();
        List<Size> sizes = params.getSupportedPictureSizes();
        Size mSize = sizes.get(0);
        params.setPictureSize(mSize.width, mSize.height);

        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
            Display display =
                    ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

            if (display.getRotation() == Surface.ROTATION_0) {
                setDisplayOrientation(mCamera, 90);
            } else if (display.getRotation() == Surface.ROTATION_270) {
                setDisplayOrientation(mCamera, 180);
            }
        } else {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                params.set("orientation", "portrait");
                params.set("rotation", 90);
            }
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                params.set("orientation", "landscape");
                params.set("rotation", 90);
            }
        }

        mCamera.setParameters(params);

//      setCameraDisplayOrientation(this, 0, mCamera);

        previewCamera();

        if (mCamera == null) {
            Toast.makeText(
                    this.getApplicationContext(),
                    "Camera is not available!",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    public void previewCamera() {
        try {
            mCamera.setPreviewDisplay(mSurfaceHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d("VideoCameraActivity", "Cannot start preview", e);
        }
    }

P.S。这一行解决了这个问题:

mMediaRecorder.setOrientationHint(90);

但会导致某些设备崩溃(galaxy s1,s3)。

0 个答案:

没有答案