Flutter ASCII艺术生成插件enough_ascii_art的使用

发布于 1周前 作者 gougou168 来自 Flutter

Flutter ASCII艺术生成插件enough_ascii_art的使用

enough_ascii_art 是一个专为Dart开发者设计的ASCII艺术库,可用于将图像转换为文本表示、创建FIGlet风格的文本横幅、将ASCII文本转换为UTF-8 "字体"等。它遵循MPL Mozilla License 2.0协议。

使用方法

功能介绍

  • 图像转ASCII:使用 convertImage(..) 方法将图像转换为ASCII字符。
  • FIGlet文本横幅:使用 renderFiglet(..) 方法创建FIGlet风格的文本横幅。
  • ASCII到UTF-8字体:使用 renderUnicode(..) 方法将ASCII文本转换为UTF-8字体(如双线体)。
  • 表情符号转换:使用 convertEmoticons(..) 方法将常见的UTF-8表情符号转换为其文本表示形式。

示例代码

以下是一个完整的示例代码,展示了如何使用 enough_ascii_art 插件:

import 'dart:io';
import 'package:enough_ascii_art/enough_ascii_art.dart' as art;
import 'package:image/image.dart' as img;

void main() async {
  // 读取并解码图像文件
  final bytes = await File('./example/enough.jpg').readAsBytes();
  final image = img.decodeImage(bytes)!;
  
  // 将图像转换为ASCII艺术
  var asciiImage = art.convertImage(image, maxWidth: 40, invert: true);
  print('');
  print(asciiImage);

  // 将包含表情符号的字符串转换为文本表情符号
  var helloWithUtf8Smileys = 'hello world 😛';
  var helloWithTextSmileys =
      art.convertEmoticons(helloWithUtf8Smileys, art.EmoticonStyle.western);
  print('');
  print(helloWithTextSmileys);
  print('');

  // 创建FIGlet风格的文本横幅
  print('cosmic:');
  var fontText = await File('./example/cosmic.flf').readAsString();
  var figure = art.renderFiglet('ENOUGH', art.Font.text(fontText));
  print(figure);
  print('');

  // 将ASCII文本转换为UTF-8字体(双线体)
  var unicode = art.renderUnicode('hello world', art.UnicodeFont.doublestruck);
  print('double struck:');
  print(unicode);
}

输出结果

上述代码将生成如下输出:

%%%%%%%%%%%%%%%%%%@%@%#%#%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%##      -@%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%.        .#%%%%%%%%%%%
%%%%%%%%%%%%%%%%%#          #%%%%%%%%%%%
%%%%%%%%%%%%%%%%#@           %%%%%%%%%%%
%%%%%%%%%%%%% #*  #      @   %%%%%%%%%%%
%%%%%%%%%%%%%%-    *   =%    #%%%%%%%%%%
%%%%%%%%%%%%%%@#      *%     .#%%%%%%%%%
%%%%%%%%%%%%%%%%-    .@%%       %%%%%%%%
%%%%%%%%%%%%%%%=    @%%@          #%%%%%
%%%%%%%%%%%%%%#     #%%=            ##%%
%%%%%%%%%%%%%%%.      .               #%
%%%%%%%%%%%%%%%%@                      *
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%                 #
%%%%%%%%%%%%%%%%%%%%%%-              +%%
%%%%%%%%%%%%%%%%%%%%%%%.             %%%
%%%%%%%%%%%%%%%%%%%%%%%.           :%%%%
%%%%%%%%%%%%%%%%%%%%%%.             +@%%
%%%%%%%%%%%%%%%%%%%%%%    +%%%%%@    @%%
%%%%%%%%%%%%%%%%%%%%%.    @%%%%%#     %%
%%%%%%%%%%%%%%%%%%%%%     #%%%%%@     %%
%%%%%%%%%%%%%%%%%%%%%%-  -%%%%%%      %%


hello world :-P

