将 ColorFiltered 图像保存到磁盘

问题描述 投票:0回答:1

在 Flutter 中,如果我们使用

ColorFilter
小部件,它需要一个
ColorFilter.matrix
和一个
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,
    ),
  ),
);
}

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

ColorFiltered
区域。

目前被迫使用 pub.dev 的

photofilters
库,该库进行像素操作以应用自定义滤镜。然而,它的效率不是很高,并且本质上对每个缩略图都应用像素级操作,这使得速度非常慢。另一方面,
ColorFiltered
小部件速度快如闪电!

以下是

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());
  }
}

任何指点表示赞赏。

flutter
1个回答
0
投票

您找到解决这个问题的方法了吗?我也面临同样的情况

© www.soinside.com 2019 - 2024. All rights reserved.