heif_converter 1.0.0 copy "heif_converter: ^1.0.0" to clipboard
heif_converter: ^1.0.0 copied to clipboard

Flutter plugin to convert HEIC/HEIF file to PNG/JPEG image.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:heif_converter/heif_converter.dart';
import 'package:path_provider/path_provider.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  final httpClient = HttpClient();
  String heicUrl = 'https://filesamples.com/samples/image/heic/sample1.heic';
  String? output;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String? tmp = await downloadAndConvert();

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      output = tmp;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: (output != null && output!.isNotEmpty)
              ? Image.file(File(output!))
              : const Text('No Image'),
        ),
      ),
    );
  }

  Future<String?> downloadAndConvert() async {
    File heicFile = await _downloadFile(heicUrl, 'sample.heic');
    return HeifConverter.convert(heicFile.path, format: 'png');
  }

  Future<File> _downloadFile(String url, String filename) async {
    var request = await httpClient.getUrl(Uri.parse(url));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    String dir = (await getTemporaryDirectory()).path;
    File file = File('$dir/$filename');
    await file.writeAsBytes(bytes);
    return file;
  }
}
13
likes
140
points
4.99k
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin to convert HEIC/HEIF file to PNG/JPEG image.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on heif_converter