【问题标题】:Camera application flips the recorded video相机应用程序翻转录制的视频
【发布时间】:2019-11-27 04:37:01
【问题描述】:

我正在为摄像机开发一个 Android 应用程序,我正在为我的应用程序使用新的 CameraX Api。这个 api 非常容易实现,并且在我的应用程序中一切正常,但问题在于我的前置摄像头录制。如果我打开前置摄像头,它会向我显示镜像,这对于每个摄像头视图来说都是正常且正常的,但如果我使用前置摄像头录制视频,则录制的视频会水平翻转。我怎样才能防止该视频翻转?我想像 Snapchat 或 Instagram 一样录制视频

private void startCamera() {

    CameraX.unbindAll();

    Rational aspectRatio = new Rational (textureView.getWidth(), textureView.getHeight());
    Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen


    PreviewConfig pConfig = new PreviewConfig.Builder().setLensFacing(lensFacing).setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
    final Preview preview  = new Preview(pConfig);

    preview.setOnPreviewOutputUpdateListener(
            new Preview.OnPreviewOutputUpdateListener() {
                //to update the surface texture we  have to destroy it first then re-add it
                @Override
                public void onUpdated(Preview.PreviewOutput output){
                    ViewGroup parent = (ViewGroup) textureView.getParent();
                    parent.removeView(textureView);
                    parent.addView(textureView, 0);

                    textureView.setSurfaceTexture(output.getSurfaceTexture());
                    updateTransform();
                }
            });

    ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setLensFacing(lensFacing).setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
            .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
    final ImageCapture imgCap = new ImageCapture(imageCaptureConfig);

    VideoCaptureConfig videoCaptureConfig = new VideoCaptureConfig.Builder().setLensFacing(lensFacing)
            .setAudioRecordSource(MediaRecorder.AudioSource.MIC)
            .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
    final VideoCapture videoCapture = new VideoCapture(videoCaptureConfig);

    findViewById(R.id.vidStart).setOnClickListener(new View.OnClickListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onClick(View v) {
            File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".mp4");
            imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
                @Override
                public void onImageSaved(@NonNull File file) {
                    String msg = "Pic captured at " + file.getAbsolutePath();
                    Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
                    String msg = "Pic capture failed : " + message;
                    Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
                    if(cause != null){
                        cause.printStackTrace();
                    }
                }
            });
            videoCapture.startRecording(file, new VideoCapture.OnVideoSavedListener() {
                @Override
                public void onVideoSaved(@NonNull File file) {
                    String msg = "Vid captured at " + file.getAbsolutePath();
                    Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(VideoCapture.UseCaseError useCaseError, String message, @Nullable Throwable cause) {

                }
            });

        }
    });

    findViewById(R.id.vidStop).setOnClickListener(new View.OnClickListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onClick(View v) {
            videoCapture.stopRecording();
        }
    });

    //bind to lifecycle:
    CameraX.bindToLifecycle(this, preview, videoCapture);
}

【问题讨论】:

  • 你能分享你的捕获代码吗?
  • 是的,我将编辑问题并添加代码
  • ??代码在哪里?
  • 我已经添加了相机代码,请帮助我
  • 了解如何在 AndroidX 中设置方向。我在 Android 2 Camera 中做过,而不是在 AndroidX Camera 中

标签: android video android-camerax


【解决方案1】:

预期的翻转输出。在大多数设备中,令人困惑的部分是预览会话而不是录制。

在前置摄像头预览中,镜面预览是为了方便用户使用前置摄像头。

您需要阅读此 SO 答案才能深入理解。 how-to-make-video-captured-by-front-camera-not-being-inverse-android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多