B4A Library CropImageView V1.0

Discussion in 'Additional libraries, classes and official updates' started by DonManfred, Mar 26, 2015.

  1. DonManfred

    DonManfred Expert Licensed User


    This is a wrapper for this GitHub Project.


    • CropImageView
      • ba As BA
      • AddToParent (Parent As ViewGroup, left As Int, top As Int, width As Int, height As Int)
      • BringToFront
      • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
      • Initialize (EventName As String)
      • Invalidate
      • Invalidate2 (arg0 As Rect)
      • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • IsInitialized As Boolean
      • RemoveView
      • RequestFocus As Boolean
      • SendToBack
      • SetBackgroundImage (arg0 As Bitmap)
      • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
      • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
      • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
      • rotateImage (degrees As Int)
        Rotates image by the specified number of degrees clockwise. Cycles from 0 to 360
      • setAspectRatio (aspectRatioX As Int, aspectRatioY As Int)
        Sets the both the X and Y values of the aspectRatio.
      • ActualCropRect As RectF [read only]
        Gets the crop window's position relative to the source Bitmap (not the image
        displayed in the CropImageView).
      • Background As Drawable
      • Color As Int [write only]
      • CroppedImage As Bitmap [read only]
        Gets the cropped image based on the current crop window.
      • Enabled As Boolean
      • FixedAspectRatio As Boolean [write only]
        Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while
        false allows it to be changed.
      • Guidelines As Int [write only]
        Sets the guidelines for the CropOverlayView to be either on, off, or to show when
        resizing the application.
      • Height As Int
      • ImageBitmap As Bitmap [write only]
        Sets a Bitmap as the content of the CropImageView.
      • Left As Int
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int

    This library is Donationware. You can download the library, you can test the library. But if you want to USE the library in your App you need to Donate for it.
    Please click here to donate (You can donate any amount you want to donate for the library (or my work) :)

    Attached Files:

    Last edited: Mar 26, 2015
  2. NJDude

    NJDude Expert Licensed User


    I'm attaching a slightly better example, which allows you to SAVE the cropped picture.

    Attached Files:

  3. DonManfred

    DonManfred Expert Licensed User

    But no like? :(
    You have forgotten to include the Resource-folder :)
    Thank you for this Example!

  4. NJDude

    NJDude Expert Licensed User

    Oops!, ok, I have added the missing directory and reposted the sample on my previous post.
  5. Douglas Farias

    Douglas Farias Expert Licensed User

    this works perfect, i go make u a donation when i launch my app thx
    DonManfred likes this.
  6. shashkiranr

    shashkiranr Active Member Licensed User

    Hi DonManfred,

    Thank you for the library. I have an image that is 1364 X 2048 size. I want to fix the crop window only to my device height and size. How do you do that?

    Phayao likes this.
  7. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    Hello I'm trying to use CroppedImage method combined with StringUtils.EncodeBase64, but it's resulting in a not valid Base64 string.

    Do you have any idea what is happening?

  8. DonManfred

    DonManfred Expert Licensed User

    No. I dont remember having a base64 routine inside this lib.... So; without seeing your code it is hard to give advices.
  9. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    You're right Manfred. I don't use any code from this library to encode the image into a Base64 format.

    I don't have the code right now, but I do something like this:

    Dim su As StringUtils
    Dim str As String = su.EncodeBase64(img.CroppedImage)
    img is declared as CropImageView.

    The issue here is, when I persist my str and try to decode them into a ImageView I got an exception. Even trying to convert using an online Base64 parser, I got the same message, that the String is a invalid Base64 string.

    I'm trying to figure out if the problem is related with the way CroppedImage handle to create the new Bitmap. Make sense?
  10. DonManfred

    DonManfred Expert Licensed User

    It will not work the way you do
    will return a BITMAP, not a byte array

    You first need to convert the bitmap into a byte array

    Dim out As OutputStream
    dim bmp as Bitmap = img.CroppedImage
    Dim data() As Byte = out.ToBytesArray
    and then

    Dim su As StringUtils
    Dim str As String = su.EncodeBase64(data)
    something like that... Have not tested it
  11. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    Cool! I'll test it in the next hours!!!

    By the way, are you planning to create this library for B4I?
  12. MaFu

    MaFu Well-Known Member Licensed User

    Or you can use the BitmatToString function of my MFLib which converts a bitmap to Base64.
    DonManfred likes this.
  13. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    Hey Manfred,

    no luck at all.

    Here is my code:

    Sub btnChoosePhoto_Click
    Dim Chooser As ContentChooser
    "image/*""Selecione uma nova foto para o aluno")
    End Sub

    Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
            imageToCrop.ImageBitmap = 
    ToastMessageShow("Nenhuma foto selecionada"True)
    End If
    End Sub
    Dim u As Utils
    Dim bmp as Bitmap = imageToCrop.CroppedImage
    Dim photo as String= u.ImageToBase64(bmp)
    Public Sub ImageToBase64(img As BitmapAs String
    Dim su As StringUtils
    Dim out As OutputStream
    Return su.EncodeBase64(out.ToBytesArray)
    End Sub
    When I paste the result of photo in a Base64 decoder like http://base64online.org/decode/#, I got the message It seems like image file is broken.

    I also observed that photo variable has some blank spaces into that. This looks to be an issue when I try to decode Base64 format.
    Last edited: Jul 28, 2015
  14. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    I also tried you library with the same result I got before. Any ideas?

    Dim mf As MF_Image
        photo = mf.BitmapToString(bmp, 
  15. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    Folks I found my mistake. After encode the bitmap I need to submit the image to an REST API. I solved this issue doing a EncodeURL =D

    Works like a charm now
    DonManfred likes this.
  16. DonManfred

    DonManfred Expert Licensed User

    I´m using exact the code i wrote above....

    Sub Button1_Click
    Dim bmp As Bitmap = civ.CroppedImage
    Dim out As OutputStream
    Dim data() As Byte = out.ToBytesArray
        ImageView1.Bitmap = bmp
    Dim su As StringUtils
    Dim str As String = su.EncodeBase64(data)
    End Sub
  17. DonManfred

    DonManfred Expert Licensed User

    glad you got it working
  18. Gorazdspajzer

    Gorazdspajzer Member Licensed User

    Great Lib!
    One question. Why I cant access all parts of the image (there is some margin and then inside is crop panel). Because maybe I want to crop part of the image that is in some corner.

  19. DonManfred

    DonManfred Expert Licensed User

    The underlying library does not have such feature. And i dont know what i need to change to makeit work, sorry.
  20. Dogbonesix

    Dogbonesix Active Member Licensed User

    Exactly what I needed Except: I can not reload another image without exiting the app and reloading - don't know why. I would like to browse several pictures and crop them in the same folder. Very difficult when I have start the app for every image to crop.
    Also, some images won't display but will still crop - memory?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice