通过相机录制视频,无需镜像,就像快照聊天一样

时间:2016-08-13 10:28:11

标签: android android-camera android-mediaplayer android-mediarecorder video-recording

我正在使用社交移动应用。它可以选择录制个人资料视频。我的刻录机的问题是它反映了实际的视频。我知道这是默认行为,但希望它像sanpchat一样工作。

预览

Video when preview

录制和播放后

After recording and playback

这是我的代码

// return camera instance when activity open first time
private Camera getCameraInstance() {
    // TODO Auto-generated method stub
    releaseCamera();
    releaseMediaRecorder();
    Camera c = null;
    try {
        cameraId = findFrontFacingCamera();
        if (cameraId < 0) {
            cameraId = findBackFacingCamera();
        }
        c = Camera.open(cameraId);

        // setCameraDisplayOrientation(this,cameraId,c);
        //setCameraDisplayOrientation(this, cameraId, c);
        c.setDisplayOrientation(90);
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}
// return __cameraPreview Id 1 to open front camera
private int findFrontFacingCamera() { 
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            setOrientationHint = 270 ;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}
// prepare and start recording
private boolean prepareMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    try {
        myCamera.unlock();
    } catch (Exception e) {
        e.printStackTrace();
    }
    mediaRecorder.setCamera(myCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    if (cameraPreview.getVideoSize() != null) {
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setOutputFile(saveUrl);

        mediaRecorder.setVideoEncodingBitRate(10000000);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setVideoSize(cameraPreview.getVideoSize().width, cameraPreview.getVideoSize().height);

        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    } else {
        CamcorderProfile cp = CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH);
        mediaRecorder.setProfile(cp);
    }
    Log.e(TAG, "Video profile CamcorderProfile.QUALITY_HIGH: " + "cp.quality:" + cp.quality
            + ", cp.videoFrameWidth:" + cp.videoFrameWidth
            + ", cp.videoFrameHeight:" + cp.videoFrameHeight);

    mediaRecorder.setMaxDuration(MAX_VIDEO_LENGTH);
    mediaRecorder.setOrientationHint(setOrientationHint);
    try {
        mediaRecorder.prepare();

    } catch (IllegalStateException | IOException e) {
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    }
    mediaRecorder.start();
    return true;
}

我希望它应该与播放中的预览相同。 Sanpchat已经做了同样的事情,提前谢谢。

3 个答案:

答案 0 :(得分:3)

您已将前置摄像头的方向设置为270,

setOrientationHint = 270 ;

相反,我建议将方向设置为180度,如下所示

setOrientationHint = (info.orientation - 180 + 360) % 360;

这也会改变整个视频文件。

答案 1 :(得分:2)

您可以使用TextureView,文档here并在X轴上应用-1比例的变换,函数here

答案 2 :(得分:1)

对我来说没什么用,最后我需要在我提交视频的api那边做。上传视频后,我使用ffmpeg垂直翻转它并在元数据中设置角度。这是这个命令。

ffmpeg -i VIDEO_PATH -vf "vflip" -metadata:s:v rotate=180 DEST_PATH