processCameraImageToGray method

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

Implementation

@override
imglib.Image? processCameraImageToGray({
  int? width,
  int? height,
  Uint8List? plane0,
  double? rotationAngle,
  int backGroundColor = 0xFFFFFFFF,
  bool isFlipHoriozntal = false,
  bool isFlipVectical = false,
}) {
  if (width == null || height == null || plane0?.isEmpty == null) {
    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 ?? 0);
  Uint8List pointerList = p.asTypedList(plane0?.length ?? 0);
  pointerList.setRange(0, plane0?.length ?? 0, plane0 ?? Uint8List(0));

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

  List<int> imgData = imgP.asTypedList(newImgWidth * newImgHeight);
  imglib.Image img = imglib.Image.fromBytes(
      bytes: Uint32List.fromList(imgData).buffer,
      width: newImgWidth,
      height: newImgHeight,
      order: imglib.ChannelOrder.rgba);

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

  return img;
}