Dart library with stack blur algorithm for RGBA images
stack_blur
Applies the stack blur to
a buffer with RGBA pixels.
The Stack blur algorithm works fast and looks good. It is a compromise between
Gaussian blur
and Box blur.
Use with image library
import 'dart:io';
import 'package:image/image.dart';
import 'package:stack_blur/stack_blur.dart';
void main() {
// loading image from file
final image = decodeImage(File('source.png').readAsBytesSync())!;
// blurring image pixels with blur radius 42
stackBlurRgba(image.data, image.width, image.height, 42);
// saving image to file
File('blurred.png').writeAsBytesSync(encodePng(image));
}
Use with Flutter and bitmap library
Flutter images have the same RGBA pixel buffer. You can get it in a rather non-obvious
way through ImageStreamListener
.
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:bitmap/bitmap.dart';
Future<Image> blurAsset(String assetName) async {
ImageProvider provider = ExactAssetImage(assetName);
// Rain dance to get RGBA pixels from image
final ImageStream stream = provider.resolve(ImageConfiguration.empty);
final completer = Completer<ui.Image>();
late ImageStreamListener listener;
listener = ImageStreamListener((frame, _) {
stream.removeListener(listener);
completer.complete(frame.image);
});
stream.addListener(listener);
ui.Image image = await completer.future;
ByteData rgbaData = (await image.toByteData(format: ui.ImageByteFormat.rawRgba))!;
// This is the pixels we need
Uint32List rgbaPixels = rgbaData.buffer.asUint32List();
// We can blur the image buffer
stackBlurRgba(rgbaPixels, image.width, image.height, 42);
// We need a third-party 'bitmap' library to turn the buffer into a widget
final bitmap = Bitmap.fromHeadless(
image.width, image.height,
rgbaData.buffer.asUint8List());
return Image.memory(bitmap.buildHeaded());
}