How to measure bitmap with android “dp”












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?










share|improve this question
























  • what is the size of the BOX?
    – Vladyslav Matviienko
    Nov 23 at 9:06










  • @VladyslavMatviienko 300 x 187 in dp
    – Achmad Naufal Syafiq
    Nov 23 at 9:07










  • show how you define it in layout
    – Vladyslav Matviienko
    Nov 23 at 9:08












  • @VladyslavMatviienko i updated my question, i added the code inside my activity's layout
    – Achmad Naufal Syafiq
    Nov 23 at 9:16










  • looks correct for me unfortunately...
    – Vladyslav Matviienko
    Nov 23 at 9:23
















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?










share|improve this question
























  • what is the size of the BOX?
    – Vladyslav Matviienko
    Nov 23 at 9:06










  • @VladyslavMatviienko 300 x 187 in dp
    – Achmad Naufal Syafiq
    Nov 23 at 9:07










  • show how you define it in layout
    – Vladyslav Matviienko
    Nov 23 at 9:08












  • @VladyslavMatviienko i updated my question, i added the code inside my activity's layout
    – Achmad Naufal Syafiq
    Nov 23 at 9:16










  • looks correct for me unfortunately...
    – Vladyslav Matviienko
    Nov 23 at 9:23














0












0








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?










share|improve this question















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?







android android-camera android-bitmap bitmapimage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 9:40

























asked Nov 23 at 8:59









Achmad Naufal Syafiq

1651417




1651417












  • what is the size of the BOX?
    – Vladyslav Matviienko
    Nov 23 at 9:06










  • @VladyslavMatviienko 300 x 187 in dp
    – Achmad Naufal Syafiq
    Nov 23 at 9:07










  • show how you define it in layout
    – Vladyslav Matviienko
    Nov 23 at 9:08












  • @VladyslavMatviienko i updated my question, i added the code inside my activity's layout
    – Achmad Naufal Syafiq
    Nov 23 at 9:16










  • looks correct for me unfortunately...
    – Vladyslav Matviienko
    Nov 23 at 9:23


















  • what is the size of the BOX?
    – Vladyslav Matviienko
    Nov 23 at 9:06










  • @VladyslavMatviienko 300 x 187 in dp
    – Achmad Naufal Syafiq
    Nov 23 at 9:07










  • show how you define it in layout
    – Vladyslav Matviienko
    Nov 23 at 9:08












  • @VladyslavMatviienko i updated my question, i added the code inside my activity's layout
    – Achmad Naufal Syafiq
    Nov 23 at 9:16










  • looks correct for me unfortunately...
    – Vladyslav Matviienko
    Nov 23 at 9:23
















what is the size of the BOX?
– Vladyslav Matviienko
Nov 23 at 9:06




what is the size of the BOX?
– Vladyslav Matviienko
Nov 23 at 9:06












@VladyslavMatviienko 300 x 187 in dp
– Achmad Naufal Syafiq
Nov 23 at 9:07




@VladyslavMatviienko 300 x 187 in dp
– Achmad Naufal Syafiq
Nov 23 at 9:07












show how you define it in layout
– Vladyslav Matviienko
Nov 23 at 9:08






show how you define it in layout
– Vladyslav Matviienko
Nov 23 at 9:08














@VladyslavMatviienko i updated my question, i added the code inside my activity's layout
– Achmad Naufal Syafiq
Nov 23 at 9:16




@VladyslavMatviienko i updated my question, i added the code inside my activity's layout
– Achmad Naufal Syafiq
Nov 23 at 9:16












looks correct for me unfortunately...
– Vladyslav Matviienko
Nov 23 at 9:23




looks correct for me unfortunately...
– Vladyslav Matviienko
Nov 23 at 9:23

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443434%2fhow-to-measure-bitmap-with-android-dp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443434%2fhow-to-measure-bitmap-with-android-dp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks