Android Question Crop limitation

Douglas Farias

Expert
Licensed User
Longtime User
good morning *-*

i have a question about crop

how can i add a minimal limitation to my crop with this code?

exemple minimal size crop is width 20%y
may not be lower than that

max size dont need limitation only minimal can someone help me with this?

i m using this example http://www.b4x.com/android/forum/threads/resize-and-crop.19550/#post-113987

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: CropSquare2
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim Left, Top, Right, Bottom, Width As Int
    Dim DeltaDot, Delta, DeltaX, DeltaY, StartX, StartY As Int
    Dim fTopLeft, fTopRight, fBottomLeft, fBottomRight, fCenter As Boolean
End Sub

Sub Globals
   
    Dim imvImage,p2 As ImageView
    Dim imvImage2 As ImageView
    Dim pnlAction As Panel
    Dim cvsAction As Canvas
    Dim rectImage,r2,r3 As Rect
    Dim Crop As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    imvImage.Initialize("")
    imvImage2.Initialize("")
       
    Activity.AddView(imvImage,  0, 0, 444, 444)
    imvImage.Bitmap= LoadBitmap(File.DirAssets, "ball.jpg")
    imvImage.Gravity = Gravity.FILL
   
    Crop.Initialize("crop")
    Crop.Text = "PRESS TO CROP PHOTO"
    Activity.AddView(Crop, 0, 80%y, 100%x, 10%y )
   
    p2.Initialize("p2")
    Activity.AddView(p2,  0, 0, 444, 444)
   
    pnlAction.Initialize("pnlAction")
    Activity.AddView(pnlAction, 10, 10, 100%x, 100%y )
    cvsAction.Initialize(pnlAction)
   
    Width = imvImage.Height
    Top = imvImage.Top
    Bottom = Top + Width /2
    Left = 33%x - Width / 2
    Right = Left + Width /2
    rectImage.Initialize(Left, Top, Right, Bottom)
    r2.Initialize(Left+25dip, Top, Right-25dip, Bottom)
    r3.Initialize(Left, Top+25dip, Right, Bottom-25dip)
   
    cvsAction.DrawRect(rectImage, Colors.Transparent, True , 20)
    cvsAction.DrawRect(rectImage, Colors.yellow, False , 10)
   
    Crop.BringToFront
    DeltaDot = 10dip
End Sub

Sub crop_click

    Dim out As OutputStream
    Dim bmp As Bitmap
    Dim fileName As String


    Dim cvs As Canvas         
    cvs.Initialize(imvImage)   
       
   
    Dim Bitmap1 As Bitmap
 
    Bitmap1.Initialize3(imvImage.Bitmap)

    Dim SrcRect As Rect
                                           
    SrcRect.Initialize( Left, Top, Right, Bottom)

    Dim DestRect As Rect
    DestRect.Initialize(0,0,444,444 )

    cvs.DrawBitmap(Bitmap1, SrcRect, DestRect)

    Dim out As OutputStream
    fileName = "1998.png"
    out = File.OpenOutput (File.DirRootExternal, fileName ,  False)
    cvs.Bitmap.WriteToStream(out,  100, "PNG")
    out.Close

    imvImage.RemoveView

    imvImage2.Initialize("")
    Activity.AddView(imvImage2, 0, 0, 444, 444)
   
    ' klaus
    ' We are obviously saving low resolution
    ' To get high resolution we might have to tap into
    ' andrews jpeg library to grab the dimensions?
    '
    imvImage2.Bitmap= LoadBitmap(File.DirRootExternal, "1998.png")
    imvImage2.Gravity=Gravity.FILL
    imvImage2.Invalidate
   
    Activity.Invalidate
    pnlAction.BringToFront
    Crop.BringToFront

