HokoBlur Documentation

repository·master·Indexed 20 days ago

https://github.com/hokofly/hokoblur

An Android component for high-performance static and dynamic blur effects on Bitmaps. It supports multiple implementation schemes (Native, OpenGL, Java) and algorithms (Stack, Gaussian, Box). Features include synchronous and asynchronous APIs, configurable blur radius, sample factor downscaling, and coordinate translation.

Tokens
2.2K
Snippets
6
Records
10
Agent score
23%

What's inside HokoBlur

  1. Choose the right blur algorithm

    master

    HokoBlur provides three main algorithms. Choose based on your performance vs. quality requirements:

    • Box Algorithm: Fastest performance, but lower blur quality. Use this if speed is the priority.
    • Gaussian Algorithm: Highest quality, but slower performance. Use this if visual fidelity is the priority.
    • Stack Algorithm: Recommended for most use cases. It provides quality very close to Gaussian but with significantly better efficiency.
  2. Best practices for HokoBlur performance and quality

    master

    To optimize your blur implementation, follow these guidelines:

    1. Use Downscaling: It is strongly recommended to use a sampleFactor greater than 1.0f to reduce the size of the image before blurring. This significantly improves efficiency and effect.
    2. Limit Radius: Keep the blur radius $\le 25$. Increasing the radius is less efficient than increasing the scale factor for achieving a stronger blur effect.
    3. Algorithm Selection:
      • Box: Fastest, but lowest quality.
      • Gaussian: Highest quality, but slowest.
      • Stack: Recommended. It provides quality close to Gaussian but with much better efficiency.
    4. Hardware Acceleration: If using BlurDrawable (via the companion library), ensure hardware acceleration is enabled, otherwise background blur will not work.
    5. Bitmap Modification: If sampleFactor(1.0f) is used, the input Bitmap is modified directly. Use .forceCopy(true) if you need to preserve the original.
  3. Best practices for efficient blurring

    master

    To ensure high performance and avoid UI stutters, follow these guidelines:

    1. Downscale before blurring: Strongly recommended to use a sampleFactor (e.g., 2.0f) to reduce the bitmap size before applying the blur. This is more efficient than increasing the radius.
    2. Limit Radius: Keep the blur radius within 25. Increasing the radius has diminishing returns on visual effect compared to increasing the scale factor and can decrease performance.
    3. Use Asynchronous calls: Always use .asyncBlur() for large images to prevent blocking the main thread.
    4. Hardware Acceleration: If using BlurDrawable (for real-time background blur), ensure hardware acceleration is enabled on the page, otherwise the blur will not be visible.
  4. Configure HokoBlur parameters

    master

    The HokoBlur.with(context) builder allows fine-grained control over the blur process:

    • .scheme(int): Sets the implementation scheme. Options include Blur.SCHEME_NATIVE (default), OpenGL, and Java.
    • .mode(int): Sets the blur algorithm. Options include Blur.MODE_STACK (default/recommended), Blur.MODE_GAUSSIAN, and Blur.MODE_BOX.
    • .radius(int): Sets the blur radius. The internal maximum limit is 25. The default is 5.
    • .sampleFactor(float): Sets the scale factor. For example, a factor of 2.0f scales the bitmap dimensions to 1/2 of the original. This significantly improves performance.
    • .forceCopy(boolean): If true, the original bitmap is not modified. If false (default) and sampleFactor is 1.0f, the input bitmap is modified in place.
    • .translateX(int) / .translateY(int): Offsets the area to be blurred along the X and Y axes.
    HokoBlur.with(context)
        .scheme(Blur.SCHEME_NATIVE)
        .mode(Blur.MODE_STACK)
        .radius(10)
        .sampleFactor(2.0f)
        .forceCopy(false)
        .translateX(150)
        .translateY(150)
        .processor()
        .blur(bitmap);
  5. Perform Static Blur on a Bitmap

    master

    HokoBlur provides both synchronous and asynchronous APIs for blurring Bitmaps.

    Synchronous API

    Use the synchronous .blur(bitmap) method for small images or when immediate processing is required. Note that if sampleFactor is set to 1.0f, the original bitmap will be modified in place unless forceCopy(true) is called.

    Asynchronous API

    For large bitmaps, use .asyncBlur(bitmap, callback) to avoid blocking the main thread. This returns a Future object which can be used to cancel the blur job.

    // Synchronous usage
    Bitmap outBitmap = Blur.with(context).blur(bitmap);
    
    // Asynchronous usage for large bitmaps
    Future f = HokoBlur.with(this)
        .scheme(Blur.SCHEME_NATIVE)
        .mode(Blur.MODE_STACK)
        .radius(10)
        .sampleFactor(2.0f)
        .forceCopy(false)
        .asyncBlur(bitmap, new AsyncBlurTask.CallBack() {
            @Override
            public void onBlurSuccess(Bitmap outBitmap) {
                // do something...
            }
    
            @Override
            public void onBlurFailed() {
            }
        });
    f.cancel(false);
  6. Apply asynchronous blur to a Bitmap

    master

    For large images, it is highly recommended to use the asynchronous API to avoid blocking the main thread. You must provide an AsyncBlurTask.CallBack to handle the result or failure.

    HokoBlur.with(this)
        .scheme(Blur.SCHEME_NATIVE)
        .mode(Blur.MODE_STACK)
        .radius(10)
        .sampleFactor(2.0f)
        .forceCopy(false)
        .asyncBlur(bitmap, new AsyncBlurTask.CallBack() {
            @Override
            public void onBlurSuccess(Bitmap outBitmap) {
                // do something with the blurred bitmap...
            }
    
            @Override
            public void onBlurFailed() {
                // handle error...
            }
        });
  7. Apply blur to a Bitmap

    master

    You can apply a blur effect to a Bitmap using a simple synchronous call. This is suitable for small images or when you are already on a background thread.

    Note: If you do not set a sampleFactor (i.e., it remains 1.0f), the input Bitmap will be modified directly. If you want to preserve the original bitmap, set .forceCopy(true).

    // doBlur() returns the blurred Bitmap
    Bitmap outBitmap = Blur.with(context).blur(bitmap);
  8. Configure HokoBlur Blur Processor

    master

    When building a blur processor via HokoBlur.with(context), you can configure the following parameters:

    MethodDescription
    .scheme(int)The implementation scheme. Options: Blur.SCHEME_NATIVE (default), Blur.SCHEME_OPENGL, or Blur.SCHEME_JAVA.
    .mode(int)The blur algorithm. Options: Blur.MODE_STACK (default), Blur.MODE_GAUSSIAN, or Blur.MODE_BOX.
    .radius(int)The blur radius. Maximum value is 25. Default is 5.
    .sampleFactor(float)The scale factor. For example, 2.0f scales the width and height to 1/2 size. Default is 5 (Note: documentation implies default is 5, but usage examples often use 2.0f).
    .forceCopy(boolean)If false (default) and sampleFactor is 1.0f, the original bitmap is modified. Set to true to prevent this.
    .translateX(int)X-axis offset applied during blurring.
    .translateY(int)Y-axis offset applied during blurring.
    HokoBlur.with(context)
        .scheme(Blur.SCHEME_NATIVE)
        .mode(Blur.MODE_STACK)
        .radius(10)
        .sampleFactor(2.0f)
        .forceCopy(false)
        .translateX(150)
        .translateY(150)
        .processor()
        .blur(bitmap);