如何在视频录制时管理Android中的前置摄像头方向?

时间:2012-09-06 10:16:34

标签: android video android-orientation video-recording

我正在尝试以纵向模式开发摄像机应用程序。当我使用后置摄像头录制视频时,视频的方向正确,因为它可以进行正确的旋转。但是,当我尝试使用前置摄像头显示预览时,在某些设备中它可以正常工作,但在其他设备中,视频像HTC Desire一样旋转180º。那么,如何控制这个问题来将相机旋转到正确的位置?

更新:我发现这个link解释了我的问题,解决方案是重新编码视频,开发能够理解元数据的播放活动或将视频上传到服务器并旋转它。我最好的解决方案是在我的应用程序中开发一个新的播放视频。有人可以给我一些指导吗?是否需要使用ffmpeg库?我已经开发了基本播放视频,但是如何使用元数据定位视频?

private boolean initCamera() {
    try {
        camera  = getCameraInstance(selected_camera);

        Camera.Parameters camParams = camera.getParameters();
        camParams.set( "cam_mode", 1 );
 //            camParams.set("orientation", "portrait");

        checkCameraFlash(camParams);

        setAngleCameraRotation();
 //            camera.setDisplayOrientation(angle_rotation_camera);

        setAspectResolutionCamera(camParams);

        camera.setParameters(camParams);

        video_view.getHolder().setFixedSize(height_video, width_video);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(height_video, width_video);
        video_view.setLayoutParams(lp);

        camera.lock();

        surface_holder = video_view.getHolder();
        surface_holder.addCallback(this);
        surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        setPreviewCamera();

    } catch(Exception e) {
        Log.v("RecordVideo", "Could not initialize the Camera");
        return false;
    }
    return true;
}

private void initCameraRecorder() {

    if(media_recorder != null) return;

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".mp4";

    File outFile = new File(output_file_name);
    if(outFile.exists()) {
        outFile.delete();
    }

    try {
        camera.stopPreview();
        camera.unlock();
        media_recorder = new MediaRecorder();
        media_recorder.setCamera(camera);

        media_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        profile.videoBitRate = 885000;              //Medium quality
        profile.videoFrameHeight = height_video;
        profile.videoFrameWidth = width_video;
        media_recorder.setProfile(profile);

        media_recorder.setMaxDuration(21000);       // limit to 21 seconds
        media_recorder.setOrientationHint(setOrientationCameraRecorder());
        media_recorder.setPreviewDisplay(surface_holder.getSurface());
        media_recorder.setOutputFile(output_file_name);

        media_recorder.prepare();
        Log.v("RecordVideo", "MediaRecorder initialized");

    } catch(Exception e) {
        Log.v("RecordVideo", "MediaRecorder failed to initialize");
        e.printStackTrace();
    }
}

public static Camera getCameraInstance(int camera_id) {
    Camera c = null;
    try {
        c = Camera.open(camera_id); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

 private void setAspectResolutionCamera(Parameters camParams) {
    float aspect_ratio = 1f;
    int aspect_width = 2000, aspect_height = 2000;
    List<Size> supported_sizes_list = camParams.getSupportedPreviewSizes();
    for (int i = 0; i < supported_sizes_list.size(); i++) {
        Size size = supported_sizes_list.get(i);
        if (Math.abs(VIDEO_ASPECT_RATIO - (float) size.height / size.width) <= aspect_ratio ) {
            if (Math.abs(VIDEO_ASPECT_WIDTH - size.height) <= aspect_width) {
                if (Math.abs(VIDEO_ASPECT_HEIGHT - size.width) < aspect_height) {
                    width_video = size.width;
                    height_video = size.height;
                    aspect_ratio = Math.abs(VIDEO_ASPECT_RATIO - (float) size.height / size.width);
                    aspect_width = Math.abs(VIDEO_ASPECT_WIDTH - size.height);
                    aspect_height = Math.abs(VIDEO_ASPECT_HEIGHT - size.width);
                }
            }
        }
    }
}


/** Sets the angle camera rotation. It's the user sees */
private void setAngleCameraRotation() {
    Camera.CameraInfo info = new Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(selected_camera, info);       // back-facing camera
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }
    angle_rotation_camera = (info.orientation - degrees) % 360;  // back-facing camera rotation
}

0 个答案:

没有答案