0

My app does some simple image processing through dart image library. As a result of image processing I has ByteData with pixels. I want display result of image processing as it is (without converting to png or other format). I tried to implement ui.Image and draw it on Canvas, but got a native code error. My code is:

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';

class WorkImage implements ui.Image {
  final int width;
  final int height;
  final ByteData byteData;

  WorkImage({
    this.width,
    this.height,
    this.byteData,
  });

  @override
  Future<ByteData> toByteData({
    ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba
  }) {
    return Future.value(byteData);
  }

  @override
  void dispose() {

  }
}

class ViewPainter extends CustomPainter {
  final ui.Image image;

  ViewPainter({ this.image });

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawImage(image, Offset(0, 0), Paint());
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

Error is:

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x78f Cause: null pointer dereference

Please help me deal with this issue.

  • A you need some format, see : https://api.flutter.dev/flutter/dart-ui/instantiateImageCodec.html you can also use helper function https://api.flutter.dev/flutter/dart-ui/decodeImageFromList.html – pskink Dec 05 '19 at 10:56
  • You have to prepend your bytes with a BMP header, and then call `instantiateImageCodec` per pskink. You need the header to describe the geometry etc of the bitmap (width, height, pixel depth, etc) – Richard Heap Dec 05 '19 at 15:44
  • @RichardHeap , thanks! It's really simple solution! – Anton I. Krygin Dec 05 '19 at 16:28
  • See also: https://stackoverflow.com/questions/54030340/convert-a-uint8list-to-image-in-flutter-dart-ui/54030817#54030817 and //https://github.com/renancaraujo/bitmap/ – Richard Heap Dec 05 '19 at 17:12

0 Answers0