0

我已经在三星平板电脑上测试了我的代码并注意,但相同的代码在 nexus 上不起作用。

我知道nexus只有前置摄像头,并且摄像头正在打开,但是单击开始录制按钮它会崩溃....我通过setProfile上的错误检查了它。

我的代码:

public boolean prepareVideoRecorder() {
      Toast.makeText(context, "Please wait..", Toast.LENGTH_SHORT).show();
//   mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();
    // Step 1: Unlock and set camera to MediaRecorder
     mCamera.stopPreview();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
     int layout = getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK;
        if (layout == Configuration.SCREENLAYOUT_SIZE_XLARGE) {

    switch (((WindowManager) getSystemService(WINDOW_SERVICE))
            .getDefaultDisplay().getOrientation()) {
    case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
    //  Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
        int degrees = 0;
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
    //      Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 1111", Toast.LENGTH_SHORT).show();
        } else { // back-facing
            result = (info.orientation - degrees + 360) % 360;
    //      Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 222-- "+ result, Toast.LENGTH_SHORT).show();
        }

        mMediaRecorder.setOrientationHint(result);

        break;
    }
    case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
    //  Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
        int degrees = 270;
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        int result;
        if (CurrentValues.cur_cam_face == 1) {
        //  Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 111", Toast.LENGTH_SHORT).show();
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
            // System.out.println("FRONT CAMERA )))))))");

        } else { // back-facing
        //  Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 222", Toast.LENGTH_SHORT).show();
            result = degrees;// (info.orientation - degrees + 360) % 360;
            // System.out.println("BACK CAMERA )))))))");
        }

        mMediaRecorder.setOrientationHint(result);
        break;
    }
    case Configuration.ORIENTATION_LANDSCAPE: {
    //  Toast.makeText(context, "ORIENTATION_LANDSCAPE", Toast.LENGTH_LONG).show();
        int degrees = 180;
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        //  Toast.makeText(context, "ORIENTATION_LANDSCAPE 111", Toast.LENGTH_SHORT).show();
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
        } else { // back-facing
        //  Toast.makeText(context, "ORIENTATION_LANDSCAPE 22", Toast.LENGTH_SHORT).show();
            result = (info.orientation - degrees + 360) % 360;
        }

        mMediaRecorder.setOrientationHint(result);
        break;
    }
    default: {
        //Toast.makeText(context, "DEFAULT 11", Toast.LENGTH_SHORT).show();
        int degrees = 90;
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        int result;
        if (CurrentValues.cur_cam_face == 1) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
        } else { // back-facing
            result = degrees;// (info.orientation - degrees + 360) % 360;
        }

        mMediaRecorder.setOrientationHint(result);
    }

        }
        }else if (layout == Configuration.SCREENLAYOUT_SIZE_LARGE) {
            switch (((WindowManager) getSystemService(WINDOW_SERVICE))
                    .getDefaultDisplay().getOrientation()) {

            case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
                Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
                int degrees = 0;
                android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                int result;
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    result = (info.orientation + degrees) % 360;
                    result = (360 - result) % 360; // compensate the mirror
                Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
                } else { // back-facing
                    result = (info.orientation - degrees + 360) % 360;
                    Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
                }
                mMediaRecorder.setOrientationHint(result);
                break;
            }
            case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {


                Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
                int degrees = 90;
                android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                int result;
                if (CurrentValues.cur_cam_face == 1) {
                    result = (info.orientation + degrees) % 360;
                    result = (360 - result) % 360; // compensate the mirror
                } else { // back-facing
                    result = degrees;// (info.orientation - degrees + 360) % 360;
                }
                mMediaRecorder.setOrientationHint(result);

                break;
        }
            }

    }else
        {
            switch (((WindowManager) getSystemService(WINDOW_SERVICE))
                    .getDefaultDisplay().getOrientation()) {
            case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
                Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
                int degrees = 0;
                android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                int result;
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    result = (info.orientation + degrees) % 360;
                    result = (360 - result) % 360; // compensate the mirror
                Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
                } else { // back-facing
                    result = (info.orientation - degrees + 360) % 360;
                    Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
                }
                mMediaRecorder.setOrientationHint(result);
                break;
            }
            case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {


                Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
                int degrees = 90;
                android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                int result;
                if (CurrentValues.cur_cam_face == 1) {
                    result = (info.orientation + degrees) % 360;
                    result = (360 - result) % 360; // compensate the mirror
                } else { // back-facing
                    result = degrees;// (info.orientation - degrees + 360) % 360;
                }
                mMediaRecorder.setOrientationHint(result);

                break;
        }
            }
        }

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CamcorderProfile camcorderProfile_HQ;
    if (CurrentValues.cur_cam_face == 1)
        camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
    else
        camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mMediaRecorder.setProfile(camcorderProfile_HQ);
    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)
            .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(TAG,
                "IllegalStateException preparing MediaRecorder: "
                        + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}
4

2 回答 2

1

假设您正在尝试 latest Nexus 7,我觉得问题可能是QUALITY_HIGH您的设备中未定义的问题。我在这里media_profiles.xml检查了AOSP树,发现只有、和它们的延时版本已被定义。您可以通过查看设备上的来确认这一点。CIFQCIF480pmedia_profiles.xml/etc/media_profiles.xml

您可以调整您的代码以使用CamcorderProfile.QUALITY_480P而不是CamcorderProfile.QUALITY_HIGH.

于 2013-03-14T17:16:11.587 回答
0

我发现您的代码存在一些问题,其中任何一个都可能导致您的错误。

首先,仅仅创建一个 CameraInfo 对象是不够的,你必须调用:

Camera.getCameraInfo(cameraId, info);

如果没有这个调用,你总是会得到 0 来表示朝向和方向(默认整数值)。

其次,您还应该使用接受 cameraId 的方法获取配置文件:

CamcorderProfile.get(cameraId, quality);

我假设您正在正确使用 Camera.open(int cameraId) 方法,否则您可能无法让任何前置摄像头工作。

此外,这只是惯例,但我建议您将 switch 语句中的方向代码分解为函数以提高可读性。理想情况下,开关盒应该只有一行,但几行仍然可以。

于 2013-05-29T20:46:56.727 回答