skip to Main Content

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.

2

Answers


  1. 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);
    }
    
    Login or Signup to reply.
  2. To convert a JP2 (JPEG 2000) image byte array to a more widely supported image format like JPEG or PNG in Flutter, you can use the image package. This package provides functionalities for decoding, encoding, and manipulating various image formats.

    Package: https://pub.dev/packages/image

    N.B: I haven’t tried it yet. I just looked through the documentation and suggesting you to try it. If it works or not, please let me know.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search