iOS Question Image Resize & Crop question

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Guys

I have the following requirements:
1. Use the camera to take a picture
2. Display the picture in an image view with the proper scale (meaning without looking stretched)
3. Crop the picture and display on a white backdground equivalent to the original picture size
4. Upload the picture to the server


After the image is captured by the camera, I am calling ResizeImage, then I am displaying the image inside the ImageView, and results are as expected (meaning no stretching )


for B4A I am using

B4X:
Sub ResizeImage(original As Bitmap, TargetX As Int, TargetY As Int) As Bitmap
    Dim origRatio As Float = original.Width / original.Height
    Dim targetRatio As Float = TargetX / TargetY
    Dim scale As Float
   
    If targetRatio > origRatio Then
        scale = TargetY / original.Height
    Else
        scale = TargetX / original.Width
    End If
   
    Dim c As Canvas
    Dim b As Bitmap
   
    b.InitializeMutable(TargetX, TargetY)
    c.Initialize2(b)
    'set the background
    c.DrawColor(Colors.LightGray)
    Dim r As Rect
    Dim w = original.Width * scale, h = original.Height * scale As Int
    r.Initialize(TargetX / 2 - w / 2, TargetY / 2 - h / 2, TargetX / 2 + w / 2, TargetY / 2+ h / 2)
    c.DrawBitmap(original, Null, r)

    Log(b.Width & " " & b.Height)
    Return b
End Sub


For B4I

B4X:
Sub ResizeBitmap(bmp As Bitmap, TargetX As Float, TargetY As Float) As Bitmap
   
   
   Dim img As ImageView
  
   Dim scaleX As Float = bmp.Width/ TargetX
   Dim scaleY As Float = bmp.Height/ TargetY
  
   img.Initialize("")
   img.Width = bmp.Width * scaleX
   img.Height = bmp.Height * scaleY
   Dim cvs As Canvas
   cvs.Initialize(img)
   cvs.DrawBitmap(bmp, cvs.TargetRect)
   Dim res As Bitmap = cvs.CreateBitmap
   cvs.Release
   Return res
End Sub


The interesting thing is, later on why I use
B4X:
 Dim bmp3 As Bitmap = ImageView1.Bitmap

and then if I try to find the ratio of bmp against imageView1, for B4A it is 1, for B4I it is 3.
Can some one please clarify, why B4I doesn't report 1, and what is the proper way of doing this


Next, can someone help with the correct syntax for the equivalent B4I code for the croping

B4X:
'This one returns a white image with the selected section on top
Private Sub GetCroppedImage2()
   
   

    Private rectSource, rectDest As Rect
    Private cLeft, cTop As Int
   
    Dim rectImageWidth As Int
    Dim rectImageHeight As Int
   
    rectImageWidth = rectImage.Right - rectImage.Left
    rectImageHeight = rectImage.Bottom - rectImage.Top
   
'    imvCroped.Height = imvCroped.Width / rectImageWidth * rectImageHeight
#if B4A   
    imvCroped.Gravity = Gravity.FILL
#else   
    imvCroped.ContentMode = imvCroped.MODE_FILL
#end If   
   
    Private cvsCroped As Canvas
    cvsCroped.Initialize(imvCroped)
   
    cLeft = (rectImage.Left - imvImage.Left) * PixelScaleX
    cTop = (rectImage.Top - imvImage.Top) * PixelScaleY
   
    rectSource.Initialize(cLeft, cTop, cLeft + rectImageWidth * PixelScaleX, cTop + rectImageHeight * PixelScaleY)
    rectDest.Initialize(cLeft, cTop, cLeft + rectImageWidth * PixelScaleX, cTop + rectImageHeight * PixelScaleY)
   

#if B4A
    bmpCroped.InitializeMutable(bmpImage.Width, bmpImage.Height)
    cvsCroped.Initialize2(bmpCroped)
    cvsCroped.DrawColor(Colors.White)

    cvsCroped.DrawBitmap(bmpImage, rectSource, rectDest)
    imvCroped.Bitmap = bmpCroped
#else   
   I am looking for the equivalent code to fill in here
   
#end if
   
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The screen size (width and height) reported in iOS / B4i is normalized (scale = 1). It is similar to using 'dip' units in B4A.

You can find the true scale with GetDeviceLayoutValues.NonnormalizedScale. I guess that this is the source for the difference.

This sub creates a canvas that with a specific size:
B4X:
Public Sub CreateCanvas (width1 As Int, height1 As Int) As Canvas
   Dim iv As ImageView
   iv.Initialize("")
   iv.SetLayoutAnimated(0, 1, 0, 0, width1, height1)
   Dim c As Canvas
   c.Initialize(iv)
   Return c
End Sub
It is similar to B4A canvas with mutable bitmap.
 
Upvote 0
Top