0

Basically i have an Activity that contains a CameraPreview and i put a RelativeLayout with shape drawable as its background to show rectangle on the middle of the screen.

enter image description here

I wanted to crop the boxed area and i managed to do it by cropping the bitmap like so

        val height = dpToPx(300)
        val width = dpToPx(187)

        var y = srcBmp.height / 2 - (height / 2)
        var x = srcBmp.width / 2 - (width / 2)

        dstBmp = Bitmap.createBitmap(
            srcBmp,
            x,
            y,
            width,
            height
      )

It worked in most devices, but i don't know why some devices are not respecting the resolution of the DP that i defined, some pictures are bigger than the BOX in some devices.

this is how i convert dp to px

fun dpToPx(dp: Int): Int {
        val density = this.resources
            .displayMetrics
            .density
        return Math.round(dp.toFloat() * density)
    }

this is my activity layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <com.camerakit.CameraKitView
            android:layout_centerInParent="true"
            android:id="@+id/camera_view_box1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:keepScreenOn="true"
            app:camera_facing="back"
            app:camera_focus="continuous"
            app:camera_permissions="camera"/>

    <RelativeLayout
            android:layout_centerInParent="true"
            android:background="@drawable/transparent_bg"
            android:layout_width="300dp"
            android:layout_height="187dp"/>

    <RelativeLayout
            android:focusable="true"
            android:clickable="true"
            android:id="@+id/take_picture_box"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:layout_alignParentBottom="true"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:background="@drawable/circle_bg"/>

</RelativeLayout>

What did i do wrong, and how can i fix it?

0 Answers0