I am developing an Android app using Camera1 API. I want to dynamically adjust the contrast of the preview image captured by the camera. How should I do this? I learned about the PreviewCallback callback method. Should I make adjustments in this callback instead?
override fun onPreviewFrame(data: ByteArray?, camera: Camera?) {
if (data == null || camera == null) return
// Get the format and size of the camera preview data
val parameters = camera.parameters
val previewSize = parameters.previewSize
// Convert the preview data to YuvImage
val yuvMat = Mat(previewSize.height + previewSize.height / 2, previewSize.width, CvType.CV_8UC1)
yuvMat.put(0, 0, data)
// Convert YuvImage to RGBA Mat
val rgbaMat = Mat(previewSize, CvType.CV_8UC4)
Imgproc.cvtColor(yuvMat, rgbaMat, Imgproc.COLOR_YUV2RGBA_NV21, 4)
// Convert RGBA Mat to grayscale
val grayscaleMat = Mat()
Imgproc.cvtColor(rgbaMat, grayscaleMat, Imgproc.COLOR_RGBA2GRAY)
// Adjust contrast
val alpha = 1.5 // Adjust the contrast multiplier as needed
val beta = 0.0
grayscaleMat.convertTo(grayscaleMat, CvType.CV_8UC1, alpha, beta)
// Convert grayscale back to RGBA
Imgproc.cvtColor(grayscaleMat, rgbaMat, Imgproc.COLOR_GRAY2RGBA)
// Display the result on the SurfaceView or perform other operations
// ...
// Release resources
yuvMat.release()
grayscaleMat.release()
rgbaMat.release()
// Manually add the buffer to ensure the camera continues to use it
camera.addCallbackBuffer(data)
}