renderRotatedImage static method

Future<Image> renderRotatedImage({
  1. required Image image,
  2. required double angle,
})

渲染一个只包含旋转变换的新图片

Implementation

static Future<ui.Image> renderRotatedImage({
  required ui.Image image,
  required double angle,
}) async {
  // 计算旋转后新图片的边界框大小
  final sinAngle = math.sin(angle).abs();
  final cosAngle = math.cos(angle).abs();
  final newWidth = image.width * cosAngle + image.height * sinAngle;
  final newHeight = image.width * sinAngle + image.height * cosAngle;

  final recorder = ui.PictureRecorder();
  final canvas = Canvas(recorder, Rect.fromLTWH(0, 0, newWidth, newHeight));

  // 将画布原点移动到新画布中心
  canvas.translate(newWidth / 2, newHeight / 2);
  // 旋转
  canvas.rotate(angle);
  // 将图片中心对齐到原点并绘制
  canvas.drawImage(image, Offset(-image.width / 2, -image.height / 2), Paint());

  final picture = recorder.endRecording();
  return await picture.toImage(newWidth.round(), newHeight.round());
}