Android Question Images display distorted

HimeAnator

Member
Licensed User
Longtime User
In my app I load an image into an image view that has a width of 268 and a height of 160. Also if the user clicks the image it shows a full screen version. However users are reporting that their images are distorted.

the code I use to load the image view
B4X:
            Dim AddImage As Bitmap

                AddImage = LoadBitmapSample(File.DirRootExternal", DisplayText.text & ".jpg", ImagePreview.Width, ImagePreview.Height)
                AddImage = CreateScaledBitmap(AddImage ,ImagePreview.Width, ImagePreview.Height)
                       
            ImagePreview.Bitmap = (AddImage )

and the code I use to display the full screen version (I open a new activity and load the image as the background)
B4X:
Dim Image As Bitmap
           
                   
                Image = LoadBitmapSample(File.DirRootExternal", Main.DisplayText & ".jpg",Activity.Width,Activity.Height)
                Image = CreateScaledBitmap(Image ,Activity.Width,Activity.Height)
               
        Activity.SetBackgroundImage(Image )


When the images were saved they were initialized using this code
B4X:
bmp.InitializeSample(Dir, FileName,Activity.Width,Activity.Height)

Any body know where I'm going wrong? Any help is much appreciated.
 

sorex

Expert
Licensed User
Longtime User
you should calculate the aspect ration of the picture and apply it on the dimensions based on the screen sizes.

not every screen has the same aspect ratio as your picture.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I don't even know how to get the dimensions of a picture in b4A, lol.

but it should be something like this

B4X:
ratio=(100%x)/(100%y)
if 100%x > 100%y then
image.width=100%x
image.height=image.height*ratio
else
image.width=image.width*ratio
image.height=100%y
end if

maybe there is a library that does this for you, don't know about that.



at second thought it might've been this

B4X:
ratio=(100%x)/(100%y)
if 100%x > 100%y then
image.width=100%x
image.height=(100%x)*ratio
else
image.width=(100%y)*ratio
image.height=100%y
end if
 
Upvote 0

eps

Expert
Licensed User
Longtime User
You need to work out the largest possible dimension on the longest dimension of the image

If it's the x dimension, then work out what space you've got on width, with the current screen resolution and then work from that.

Let's say the image is 268x160. WxH

The device's screen is 320x480 WxH

You can scale 268 up to 320, but then you need to scale the 160 by the same amount.

320/268 = 1.19...

Round this down to 1.19, which gives :

268x1.19 = 319

and

160x1.19 = 190

Or something similar..

Hope this helps
 
Last edited:
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
Thank you both for the responses. Sorry it has taken me so long to get back to you on my progress with this issue. You both have a been a huge help and I think I was able to come up with the right fix. The images that are loaded are always taken in landscape mode so I ended up with code looking like this...
B4X:
      Dim AddImage As Bitmap
       
         AddImage = LoadBitmap(File.DirRootExternal, DisplayText.text & ".jpg")
         Dim OrigWidth
         Dim OrigHeight
         Dim NewHeight
         
         OrigWidth = AddImage.Width
         OrigHeight = AddImage.Height
         
         NewHeight = OrigHeight / OrigWidth * ImagePreview.Width
         
         AddImage = CreateScaledBitmap(AddImage3,ImagePreview.Width,NewHeight)
       
             
ImagePreview.Bitmap = (AddImage)

I don't get any errors and the images fill the ImageView and seem to display without any noticeable distortion on my phone however Ive never really encountered the issue, I have only heard reports of it. So would this code get the job done? Also in order to get the code to work I had to take out the "LoadBitmapSample" part when loading the image. Will loading images without that still be ok? or will it cause issues?
 
  • Like
Reactions: eps
Upvote 0

Informatix

Expert
Licensed User
Longtime User
There's a better approach. By using the Accelerated Surface library, you can know the size of your images WITHOUT loading them (no risk of OutOfMemory with large images). And you can resample and rescale them while you load them with LoadScaledBitmap. Example:
B4X:
Dim IU As AS_ImageUtils
Dim mapSize As Map = IU.GetImageDimensions(File.DirRootExternal, DisplayText.text & ".jpg")
Dim NewHeight As Int = mapSize.Get("height") / mapSize.Get("width") * ImagePreview.Width
ImagePreview.Bitmap = IU.LoadScaledBitmap(File.DirRootExternal, DisplayText.text, ImagePreview.Height, ImagePreview.Width, True)
In case of problem, the function traps the OutOfMemory error and returns it as an exception.
 
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
Very simply, thank you so much Informatix!
I replaced my original code with the one you listed above and the images are loading with no issues. At first I was getting a an "Invalid Double" error but I removed the following line..
B4X:
Dim NewHeight = mapSize.Get("height") / mapSize.Get("width") *ImagePreview.Width
and it started working.

I don't really need that line since this line does the work right?
B4X:
ImagePreview.Bitmap = IU.LoadScaledBitmap(File.DirRootExternal, DisplayText.text & ".jpg", ImagePreview.Height, ImagePreview.Width, True)
 
  • Like
Reactions: eps
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Very simply, thank you so much Informatix!
I replaced my original code with the one you listed above and the images are loading with no issues. At first I was getting a an "Invalid Double" error but I removed the following line..
B4X:
Dim NewHeight = mapSize.Get("height") / mapSize.Get("width") *ImagePreview.Width
and it started working.

I don't really need that line since this line does the work right?
B4X:
ImagePreview.Bitmap = IU.LoadScaledBitmap(File.DirRootExternal, DisplayText.text & ".jpg", ImagePreview.Height, ImagePreview.Width, True)
I didn't test my code so there's maybe an error with the mapSize.Get in Dim NewHeight. The reason of this line is to compute the height as in your example. I don't know whether it's useful or not. It's you who decide. If you don't need it, then you can remove also the call to GetImageDimensions.
 
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
Well I tried getting the height before in an attempted to make sure images display with the correct aspect ratio to eliminate any image distortion.
So does "IU.LoadScaledBitmap" do this for me and make sure images display right across all screen ratios?
 
Last edited:
Upvote 0
Top