B4J Library [B4X] [XUI] xResizeAndCrop

Discussion in 'B4J Libraries & Classes' started by klaus, Dec 5, 2018.

  1. klaus

    klaus Expert Licensed User

    New
    The xResizeAndCrop Class does now also exist as a B4X libary.
    The xResizeAndCrop.b4xlib and the xResizeAndCrop.xml files are attached.
    You need to copy the xResizeAndCrop.b4xlib file to the AdditionlLibraries folder for eace product!
    Don’t copy thexResizeAndCrop.xml file to the AdditionalLibraries folder, copy it in another folder for all b4xlib xml files.
    The xResizeAndCrop.xml file is for help purposes and is useful with the B4X Help Viewer or the B4XObjectBrowser.
    The xResizeAndCrop.xml was generated with this tool: b4xlib - XML generation

    This is a crossplatform CustomView.
    It allows to resize and crop images.
    It works on all three products: B4A, B4i and B4J.

    It is an evolution of the B4A project: Resize and crop image.
    To make a B4X CustomView was inspired by this thread: [B4X] [XUI] CropView.

    Demoprograms for all three products.
    Tested on PC, Android Samsung S6, iPhone 6.

    EDIT: 2019.01.30 Version 1.1
    Added a Square property.
    When Square = True then the resized image is always square.

    upload_2018-12-5_19-49-13.gif

    xResizeAndCrop

    Author: Klaus CHRISTL (klaus)
    Version: 1.1
    • xResizeAndCrop
      • Events:
        • CropFinished
      • Functions:
        • Initialize (Callback As Object, EventName As String) As String
        • IsInitialized As Boolean
          Tests whether the object has been initialized.
        • LoadImage (Dir As String, FileName As String) As String
          loads the given image file to the xResizeAndCrop Customview
      • Properties:
        • CroppedImage As B4XBitmap [read only]
          gets the cropped image as a B4XBitmap, read only
        • CroppedView
          sets the Panel / Pane view for the cropped image, write only
          the Panel / Pane view must be added in the main code and transmitted to the custom view as this property.
        • HandleColor As Int
          sets or gets the HandleColor property
          value must a xui color
          Example code: <code>xResizeAndCrop1.HandleColor = xui.Color_RGB(255, 215, 0)</code>
        • Height As Int
          sets or gets the Height property
        • Image As B4XBitmap
          sets the image (B4XBitmap) to the Customview, write only
        • Left As Int
          sets or gets the Left property
        • MinCroppedHeight As Int
          sets or gets the MinHeight property
          value in pixels, no dip value
          if Square = True, the min cropped width is set equal to this height value
        • MinCroppedWidth As Int
          sets or gets the MinWidth property
          value in pixels, no dip value
          if Square = True, the min cropped height is set equal to this width value
        • Square As Boolean
          sets or gets the Square property
          Square = True means that the resized image is always square
        • Top As Int
          sets or gets the Top property
        • Visible As Boolean
          sets or gets the Visible property
        • Width As Int
          sets or gets the Width property
     

    Attached Files:

    Last edited: Jan 30, 2019
    GMan, amaxco, Tayfur and 13 others like this.
  2. klaus

    klaus Expert Licensed User

    The xResizeAndCrop class is now also available as a B4XLibrary, files in the first post.
     
    Erel and DonManfred like this.
  3. klaus

    klaus Expert Licensed User

    The xResizeAndCrop class and B4X Library have been updated to version 1.1.
    Added a Square property.
    When Square = True then the resized image is always square.
    Files updated in the first post.
     
    Almora, Erel, DonManfred and 2 others like this.
  4. viriato

    viriato Member Licensed User

    Hi,
    Great example ..
    But for a better alignment I would like to rotate the picture or the square.
    Any idea how to do that ?
    Thank you
     
  5. klaus

    klaus Expert Licensed User

    Sorry, but I don't understand what exactly you want to do.
    What do you want to rotate?
    The reference image or the croped image?
    What do you mean with: But for a better alignment... ?
     
  6. viriato

    viriato Member Licensed User

    The reference image should work for my need
     
  7. viriato

    viriato Member Licensed User

    For example if you take a picture of a business card most of the time the picture is not squared
    so the fact to rotate the picture help to have a squared cropped image

    May be obtain the same result rotating the square will be also a solution.

    merci
     
  8. klaus

    klaus Expert Licensed User

    You can turn the device to better fit a rectangular image.
    The program calculates the display of the origin image to occupy the max possible space.
    If you have an image in portrait hold the device in portrait, if you have an image in landscape hold the device in landscape.
    I suspect a problem with rotating the image that the cropped image needs a again a rotation.

    upload_2019-1-31_19-15-18.png
    upload_2019-1-31_19-15-58.png
     
  9. viriato

    viriato Member Licensed User

    I have seen this possibility in some APP and it is really practical and convenient

    Thanks
     
  10. klaus

    klaus Expert Licensed User

    Sorry, but I still don't see any advantage.
     
  11. viriato

    viriato Member Licensed User

    HI,
    I have another question, the pictures captured have a size of 3 M (Fidelity cards like Avis ; Hertz ..)
    I would like to save the cropped picture with a minimum size (max 200 k)
    How can i modify your program to do that ?

    Thank you
     
  12. klaus

    klaus Expert Licensed User

    Two solutions:
    1: Play with the quality parameter WriteToStream(Out, 100, "JPEG":
    Code:
    Private Sub btnSaveCroppedImage_Click
        
    Private Dir, FileName As String
       
        xui.SetDataFolder(
    "xResizeAndCropDemo")
        Dir = xui.DefaultFolder
        
    Log(Dir)
        FileName = 
    "image.jpg"
        
    Private Out As OutputStream
        Out = 
    File.OpenOutput(Dir, FileName, False)
        xResizeAndCrop1.CroppedImage.WriteToStream(Out, 
    100"JPEG")
        Out.Close
    End Sub
    2: Save the croped image in a temporary file and reload it resized and delete the temporary file.
    Resize with: LoadBitmapResize(Dir, "temp.jpg", 100, 100, True).
    Code:
    Private Sub btnSaveCroppedImage_Click
        
    Private Dir, FileName As String
       
        xui.SetDataFolder(
    "xResizeAndCropDemo")
        Dir = xui.DefaultFolder
        
    Log(Dir)
        FileName = 
    "image.jpg"
        
    Private Out As OutputStream
       
        Out = 
    File.OpenOutput(Dir, "temp.jpg"False)
        xResizeAndCrop1.CroppedImage.WriteToStream(Out, 
    100"JPEG")
        Out.Close
        
    Private bmpTemp As B4XBitmap
        bmpTemp = xui.LoadBitmapResize(Dir, 
    "temp.jpg"100100True)
        
    Private Out As OutputStream
        Out = 
    File.OpenOutput(Dir, FileName, False)
        bmpTemp.WriteToStream(Out, 
    100"JPEG")
        Out.Close
        
    File.Delete(Dir, "temp.jpg")
    End Sub
     
Loading...
  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