ldd_bmp 0.0.4 copy "ldd_bmp: ^0.0.4" to clipboard
ldd_bmp: ^0.0.4 copied to clipboard

将图像转换成标签打印机能识别的bmp图像,它是 1 位深度的

example/lib/main.dart

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('选择文件')),
              if (file != null)
                Image.file(
                  file!,
                  width: 200,
                  height: 200,
                ),
              ElevatedButton(
                  onPressed: file == null
                      ? null
                      : () async {
                          final bts = await file!.readAsBytes();
                          bmpData = await convertTo1BitBmp(
                              inputData: bts,
                              imageType: LddImageType.jpeg,
                              isApplyOrderedDithering: false,
                              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(
                  elevation: 10,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      children: [
                        const Text('转换结果'),
                        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');
}
0
likes
140
points
118
downloads

Publisher

verified publisheritbug.shop

Weekly Downloads

将图像转换成标签打印机能识别的bmp图像,它是 1 位深度的

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, flutter_rust_bridge, plugin_platform_interface

More

Packages that depend on ldd_bmp