android camera2 api - How can i add a company logo to the recorded video file

940 views Asked by At

Hy everyone. I am trying to add a small logo in the corner to the video i've recorded . I've tried to add the imageView directly to the recording surface but it is not the solution.

I guess i'll have to create another surface and merge them together but i couldn't find any tutorial or code sample for such a thing.

I've found the option to add a foreground drawable but it doesn't show the logo on the preview surface. This is the code:

   private void startRecording(){
    try {
        setupMediaRecorder();
        mTextureView.setForeground(getDrawable(R.drawable.toolbarlogo));
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight());

        Surface previewSurfice = new Surface(surfaceTexture);
        Surface recordSurface = mMediaRecorder.getSurface();

        mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        mPreviewCaptureRequestBuilder.addTarget(previewSurfice);
        mPreviewCaptureRequestBuilder.addTarget(recordSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurfice, recordSurface), new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                try {
                    session.setRepeatingRequest(
                            mPreviewCaptureRequestBuilder.build(),null,null
                    );
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {

            }
        },null);



    } catch (Exception e) {
        e.printStackTrace();
    }
}

please help. Thanks

1

There are 1 answers

0
Mick On BEST ANSWER

Adding a separate surface will just show the logo while you are viewing it in your app - if you want to logo to be added to the file so it can be viewed if you share or load the video, then you need to add it to the video itself.

Assuming you capture the video to your local device, then one way you can do this is to use ffmpeg to add an image to the video - see this answer which includes notes on how to position the image:

There are several ways to include ffmpoeg in an Android project, but maybe the easiest is to use a well supported ffmpeg wrapper project like this one:

The wrappers basically wrap an interface around there command line ffmpeg tool, which has the advantage that you can use the same syntax - e.g. the syntax in the answer noted above, and leverage the support and Q&A on the web around it.

The disadvantage is that the command line tool was not originally designed to be used this way, but if you use a well supported wrapper you will likely find a lot of the problems have been ironed out.

One thing to note - video processing has quite high CPU and hence battery requirements on a mobile device. If you are going to be uploading the video to a server to share it, it may make more sense to add the image here where you have more CPU horsepower.