processCameraImageToGray8Bit method

  1. @override
Image8bit? processCameraImageToGray8Bit({
  1. int? width,
  2. int? height,
  3. Uint8List? plane0,
  4. double? rotationAngle,
  5. int backGroundColor = 0xFF,
  6. bool isFlipHoriozntal = false,
  7. bool isFlipVectical = false,
})
override

Implementation

@override
Image8bit? processCameraImageToGray8Bit({
  int? width,
  int? height,
  Uint8List? plane0,
  double? rotationAngle,
  int backGroundColor = 0xFF,
  bool isFlipHoriozntal = false,
  bool isFlipVectical = false,
}) {
  if (width == null || height == null || plane0 == null || plane0.isEmpty) {
    return null;
  }

  rotationAngle ??= 0;

  double rad =
      (rotationAngle * 3.14159265358979323846264338327950288 / 180.0);
  double sinVal = sin(rad).abs();
  double cosVal = cos(rad).abs();
  int newImgWidth = (sinVal * height + cosVal * width).toInt();
  int newImgHeight = (sinVal * width + cosVal * height).toInt();

  Pointer<Uint8> p = ffi.malloc.allocate(plane0.length);
  Uint8List pointerList = p.asTypedList(plane0.length);
  pointerList.setRange(0, plane0.length, plane0);

  Pointer<Uint8> imgP = _convertImageYuv420pToGray8Bit(
    p,
    width,
    height,
    rotationAngle,
    backGroundColor,
    isFlipVectical,
    isFlipHoriozntal,
  );

  Uint8List imgData = imgP.asTypedList(newImgHeight * newImgWidth);
  imglib.Image img = imglib.Image.fromBytes(
      bytes: imgData.buffer,
      width: newImgWidth,
      height: newImgHeight,
      numChannels: 1,
      order: imglib.ChannelOrder.red);

  ffi.malloc.free(p);
  ffi.malloc.free(imgP);

  return Image8bit(
    data: img.getBytes(),
    heigh: newImgHeight,
    width: newImgWidth,
  );
}