Android Question Images resample question

Sub7

Active Member
Licensed User
Longtime User
IF i want to take a picture with the camera and later i want to show it on a imageview which cover the screen 100% horizontally and lets say 80% vertically, should i set the camera picture size to the same size of my view or let the image adapt with using gravity?
But in this case aint the result image a distorted image?
Setting the camera output size accordling screen dimensions looks complicated if not impossible to me.

how do i cope to different screen sizes?

In another scenario, i have a very big image, let's say his dimensions are 3872*2592 my thought is to use loadbitmapsample to resample the image and save memory giving as width the same size of the imageview and height proportionally calculated by maintaining the aspect ratio with this formula:

height / width * imageview.heigh = new height

Is that correct?

Thanks
 

sorex

Expert
Licensed User
Longtime User
no.

to fit your landscape picture to lets say a screen of 480x800 you (could) do

r=width/height
w=480
h=w*r

r=3872/2592 '= 1.49
w=480
h=480*1.49 ' = 715

you should base the math on what you prefer to fill to, in this case I prefer full width above full height.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Hi Luca, I have read your thread, in my case the imageview has not fixed dimensions but it is variable depending on screen size as it *anchor horizontally for the full width of the screen.
Recompile an image will result in quality loss with any software but in another other hand i cannot let load a 3872*2592 image, so for that resizing is the way, at least for what i can understand, i am really a rookie about images.

I want to find a way to resize an image proportionally, covering all the width of my view and maintaining the aspect ratio, that formula should to that.

Of course if the image is smaller than the view then it will be stretched, or maybe in this case i could add an *exception to center the image without stretching. ?

Should i make all this calculations by myself or there is another way? i am wrong?
 
Last edited:
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
I want to avoit to load enormous images in memory:
Private bmp AsBitmap = LoadBitmap(Dir, FileName) does this action load them in memory on only after imv.invalidate is fired?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you have the calculations I gave you. you just need to adopt it a bit like

B4X:
if image.width>480 or image.height>480 then 'only resize if bigger than screen size
 r=image.width/image.height
 if image.width<image.height then
  w=480 'portrait
  h=w*r
 else
  h=480 'landscape
  w=h*r
 end if
end if

probably not 100% right but you get the idea.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User

Thank you, still, .width i need to call loadbitmap, this goes straight to memory or later when i update the imageview?
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
lol i know, but loadbitmapSample aspects two paraments, how i can know them BEFORE load the image, and make the calculations! that's the point
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you can't, you need to load it first unless there is a library to allows that loading the header only.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
So potentially anoyone can open up an image that is 400000*700000 (exageration) and make the app crash.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
B4X:
  Dim IM As AS_ImageUtils
im.GetImageDimensions

*The image is not loaded into memory, from @Informatix accellerated surface lib

Solved, thanks
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that jpglib might do it aswell but I prevent the use of extra libs as much as possible.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
nice, didn't know about that one (there's too much out there actually)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
B4X:
  Dim IM As AS_ImageUtils
im.GetImageDimensions

*The image is not loaded into memory, from @Informatix accellerated surface lib

Solved, thanks
And I suggest strongly to use LoadScaledBitmap from this lib instead of LoadBitmapSample because LoadBitmapSample just resamples the image, so the final size of the image is not exactly the size you defined. LoadScaledBitmap will really resize it (and moreover the density won't be changed, so no surprise with ImageViews). With big images, the difference between these functions is far from being neglectable.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…