B4J Library [B4X] [XUI] BitmapCreator - Pixels, Drawings and More

Discussion in 'B4J Libraries & Classes' started by Erel, Apr 18, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    BitmapCreator is a cross platform class.
    Its core is made of:

    1. A bytes array that represents an image.
    2. Platform specific code that efficiently creates a regular bitmap from the bytes array data (Bitmap property).
    3. Platform specific code that efficiently extracts the pixels data from a bitmap and copies them to the bytes array (CopyPixelsFromBitmap).

    Reading and writing to an array of bytes are very quick operations. This allows low level access that was previously difficult to implement and more or less impossible to implement with cross platform code.

    Video example:



    [​IMG]



    Additional methods in BitmapCreator:

    - Methods to get or set a pixel color in various formats.
    - Methods to copy a pixel from a different BitmapCreator to this BitmapCreator.
    - Methods to draw a bitmap or a different BitmapCreator to this BitmapCreator.
    - Method to draw gradients.
    - And a few other methods.


    Scaling

    1. The bitmap created with the Bitmap property has a scale of 1. This means that on Android the OS will resize the bitmap automatically based on the device scale.
    Don't use 'dip' units with BitmapCreator.
    Note that in the example class the "missing dip" warning is disabled with:
    Code:
    #IgnoreWarnings: 6
    2. Depending on your use case it might be better from performance perspective to create a smaller BitmapCreator and resize the output Bitmap with Bitmap.Resize.

    Blending

    When a non-opaque pixel (source) is set, the target pixel color should be combined with the source color for the transparency to have its expected effect. This is called blending.
    When you call DrawBitmap or DrawBitmapCreator you can choose whether to skip blending or not. It is significantly faster to skip blending, though drawing with blending will be fast enough in most cases.

    There is also a BlendPixel method that you can use to blend a single pixel.

    BitmapCreator vs. Canvas / B4XCanvas

    Use them together. BitmapCreator returns a regular bitmap and Canvas can be used to make a drawing that is then copied or drawn over a BitmapCreator.

    Debugging

    Many of the methods that you will use with BitmapCreator are computational intensive. The debugger might be too slow, this is especially true if the code is in a resumable sub as the debugger cannot optimize such subs.
    As BitmapCreator is cross platform you can write the code in B4J and only test it on the other platforms.
    The example project features are implemented in a class named BitmapCreatorExamples. The three projects all link to the exact same class.

    Dependencies:

    B4A / B4J - XUI, JavaObject and ByteConverter (https://www.b4x.com/android/forum/threads/6787/#content)
    B4i - iXUI, iRandomAccessFile

    BitmapCreator v3.01 (beta) is included in the attached projects.
    Note that BitmapCreator and BitmapCreatorExamples classes are identical in all three projects.
    Images source: https://www.pexels.com/

    Updates

    Starting from B4J v6.30, B4i v5.0 and B4A v8.3, BitmapCreator is included as an internal library. The source code is attached separately. Note that it will not work with older versions of B4J, B4i and B4A.
    Latest version is 3.20.

    BitmapCreatorEffects class: https://www.b4x.com/android/forum/threads/b4x-bitmapcreator-effects.93608/
     

    Attached Files:

    Last edited: May 30, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Amazing @Erel, thank you my brother.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Johan Hormaza

    Johan Hormaza Active Member Licensed User

  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Erel

    Erel Administrator Staff Member Licensed User

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