将颜色过滤后的图像保存到磁盘

spp*_*c42 6 flutter

在 Flutter 中,如果我们使用ColorFilterwidget,它需要 aColorFilter.matrix和 an Image,并在其上应用ColorFilter.matrix.

const ColorFilter sepia = ColorFilter.matrix(<double>[
  0.393, 0.769, 0.189, 0, 0,
  0.349, 0.686, 0.168, 0, 0,
  0.272, 0.534, 0.131, 0, 0,
  0,     0,     0,     1, 0,
]);

Container _buildFilterThumbnail(int index, Size size) {
final Image image = Image.file(
  widget.imageFile,
  width: size.width,
  fit: BoxFit.cover,
);

return Container(
  padding: const EdgeInsets.all(4.0),
  decoration: BoxDecoration(
    border: Border.all(color: _selectedIndex == index ? Colors.blue : Theme.of(context).primaryColor, width: 4.0),
  ),
  child: ColorFiltered(
    colorFilter: ColorFilter.matrix(filters[index].matrixValues),
    child: Container(
      height: 80,
      width: 80,
      child: image,
    ),
  ),
);
}
Run Code Online (Sandbox Code Playgroud)

我们如何获取底层图像(以像素/字节为单位),以便将其保存在磁盘上。我不想保存ColorFiltered屏幕上的渲染区域。

目前我被迫使用photofilterspub.dev 的库,该库进行像素操作以应用自定义过滤器。然而,它的效率不是很高,并且本质上对每个缩略图都应用像素级操作,这使得速度非常慢。另一方面,ColorFilteredwidget 的速度快如闪电!

以下是photofilters图书馆的内部运作

int clampPixel(int x) => x.clamp(0, 255);

// ColorOverlay - add a slight color overlay.
void colorOverlay(Uint8List bytes, num red, num green, num blue, num scale) {
  for (int i = 0; i < bytes.length; i += 4) {
    bytes[i] = clampPixel((bytes[i] - (bytes[i] - red) * scale).round());
    bytes[i + 1] =
        clampPixel((bytes[i + 1] - (bytes[i + 1] - green) * scale).round());
    bytes[i + 2] =
        clampPixel((bytes[i + 2] - (bytes[i + 2] - blue) * scale).round());
  }
}

// RGB Scale
void rgbScale(Uint8List bytes, num red, num green, num blue) {
  for (int i = 0; i < bytes.length; i += 4) {
    bytes[i] = clampPixel((bytes[i] * red).round());
    bytes[i + 1] = clampPixel((bytes[i + 1] * green).round());
    bytes[i + 2] = clampPixel((bytes[i + 2] * blue).round());
  }
}
Run Code Online (Sandbox Code Playgroud)

任何指示表示赞赏。