How to get image dimension in inches without knowing the DPI? - java

I am new in android studio... I was searching as to how i can convert the dimensions of an image. I found out that you need to get the DPI. My question is will it be possible to convert the unit of the image, to inches, without knowing the DPI?? I tried using .getheight() and .getwidth() and it does give me the resolution, my problem is I need to convert this to inches. Also, I saw this "dp" in imageview... I'm wondering wheter this has an equivalent value in inches that I can use to convert my image using ratio and proportion. Also found out bitmap.getdensity(). Does that get the actual dpi of the image??

How to get image dimension in inches without knowing the DPI?
The question doesn't make much sense.
In general, a digital image has no inherent size in inches, or an inherent DPI (dots per inch). It has a size in pixels (or dots).
The DPI is actually a property of the device you are displaying the image on. For an image that is displayed, you multiply the DPI of the device by the number of pixels in the height or width of the image to give you the height / width of the image on the screen. (If the image has been scaled, the scaling factor needs to be taken into account too.)
In the case of images produced by digital cameras, the metadata of the image may include a "resolution height DPI" and "resolution width DPI". I couldn't find a explanation of what these are supposed to mean, but I surmise that they are either
the physical resolution of the capture device (e.g. CCD), or
a value chosen by the creator of the image to give you their preferred image size.
Neither of these is of much relevance when you are displaying an image. (IMO)

Related

Why are the values returned by getWidth() and getHeight() double the actual image dimensions?

I'm developing for Android, and my program involves a lot of bitmap images - these are set using BitmapFactory. One of my images is 80x30, and I assign it to a Bitmap variable wit the following instruction:
bmapImg = BitmapFactory.decodeResource(currContext.getResources(), R.drawable.brick);
When I then use the instruction
myImgWidth = bmapImg.getWidth();
myImgWidth is evaluated to 160 instead of 80. Likewise, when I run
myImgHeight = bmapImg.getHeight();
myImgHeight is evaluated to 60 instead of 30. I've looked through other questions on StackOverflow relating to these methods, but all of those issues were about getWidth() and getHeight() returning 0. Am I doing something wrong, or is there a reason as to why these functions return double the actual image dimensions?
You using decodeResource, so image is scaled based on your DPI. To scale it to size you need, place it in different folder. I suggest drawable_xhdpi is what you need, if scale factor is 2.
And keep in mind, it will be scaled on devices with different screen density.
To completely avoid scaling you may put your image to assets folder for example.
It is all about which screen you are running your app on. The values returned by those methods are the pixels that the image takes on the current screen. They will return values that are 1:1 with your image's resolution if you are running on a mdpi screen and you used 80dp and 30dp respectively for their dimensions. I would bet you are probably running your app on a xhdpi screen and do not have a resource for that density bucket
As mentioned in other answer, image is resized based on the device you check on.
If you don't want android to resize your image based on DPI, I recommend you to add your image in drawable-nodpi folder of your project

Correct calculation from ldpi dp values for mdpi and so on

