2

I am developing an Android application for video camera, I am using the new CameraX Api for my application. This api is really easy to implement and everything working fine in my app, but the problem is in my front camera recording. If i open the front camera it shows me mirror image, this is fine and normal for every camera view but if i record video using my front facing camera then the recorded video flips horizontally. How can i prevent from that video fliping? I want record video just like Snapchat or 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);
}
Waqas Ali
  • 21
  • 7

1 Answers1

0

The flipped output is intended. In most devices, the confusing part is in preview session than recording.

In preview with the front camera, the mirror-like preview is for easy use of the front camera to users.

You need to read this SO answer for deep understanding. how-to-make-video-captured-by-front-camera-not-being-inverse-android

MJ Studio
  • 3,947
  • 1
  • 26
  • 37