Convert jp2 to jpeg image in flutter

238 views Asked by At

How do I convert jp2 bytes array to jpeg or some other image type in Flutter. I'm working with a file which is of jp2 format, but since flutter does not support this file format I'm searching for a way to convert this image to any readable image time. I have tried This Approach and replaced with the jp2 image bites accordingly but this has worked for me. How do I solve this.

1

There are 1 answers

1
Snehal Shingala On

import 'dart:io'; import 'package:jpeg_encode/jpeg_encode.dart';

void main() {
  // Get the path to the JP2 image file.
  final String jp2Path = '/path/to/image.jp2';

  // Read the JP2 image file as a Uint8List.
  final Uint8List jp2Bytes = File(jp2Path).readAsBytesSync();

  // Create a JPEG encoder.
  final JpegEncoder encoder = JpegEncoder();

  // Encode the JP2 image bytes as a JPEG image bytes.
  final Uint8List jpegBytes = encoder.compress(jp2Bytes, 90);

  // Save the JPEG image bytes to a file.
  File('${jp2Path}.jpg').writeAsBytesSync(jpegBytes);
}