I'm having a gridview for which I determine the cell size like this (in the xml file):
android:columnWidth="#dimen/gridview_cell_dimen"
As you can see I am getting the dp dimension from my dimens folders (values-ldpi, values-hdpi,...).
My question now is - how to determine the correct dp sizes?
For ldpi I simply use 20dp in this case, which perfectly fits the size from my smartphone. What would be the correct forumla to see which size would be correctly suitable for bigger screen resolutions like mdpi, hdpi, xhdpi, xxdpi?
Is there a rule of thumb which says: "Increase the size by 120%?" for example? So that I can say that the best resolution for mdpi would be: 24dp ? and for hdpi: 28dp in my case?
Thanks in advance!
using dp as the unit is independent of the density of the screen, so you can stick to one value for all screen sizes.
From the Documentation:
Density-independent pixel (dp) A virtual pixel unit that you should
use when defining UI layout, to express layout dimensions or position
in a density-independent way. The density-independent pixel is
equivalent to one physical pixel on a 160 dpi screen, which is the
baseline density assumed by the system for a "medium" density screen.
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
The conversion of dp units to screen pixels is simple: px = dp * (dpi
/ 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your
application's UI, to ensure proper display of your UI on screens with
different densities.

About image in IText 5.2.1

When you zoom in PDF, text will not be distorted, the image distortion.
How to make the picture enlarged without distortion, But don't vector map.
the problem that you are facing is about your image's DPI. Most PDF viewers draw the image at a fixed dpi regardless of the image's dpi. My guess is you are using a 150 dpi or so. Try playing with the dpi value to find a good fit for the area you are trying to draw your image into and the size of the original image.
Also make sure your original image is big enough for the area you are trying to cover.

Validate that uploaded image DPI and size is printable

My web application requires users to upload a passport-style photo of themselves. This photo will be used to generate several images:
web avatars of multiple sizes to display within the application (72 dpi)
printable image to print a 1"x1" face shot using an ID card printer (300 dpi)
printable image to print in reports on a standard printer (300+ dpi)
I'm going to use a jquery-based image cropping tool (perhaps JCrop) so that the user can select an area just around their face and disregard the rest of the image.
Are there any techniques to make sure that the image that is uploaded is of high enough resolution that it can be printed to the card printer and regular printers with a dimension of at least 1" x 1"?
My understanding is that EXIM dpi information is not reliable. Should I simply verify that the size they select in the crop equates to at least 300x300 pixels in the raw image?
Would it be best to handle this on the client in javascript or on the server (which is using Java)?
Well if you want to make sure the image resolution is big enough so that it can be printed at 300dpi with a good quality you just need to make sure that the part that is being selected by the user.
After having a quick look on JCrop it seem like you can access the coordinates of the selected image part easily (using showCoords() ).
With that you know the size of the selected image parts in pixels. It now depends on how big you want to print your image with 300dpi.
Therefore for i.e. an US Letter at 300dpi it needs to be 2550x3300px. For DIN A4 it would be 2480x3508 pixels.
So out of the coordinates you get from JCrop simply calculate how big the rectangle dimensions are in pixels and check if it's big enough to be printed to the size you desire at 300dpi...
Hope that helps...
Edit:
If you want to make sure the image is correct, by which I mean it has a face that fills about 80% of the image you could try using a python script that uses OpenCV... OpenCV already provides basic face detection algorithms. So maybe you can have the uploaded image run through the face detection algorithm which then says whether it contains a face or not...

Java image scaling

I am outputting images to a PDF file using iText. The images always appear larger than they are supposed to. According to the book (iText in Action), this is because iText always displays the images at a resolution of 72 dpi, regardless of what the actual dpi property of the image is. The book suggests using image.getDpiX() to find the dpi of the image, and then using image.scalePercent(72 / actualDpi * 100) to display the image properly. So far, the getDpiX() property of all my images have returned 0 (I've tried 2 gifs and 1 jpg). Is there another way to figure out the actual DPI so that my images scale properly?
com.lowagie.text.Image graphic = com.lowagie.text.Image.getInstance(imgPath);
float actualDpi = graphic.getDpiX();
if (actualDpi > 0)
//Never gets here
graphic.scalePercent(72f / actualDpi * 100);
According to the com.lowagie.text.Image JavaDoc, the method getDpiX gets the dots-per-inch in the X direction. Returns zero if not available.
You're going to have to assume a value when the getDpiX method returns zero. 100 dpi is as good an assumption as any.
if (actualDpi <= 0) actualDpi = 100f;
graphic.scalePercent(72f / actualDpi * 100f);
For GIF, there is no place to store a "DPI" information in the file, so "actualDpi" has no meaning in that case. For JPEG, the "DPI" information can be stored in the file, but is not mandatory, and if not set: "actualDPI" has no meaning as well.
The real answer is: there is no such thing as "actual DPI", either the information is provided (ie. "in this image I want 1 pixel to be rendered with this specific physical width (or height)"), or it's not. Another element is in your sentence: "always appear larger than they are supposed to"; the "supposed to" is the DPI information stored in the image. So if this information is absent, and you feel that when you open the image it seems right on the screen, then you have to calculate the density of your screen (width in number of pixels divided by the width in inches of your screen), and use that as your "actualDPI" variable.
The screen-resolution, retrieved by
java.awt.Toolkit.getDefaultToolkit().getScreenResolution()
did not help, image was still too big. However, one can scale the image to the page width in the following way:
BufferedImage bufferedImage = null; // your image
Image image = Image.getInstance(bufferedImage,null);
image.scalePercent((document.right()-document.left())/((float)image.getWidth())*100);

Categories

Resources