openjpeg_ffi 0.3.1
openjpeg_ffi: ^0.3.1 copied to clipboard
Dart bindings to OpenJPEG for encoding and decoding raw JPEG2000 codestreams — native-assets FFI on native platforms, WebAssembly on web. No manual setup required either way.
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:openjpeg_ffi/openjpeg_ffi.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String resultText = 'loading…';
@override
void initState() {
super.initState();
_run();
}
Future<void> _run() async {
// No-op on native platforms; on web this awaits the WebAssembly module
// instantiating — required before the first decodeJ2k/encodeJ2k call.
await initOpenJpegWasm();
// No bundled .j2k fixture in this minimal example — this just proves
// the call reaches OpenJPEG (native or WebAssembly, depending on
// platform) and a real decode error comes back, which is enough to
// confirm the wiring itself works end to end. See
// ../test/openjpeg_ffi_test.dart / ../test/openjpeg_ffi_web_test.dart
// for real decodes against real JPEG2000 data.
String text;
try {
final image = decodeJ2k(Uint8List(0));
text = 'decoded ${image.width}x${image.height}';
} on Jp2kDecodeException catch (e) {
text =
'call reached OpenJPEG, got the expected decode error:\n${e.message}';
}
setState(() => resultText = text);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('openjpeg_ffi')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(
resultText,
style: const TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
),
),
),
);
}
}