Skip to content Skip to sidebar Skip to footer

MetadataOutputRectConverted(fromLayerRect:) In Android

Currently I have a problem with cropping a rectangle from the camera preview. Basically I have setup the camera using fotoapparat where I have setup the scaleType of the preview to

Solution 1:

I figured it out with the following code.

private fun cropImage(bitmap: Bitmap, cameraFrame: View, cropRectFrame: View): Bitmap {
    val scaleFactor: Double; val widthOffset: Double; val heightOffset: Double

    if (cameraFrame.height * bitmap.width > cameraFrame.height * bitmap.width) {
        scaleFactor = (bitmap.width).toDouble() / (cameraFrame.width).toDouble()
        widthOffset = 0.0
        heightOffset = (bitmap.height - cameraFrame.height * scaleFactor) / 2
    } else {
        scaleFactor = (bitmap.height).toDouble() / (cameraFrame.height).toDouble()
        widthOffset = (bitmap.width - cameraFrame.width * scaleFactor) / 2
        heightOffset = 0.0
    }

    val newX = cropRectFrame.left * scaleFactor + widthOffset
    val newY = cropRectFrame.top * scaleFactor + heightOffset
    val width = cropRectFrame.width * scaleFactor
    val height = cropRectFrame.height * scaleFactor

    return Bitmap.createBitmap(bitmap, (newX).toInt(), (newY).toInt(), (width).toInt(), (height).toInt())

}

Post a Comment for "MetadataOutputRectConverted(fromLayerRect:) In Android"