Search code examples
javaandroidpixel

How to convert from pixel unit to square inches of an image in android studio?


I am doing an Android Project. I have area of the image in pixel per unit. How can I convert that to square inches

I have tested the conversion of unit in one particular image. I have used this particular formula for conversion in Java.

float inchesWidth = (float) width / dpi;
float inchesHeight = (float) height / dpi;
float areaInSquareInches = inchesWidth * inchesHeight;

Suppose the Width of the image is 678. The height of the image is 452. The dpi of the Android device is 440. I am getting area = 1.58 square inches. Can anyone confirm me that I am using right formula and right method because there is no way to confirm this.


Solution

  • Assuming you're not scaling the image at all, your math is right. If you're scaling, you need to multiply by the scale factor (and the x and y scale factors can be independent, although generally they are the same). You also have to make sure you're using the right dpi- you want to use the logical dpi of the running OS, not the native dpi of the physical device (some devices might have a physical dpi of say 300 pixels per inch, but the OS will pretend its some smaller number (say 250) to provide some magnification).