我正在使用flutter flutter捕获窗口小部件的屏幕截图。但是,在某些设备(例如
Tecnobe8)上,捕获的图像出现(垂直翻转).。 FlutterSDK版本:
代码我正在使用
RenderRepaintBoundary
screenshot: ^3.0.0
仅在某些设备上(例如tecno be8)。 其他设备正确捕获图像。
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
final GlobalKey _globalKey = GlobalKey();
Future<Uint8List?> _captureWidget() async {
try {
// Find the RenderRepaintBoundary
RenderRepaintBoundary boundary =
_globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
// Convert the widget to an image
ui.Image image = await boundary.toImage(pixelRatio: 3.0); // Adjust pixelRatio for quality
// Convert the image to a byte array (Uint8List)
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List uint8List = byteData!.buffer.asUint8List();
return uint8List;
} catch (e) {
print('Error capturing widget: $e');
return null;
}
}
// Display the captured image
void _showCapturedImage(BuildContext context, Uint8List image) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Image.memory(image),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Close'),
),
],
);
},
);
}
RepaintBoundary(
key: _globalKey,
child: Container(
padding: EdgeInsets.all(20),
color: Colors.blue,
child: Text(
'Hello, World!',
style: TextStyle(color: Colors.white, fontSize: 24),
),
),
),
pixelRatio
问题是,捕获的图像在某些设备上垂直翻转。
BTW仅用于您的信息,
toImage()
软件包还在引擎盖下使用
screenshot