'cvsAction.DrawRect(DestRect, Colors.Transparent, True , 25)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pnlAction_Touch (Action As Int, X As Float, Y As Float)
    Select Action
    Case Activity.ACTION_DOWN
        fTopLeft = False
        fTopRight = False
        fBottomLeft = False
        fBottomRight = False
        fCenter = False
        If X >= Left - DeltaDot AND X <= Left + DeltaDot AND Y >= Top - DeltaDot AND Y <= Top + DeltaDot Then
            fTopLeft = True
            Activity.Title = "TopLeft"
        Else If X >= Left - DeltaDot AND X <= Left + DeltaDot AND Y >= Bottom - DeltaDot AND Y <= Bottom + DeltaDot Then
            fBottomLeft = True
            Activity.Title = "BottomLeft"
        Else If X >= Right - DeltaDot AND X <= Right + DeltaDot AND Y >= Top - DeltaDot AND Y <= Top + DeltaDot Then
            fTopRight = True
            Activity.Title = "TopRight"
        Else If X >= Right - DeltaDot AND X <= Right + DeltaDot AND Y >= Bottom - DeltaDot AND Y <= Bottom + DeltaDot Then
            fBottomRight = True
            Activity.Title = "BottomRight"
        Else If X >= Left + DeltaDot AND X < Right - DeltaDot AND Y >= Top + DeltaDot AND Y < Bottom - DeltaDot Then
            fCenter = True
            Activity.Title = "Center"
        End If       
        StartX = X
        StartY = Y
       
       
       
    Case Activity.ACTION_MOVE
    cvsAction.RemoveClip
    pnlAction.Invalidate
   
        cvsAction.DrawRect(rectImage, Colors.Transparent, False , 55)
            pnlAction.Invalidate2(rectImage)
           
        Dim Diff, Sign As Int
        DeltaX = X - StartX
        DeltaY = Y - StartY
        If Abs(DeltaX) >= Abs(DeltaY) Then
            Delta = DeltaX
        Else
            Delta = DeltaY
        End If
        'cvsAction.DrawRect(rectImage, Colors.ARGB(128, 0, 0, 0), True , 35)
        pnlAction.Invalidate2(rectImage)
        If fCenter = True Then
            rectImage.Left = Left + DeltaX
            rectImage.Right = Right + DeltaX
            rectImage.Top = Top + DeltaY
            rectImage.Bottom = Bottom + DeltaY
            If rectImage.Left < imvImage.Left Then
                Diff = imvImage.Left - rectImage.Left
                rectImage.Left = imvImage.Left
                rectImage.Right = rectImage.Right + Diff
            End If
            If rectImage.Top < imvImage.Top Then
                Diff = imvImage.Top - rectImage.Top
                rectImage.Top = imvImage.Top
                rectImage.Bottom  = rectImage.Bottom + Diff
            End If
            If rectImage.Right >  imvImage.Left + imvImage.Width Then
                Diff = rectImage.Right - (imvImage.Left + imvImage.Width)
                rectImage.Right = imvImage.Left + imvImage.Width
                rectImage.Left  = rectImage.Left - Diff
            End If
            If rectImage.Bottom > imvImage.Top + imvImage.Height Then
                Diff = rectImage.Bottom - (imvImage.Top + imvImage.Height)
                rectImage.Bottom = imvImage.Top + imvImage.Height
                rectImage.Top  = rectImage.Top - Diff
            End If
        Else
            If fTopLeft = True Then
                If Left + Delta > imvImage.Left AND Top + Delta > imvImage.Top Then
                    rectImage.Left = Left + Delta
                    rectImage.Top = Top + Delta
                End If
            End If
            If fTopRight = True AND DeltaX <> 0 Then
                Sign = DeltaY / Abs(DeltaY)
                If Abs(DeltaX) >= Abs(DeltaY) Then
                    Delta = Abs(DeltaX) * Sign
                Else
                    Delta = DeltaY
                End If
                If Top + Delta > imvImage.Top AND Right - Delta < imvImage.Left + imvImage.Width Then
                    rectImage.Top = Top + Delta
                    rectImage.Right = Right - Delta
                End If
            End If
            If fBottomLeft = True Then
                Sign = DeltaY / Abs(DeltaY)
                If Abs(DeltaX) >= Abs(DeltaY) Then
                    Delta = Abs(DeltaX) * Sign
                Else
                    Delta = DeltaY
                End If
                If Bottom + Delta < imvImage.Top + imvImage.Height AND Left - Delta > imvImage.Left Then
                    rectImage.Bottom = Bottom + Delta
                    rectImage.Left = Left - Delta
                End If
            End If
            If fBottomRight = True Then
                If Bottom + Delta < imvImage.Top + imvImage.Height AND Right + Delta < imvImage.Left + imvImage.Width Then
                    rectImage.Bottom = Bottom + Delta
                    rectImage.Right = Right + Delta
                End If
            End If
        End If
       
            pnlAction.Invalidate2(rectImage)
       
        cvsAction.DrawRect(rectImage, Colors.Transparent, True , 27)
        cvsAction.DrawRect(rectImage, Colors.yellow, False ,2)
       
   
   
       
    Case Activity.ACTION_UP
   
        Left = rectImage.Left
        Top = rectImage.Top
        Right = rectImage.Right
        Bottom = rectImage.Bottom
       
        cvsAction.DrawRect(rectImage, Colors.Transparent, True , 26)
        cvsAction.DrawRect(rectImage, Colors.black, False , 10)
        cvsAction.DrawRect(rectImage, Colors.white, False , 2)
       
   
        pnlAction.Invalidate2(rectImage)
        Activity.Title = ""
    End Select
End Sub
 
Top