Flutter Plugin to Convert HEIC/HEIF File To PNG/JPEG Image

HEIF Converter

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

Installation

Add the Package

dependencies:
  heif_converter: ^lastVersion

How to use

Import the package in your dart file

import 'package:heif_converter/heif_converter.dart';

And call convert method with local HEIC/HEIF image file path.

String jpgPath = await HeifConverter.convert(heicPath, output: jpgPath);
String pngPath = await HeifConverter.convert(heicPath, format: 'png'); 

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add heif_converter

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  heif_converter: ^1.0.0

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:heif_converter/heif_converter.dart'; 

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;
  }
} 

Download details:

Author: joutvhu

Source: https://github.com/joutvhu/heif_converter

#flutter #android #ios 

Flutter Plugin to Convert HEIC/HEIF File To PNG/JPEG Image
1.05 GEEK