2

I am trying to record a video using camera2 API but the Camera preview size and video are not the same as expected. The object appears centered in-camera preview but when the video is recorded, then the object is shifted to slightly. These are 2 images which explain better what I'm saying

enter image description here

enter image description here

And I'm using the following code inside the open camera fun

mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder::class.java),
                mCameraRecordingWidth!!,mCameraRecordingHeight!!)
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture::class.java),
                width, height, mVideoSize)

fun chooseVideoSize(choices: Array<Size>, configCameraWidth: Int, configCameraHeight: Int): Size {
        for (size in choices) {
            if (configCameraHeight == size.width && configCameraWidth == size.height) {
                return size
            }
        }
        for (size in choices) {
            if (size.width == size.height * 4 / 3 && (size.width <= 1080 || size.width <= configCameraHeight)) {
                return size
            }
        }
     
        return choices[choices.size - 1]
    }

 private fun chooseOptimalSize(choices: Array<Size>, width: Int, height: Int, aspectRatio: Size?): Size {
        val aspectRatioWidth = aspectRatio!!.width
        val aspectRatioHeight = aspectRatio.height
        val bigEnough: MutableList<Size> = ArrayList()
        for (option in choices) {
            if (option.height == option.width * aspectRatioHeight / aspectRatioWidth && option.width >= width && option.height >= height) {
                bigEnough.add(option)
            }
        }
        return if (bigEnough.size > 0) {
            Collections.min(bigEnough, CompareSizesByArea())
        } else {
            choices[0]
        }
    }

I'm following this link: https://github.com/googlearchive/android-Camera2Video and using the AutoFitTextureView

 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    val width = MeasureSpec.getSize(widthMeasureSpec)
    val height = MeasureSpec.getSize(heightMeasureSpec)
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height)
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height)
        } else {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth)
        }
    }
}

Any idea/solution is more important to me. Thanks a lot.

narender sharma
  • 561
  • 1
  • 4
  • 7

0 Answers0