Birrel
Birrel

Reputation: 4834

Android, get real-world dimensions (inches, cm) from pixel difference?

I have two image views that the user can move around on the screen:

<ImageView
    android:layout_width="35dp"
    android:layout_height="match_parent"
    android:id="@+id/leftRulerView"/>

<ImageView
    android:layout_width="35dp"
    android:layout_height="match_parent"
    android:id="@+id/rightRulerView"/>

The are initialized in the following way:

mLeftRuler = (ImageView) view.findViewById(R.id.leftRulerView);
mRightRuler = (ImageView) view.findViewById(R.id.rightRulerView);

When either is moving, I am calculating the actual physical distance between the two views (this will be used for measuring real-world objects). rightRulerView is ALWAYS to the right of the left, and so the distance between them is from the right edge of the left-most view to the left edge of the right view. Refer to the following image:

enter image description here

First, het the pixel separation of the two views:

float distance = mRightRuler.getX() - (mLeftRuler.getX() + mLeftRuler.getWidth());

This works out because the getX() returns the x-position of the left-side of the view. I AM interested in the left side of the right ruler, but I need to add the width of the left ruler to get the location of the right side of it. Then, just subtract the two for the pixel count between the two. This works great and gives the pixel count between the views just fine.

To convert this to inches, I do the following:

DisplayMetrics mDisplayMetrics = getResources().getDisplayMetrics();
float mDPI = (float) mDisplayMetrics.densityDpi;

float measurement = distance / mDPI;

This all works fairly well, but it is not perfect. When is says "1.00 inches" between the two views, it is actually just under 1 1/8 inches (measured with a ruler on the physical screen). The inaccuracy propagates to larger distances (i.e. "3.00 inches" in the app is actually measured to a little over 3 1/4 inches with a ruler on the screen).

There are ruler apps out there, that are accurate to real-world rulers, so how can I convert the pixel distance between the two views into an accurate measurement?

Upvotes: 0

Views: 2368

Answers (3)

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28516

In API 17 getRealMetrics() has been introduced. It should give you accurate values in xdpi and ydpi.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);

However, if you need to support lower API versions there is no way you can achieve your goal without user input and calibration.

Upvotes: 0

sergiomse
sergiomse

Reputation: 785

In DisplayMetrics you can use

mDisplayMetrics.xdpi The exact physical pixels per inch of the screen in the X dimension. mDisplayMetrics.ydpi The exact physical pixels per inch of the screen in the Y dimension.

Then you can do some simple math to get the real distance

Upvotes: 1

PlatinTato
PlatinTato

Reputation: 378

there is to my knowledge at least no way to get a precise physical size without any userinput. what i did , is make a popup where the user has to input the diameter for example and then use some 7th grade math and get to the physical size of the Screen edges, assuming they are squares. and then you have smthing like "0.1mm/pixel" and then you could obviously keep on going with your method.

Upvotes: 1

Related Questions