任意图像转 1 位深度的 bmp图像

初始化

await bmpSdkInit();

Future<void> main() async {
  await bmpSdkInit();
  runApp(const MyApp());
}

转换

函数: convertTo1BitBmp

  • 参数 1: 要转换的图片字节数据
  • 参数 2: 图片的类型,LddImageType是一个枚举,
      enum LddImageType {
      png,
      jpeg,
      gif,
      webP,
      pnm,
      tiff,
      tga,
      dds,
      bmp,
      ico,
      hdr,
      openExr,
      farbfeld,
      avif,
      qoi,
      ;
    }
    
  • 可选参数: resize: 对图像进行缩放
    const reSize = ResizeOpt(
      width: 200,//宽度
      height: 200,//高度
      filter: LddFilterType.nearest,//压缩算法
    );
    
  • 可选参数: isApplyOrderedDithering: 是否使用 h4x4a 抖动算法

返回类型: 处理完成的字节数据Uint8List

完整的代码

import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:ldd_bmp/api/entitys.dart';
import 'package:ldd_bmp/api/image.dart';
import 'package:ldd_bmp/ldd_bmp.dart';
import 'dart:async';

import 'package:path_provider/path_provider.dart';

const reSize = ResizeOpt(
  width: 200,
  height: 200,
  filter: LddFilterType.nearest,
);

Future<void> main() async {
  await bmpSdkInit();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File? file;
  Uint8List? bmpData;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Packages'),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              FilledButton(onPressed: selectFile, child: const Text('选择文件')),
              ElevatedButton(
                  onPressed: file == null
                      ? null
                      : () async {
                          final bts = await file!.readAsBytes();
                          bmpData = await convertTo1BitBmp(
                              inputData: bts,
                              imageType: LddImageType.jpeg,
                              isApplyOrderedDithering: true,
                              resize: const ResizeOpt(
                                width: 200,
                                height: 200,
                                filter: LddFilterType.nearest,
                              ));
                          setState(() {});
                        },
                  child: const Text("转换")),
              ElevatedButton(
                  onPressed: bmpData == null
                      ? null
                      : () {
                          saveImageToFile(bmpData!);
                        },
                  child: const Text("保存图片"))
            ],
          ),
        ),
        floatingActionButton: bmpData != null
            ? ConstrainedBox(
                constraints:
                    const BoxConstraints(maxHeight: 300, maxWidth: 300),
                child: Card(
                  child: Image.memory(bmpData!),
                ),
              )
            : null,
      ),
    );
  }

  Future<void> selectFile() async {
    setState(() {
      file = null;
    });
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    if (result != null) {
      file = File(result.files.single.path!);
      setState(() {});
    } else {
      // User canceled the picker
    }
  }
}

Future<void> saveImageToFile(Uint8List imageData) async {
  // 获取应用程序的文档目录
  final directory = await getApplicationDocumentsDirectory();

  // 设置文件路径和文件名
  final filePath = '${directory.path}/image.bmp';

  // 创建一个文件对象
  final file = File(filePath);

  // 将Uint8List数据写入文件
  await file.writeAsBytes(imageData);

  print('Image saved to $filePath');
}