cosmic:
.,:::::::::.    :::.    ...      ...    :::  .,-:::::/    ::   .:
;;;;''''`;;;;,  `;;; .;;;;;;;.   ;;     ;;;,;;-'````'    ,;;   ;;,
 [[cccc   [[[[[. '[[,[[     \[[,[['     [[[[[[   [[[[[[/,[[[,,,[[[
 $$""""   $$$ "Y$c$$$$$,     $$$$$      $$$"$$c.    "$$ "$$$"""$$$
 888oo,__ 888    Y88"888,_ _,88P88    .d888 `Y8bo,,,o88o 888   "88o
 """"YUMMMMMM     YM  "YMMMMMP"  "YmmMMMM""   `'YMUP"YMM MMM    YMM

double struck:
 heltto wrold

安装

pubspec.yaml 文件中添加依赖:

dependencies:
  enough_ascii_art: ^1.0.0

确保使用最新版本的 enough_ascii_art,可以通过 Pub 获取最新版本信息。

运行示例

从项目根目录运行示例代码:

dart ./example/enough_ascii_art_example.dart

已知限制

  • 图像总是被转换为灰度图。
  • FIGlet横幅目前仅支持从左到右的文字方向。
  • FIGlet横幅不会自动换行。
  • 需要知道具体的FIGlet字体名称(可以在 figlet/fonts 目录中查找)。
  • 目前只支持西方风格的表情符号。

如果您有兴趣帮助改进这些限制,请访问 GitHub issue tracker 提交功能请求或报告问题。

更多示例

以下是更多FIGlet字体的示例:

shadow:
 ____|  \  |  _ \  |   |  ___| |   |
 __|     \ | |   | |   | |     |   |
 |     |\  | |   | |   | |   | ___ |
_____|_| \_|\___/ \___/ \____|_|  _|



smslant:
   _____  ______  __  _________ __
  / __/ |/ / __ \/ / / / ___/ // /
 / _//    / /_/ / /_/ / (_ / _  /
/___/_/|_/\____/\____/\___/_//_/



eftifont:
 ___  _  _  _  _ _  __  _ _
| __|| \| |/ \| | |/ _|| U |
| _| | \\ ( o ) U ( |_n|   |
|___||_|\_|\_/|___|\__/|_n_|



big:
 ______ _   _  ____  _    _  _____ _    _
|  ____| \ | |/ __ \| |  | |/ ____| |  | |
| |__  |  \| | |  | | |  | | |  __| |__| |
|  __| | . ` | |  | | |  | | | |_ |  __  |
| |____| |\  | |__| | |__| | |__| | |  | |
|______|_| \_|\____/ \____/ \_____|_|  |_|




isometric1:
      ___           ___           ___           ___           ___           ___
     /\  \         /\__\         /\  \         /\__\         /\  \         /\__\
    /::\  \       /::|  |       /::\  \       /:/  /        /::\  \       /:/  /
   /:/\:\  \     /:|:|  |      /:/\:\  \     /:/  /        /:/\:\  \     /:/__/
  /::\~\:\  \   /:/|:|  |__   /:/  \:\  \   /:/  /  ___   /:/  \:\  \   /::\  \ ___
 /:/\:\ \:\__\ /:/ |:| /\__\ /:/__/ \:\__\ /:/__/  /\__\ /:/__/_\:\__\ /:/\:\  /\__\
 \:\~\:\ \/__/ \/__|:|/:/  / \:\  \ /:/  / \:\  \ /:/  / \:\  /\ \/__/ \/__\:\/:/  /
  \:\ \:\__\       |:/:/  /   \:\  /:/  /   \:\  /:/  /   \:\ \:\__\        \::/  /
   \:\ \/__/       |::/  /     \:\/:/  /     \:\/:/  /     \:\/:/  /        /:/  /
    \:\__\         /:/  /       \::/  /       \::/  /       \::/  /        /:/  /
     \/__/         \/__/         \/__/         \/__/         \/__/         \/__/


chunky:
 _______ _______ _______ _______ _______ _______
|    ___|    |  |       |   |   |     __|   |   |
|    ___|       |   -   |   |   |    |  |       |
|_______|__|____|_______|_______|_______|___|___|

希望这个插件能为您的项目带来更多的创意和乐趣!如果有任何问题或建议,请随时联系开发者社区。


更多关于Flutter ASCII艺术生成插件enough_ascii_art的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter ASCII艺术生成插件enough_ascii_art的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用enough_ascii_art插件来生成ASCII艺术的代码案例。enough_ascii_art是一个Flutter插件,它可以将文本或图像转换为ASCII艺术。

首先,你需要在你的pubspec.yaml文件中添加依赖项:

dependencies:
  flutter:
    sdk: flutter
  enough_ascii_art: ^最新版本号  # 请替换为实际最新版本号

然后运行flutter pub get来安装依赖项。

接下来是一个简单的Flutter应用示例,演示如何使用enough_ascii_art插件:

import 'package:flutter/material.dart';
import 'package:enough_ascii_art/enough_ascii_art.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ASCII Art Generator',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AsciiArtScreen(),
    );
  }
}

class AsciiArtScreen extends StatefulWidget {
  @override
  _AsciiArtScreenState createState() => _AsciiArtScreenState();
}

class _AsciiArtScreenState extends State<AsciiArtScreen> {
  final TextEditingController _controller = TextEditingController();
  String _asciiArt = '';

  void _convertTextToAsciiArt() async {
    String inputText = _controller.text;
    try {
      String asciiArt = await AsciiArt.textToAsciiArt(inputText);
      setState(() {
        _asciiArt = asciiArt;
      });
    } catch (e) {
      print('Error converting text to ASCII art: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ASCII Art Generator'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            TextField(
              controller: _controller,
              decoration: InputDecoration(
                labelText: 'Enter text',
              ),
              maxLines: 5,
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: _convertTextToAsciiArt,
              child: Text('Convert to ASCII Art'),
            ),
            SizedBox(height: 16),
            Text(
              _asciiArt,
              style: TextStyle(fontFamily: 'monospace'),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,用户可以在输入框中输入文本,然后点击按钮将文本转换为ASCII艺术。转换后的ASCII艺术会显示在下方的文本区域中。

  1. TextField用于接收用户输入的文本。
  2. ElevatedButton用于触发文本到ASCII艺术的转换。
  3. _convertTextToAsciiArt方法调用AsciiArt.textToAsciiArt进行转换,并更新状态以显示生成的ASCII艺术。

请确保你已经在pubspec.yaml中添加了enough_ascii_art的依赖,并且已经运行了flutter pub get。如果你遇到任何问题,请检查enough_ascii_art的最新版本和API文档,因为插件的API可能会随时间发生变化。

回到顶部