iOS Question Need Help Cropping an Image

mollensoft

Member
Licensed User
Longtime User
Hello All, I'm trying to figure out how to crop an image - it does not seem to be working the way it looks in other posts on the same subject using the DrawBitmap Method.

1. The Attached project has two image views and a button.

2. When the page loads, a red and blue box image is loaded into Imageview1

3. When the user clicks the "Crop Picture" button, I'm trying to Only copy the blue part of the picture into the second image view (without the red boarder)

Other posts indicate the DrawBitmap Method is best but I cannot get it working

Thank you for any help or insights.

-Alan
 

Attachments

  • croptest.zip
    3.1 KB · Views: 149

mollensoft

Member
Licensed User
Longtime User
Hi Klause, thank you - i was not aware of that class.

I fixed my issue - I did not understand how the canvas really worked -

-Alan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
first you should change the title of the thread if you changed the issue topic because it will be simpler for other people to find threads that are related to their issues.

there are several problems with your code.

1. you are loading a bitmap with a specific resolution to a imageview with a specific size. imageview size and bitmap resolution are not the same so you cannot thread the size as the bitmap size since you are cropping it with imageview size. you imagevie w contentmode is set to fill that means the bitmap will be fill the whole imageview and the whole bitmap will be shown so you must take this in account when you want to crop a bitmap from an imageview that both have different size.

2. the srcsect (searchrect should be the part with the resized parameters and not dstrect. because destination rect should be the whole view size and not a part of it. the search rect will only be cropped a part of it and put it in full size of the destination rect.

3. because it will be harder to crop the bitmap itself it would make more sense to use b4xview and return a snapshot of the imageview1 and like this you can really handle the size of the cropped part simpler.

4. note the the rect is initialize with left, top, RIGHT, BOTTOM but accoring to you code you handle it as left, top, WIDTH, HEIGHT and you will get different results.

correct code:

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 8
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private CropPicture As Button
    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    Dim temp_bmp As Bitmap = LoadBitmap(File.DirAssets,"boxes.png")
    ImageView1.Bitmap = temp_bmp
End Sub


Private Sub Page1_Resize(Width As Int, Height As Int)
   
End Sub

Sub CropPicture_Click
    Log("Attempting To Crop Picture ")

    Dim canvas1 As Canvas
    Dim b4xv As B4XView = ImageView1

    canvas1.Initialize(ImageView2)
   
    Dim SRCRect As Rect
    SRCRect.Initialize(60, 30, ImageView1.Width-60, ImageView1.Height-30)
   
    Dim DSTRect As Rect
    DSTRect.Initialize(0,0,ImageView2.Width,ImageView2.Height)
   
    DrawBitmap(canvas1, b4xv.Snapshot, SRCRect, DSTRect)
   
End Sub


Sub DrawBitmap(canvas1 As Canvas, Bitmap1 As Bitmap, SrcRect As Rect, DestRect As Rect)
    If SrcRect = Null Then
        Dim SrcRect As Rect
        SrcRect.Initialize(0, 0, Bitmap1.Width, Bitmap1.Height)
    End If
    Dim p1 As Path
    p1.InitializeRect(DestRect, 0)
    canvas1.ClipPath(p1)
    Dim sx, sy As Float
    sx = (DestRect.Right - DestRect.Left) / (SrcRect.Right - SrcRect.Left)
    sy = (DestRect.Bottom - DestRect.Top) / (SrcRect.Bottom - SrcRect.Top)
    Dim x, y, width, height As Int
    x = DestRect.Left - sx * SrcRect.Left
    y = DestRect.Top - sy * SrcRect.Top
    width = Bitmap1.Width * sx
    height = Bitmap1.Height * sy
    Dim d2 As Rect
    d2.Initialize(x, y, x + width, y + height)
    canvas1.DrawBitmap(Bitmap1, d2)
    canvas1.RemoveClip
    canvas1.Refresh
    Log("End of DrawBitmap, Path Clip Removed, Canvas Refreshed")
End Sub
 
Upvote 0

mollensoft

Member
Licensed User
Longtime User
Thank You Ilan, I really appreciate your thorough answer, I did end up using the B4XView - I did not completely understand that the SRCrect and DSTTrect details so thank you for the insight - greatly appreciated

-Alan
 
Upvote 0
Top