【问题标题】:How to get dominant color from image in flutter?如何从颤动的图像中获得主色?
【发布时间】:2020-10-24 08:21:52
【问题描述】:

我想从图像中提取主色,以便我可以将其作为混合应用到其他图像中。我怎样才能做到这一点??

在我当前的代码中,我手动指定了颜色,但我希望它由应用程序生成。

class MyApp extends StatelessWidget {

  Color face = new HexColor("a8a8a8");
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Image from assets"),
        ),
        body: Column (
            mainAxisAlignment: MainAxisAlignment.center,
              children:<Widget>[
                Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children:<Widget>[
                      new Image.asset('assets/images/6.jpg',
                        color: face, colorBlendMode:BlendMode.modulate ,
                        fit:BoxFit.cover,
                        height: 50,
                        width: 50,
                      ),


                new Image.asset('assets/images/1.jpg',
              color: face, colorBlendMode: BlendMode.modulate,
            fit:BoxFit.cover,
                  height: 200,
                  width: 200,
          ),
                    ]),
                ])),
    );
  }
}


【问题讨论】:

    标签: image flutter dart colors color-palette


    【解决方案1】:

    这里有 palette_generator 库,即使你在 youtube 或其他地方搜索,你也可以找到一些 tutorials 来告诉你哪些结果。

    https://pub.dev/packages/palette_generator

    【讨论】:

    • 它有错误的文档。我读过但没看懂
    【解决方案2】:

    我找到了使用调色板生成器的解决方案.. 第一个导入库

    import 'package:palette_generator/palette_generator.dart';
    

    也将其添加到 pubspec.yaml 文件中

    下面的函数将返回调色板

    Future<PaletteGenerator>_updatePaletteGenerator ()async
    {
      paletteGenerator = await PaletteGenerator.fromImageProvider(
        Image.asset("assets/images/8.jfif").image,
      );
    return paletteGenerator;
    }
    

    现在我们可以在未来的构建器中获取它

      FutureBuilder<PaletteGenerator>(
                      future: _updatePaletteGenerator(), // async work
                      builder: (BuildContext context, AsyncSnapshot<PaletteGenerator> snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.waiting: return Center(child:CircularProgressIndicator());
                          default:
                            if (snapshot.hasError)
                              return new Text('Error: ${snapshot.error}');
                            else {
                             // Color color=new Color(snapshot.data.dominantColor.color);
                              face=snapshot.data.dominantColor.color;
                               return new Text('color: ${face.toString()}');
                                  }}})
    

    这就是我们可以轻松获取主色的方法

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2020-02-03
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多