I have a PreviewView with CameraX and an ImageView that acts as a frame on top of a full-screen PreviewView. When I try to crop the image I received with ImageCapture, I face the problem that the area occupied by the frame on the preview view does not coincide with the area on the original image, especially in terms of width. Before cropping, I scale the frame to the dimensions and change the position according to the dimensions of the original image using the method:
fun getBoundingBoxOnOutputPhoto(
imageView: ImageView,
previewView: PreviewView,
outputPhotoWidth: Int,
outputPhotoHeight: Int
): RectF {
// Assuming ImageView's layout params is relative to PreviewView
val imageViewWidthRatio = imageView.width.toFloat() / previewView.width
val imageViewHeightRatio = imageView.height.toFloat() / previewView.height
val imageViewXRatio = imageView.x.toFloat() / previewView.width
val imageViewYRatio = imageView.y.toFloat() / previewView.height
// Calculate the bounding box of ImageView on the output photo
val left = outputPhotoWidth * imageViewXRatio
val top = outputPhotoHeight * imageViewYRatio
val right = left + (outputPhotoWidth * imageViewWidthRatio)
val bottom = top + (outputPhotoHeight * imageViewHeightRatio)
return RectF(left, top, right, bottom)
}
Does anyone know how to solve this problem and make it possible to make the area occupied by the frame on the PreviewView correspond to the dimensions of the original image