【发布时间】:2020-10-20 18:45:40
【问题描述】:
在我的应用程序中,我必须捕获国民身份证(与信用卡大小相同)并且必须将图像传递到后端。我已尝试使用以下代码在相机应用程序中显示矩形叠加层:
return Container(
height: MediaQuery.of(context).size.height,
child: Stack(
children: <Widget>[
CustomPaint(
foregroundPainter: Paint(),
child: CameraPreview(controller),
),
ClipPath(
clipper: Clip(),
child: CameraPreview(controller)),
],
),
);
}
class Paint extends CustomPainter{
@override
void paint(Canvas canvas, Size size) {
canvas.drawColor(Colors.grey.withOpacity(0.8), BlendMode.dstOut);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}
class Clip extends CustomClipper<Path>{
@override
getClip(Size size) {
print(size);
Path path = Path()
..addRRect(RRect.fromRectAndRadius(Rect.fromLTWH(10, size.height/2-120, size.width-20, 260), Radius.circular(26)));
return path;
}
@override
bool shouldReclip(oldClipper) {
// TODO: implement shouldReclip
return true;
}
现在我可以显示叠加层(PFB 屏幕截图)。
然后我正在导航下一个屏幕...绕过图像路径
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CropImageScreen(imagePath: path),
),
);
我正在尝试裁剪图像。但是图像的高度和宽度不正确。通常在纵向模式下,高度应该更大,宽度应该更小。
但是我得到了高度:720,宽度:1280。
ImageProperties properties =
await FlutterNativeImage.getImageProperties(imagePath);
final height = properties.height;
final width = properties.width;
print("1. height is: $height");
print("2. Width is: $width");
// I have used some static values to crop the image
File croppedFile = await FlutterNativeImage.cropImage(
imagePath, 250, 40, properties.width - 500, 640);
final originalFile = croppedFile;
List<int> imageBytes = await originalFile.readAsBytes();
final originalImage = img.decodeImage(imageBytes);
但我的问题是如何使用动态值来裁剪图像。我在上面输入的静态值可能适用于我的三星设备。但这些值不适用于其他设备。
还有其他好的解决方案来裁剪矩形叠加层中的图像吗?
注意:我使用了 'package:camera/camera.dart' 库。
【问题讨论】:
-
您有解决方案吗?您能否在此处更新解决方案,以便像我这样的其他人也可以获得帮助。
-
我也遇到了同样的问题,能否分享一下你的解决方案?
标签: flutter dart camera overlay crop