ldd_bmp 0.1.0
ldd_bmp: ^0.1.0 copied to clipboard
将图像转换成标签打印机能识别的bmp图像,它是 1 位深度的
example/lib/main.dart
import 'dart:io';
import 'dart:typed_data';
import "package:image_picker/image_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';
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("保存图片")),
Text("转换结果"),
if(bmpData!=null)
Image.memory(bmpData!)
],
),
),
// 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;
});
final ImagePicker picker = ImagePicker();
final xfile = await picker.pickImage(source: ImageSource.gallery);
if(xfile!=null){
setState(() {
file = File(xfile.path);
});
}
}
}
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');
}