gpt4 book ai didi

flutter - 如何在Flutter中裁剪图像?

转载 作者:行者123 更新时间:2023-12-02 02:18:20 60 4
gpt4 key购买 nike

我使用tflite进行图像识别

对于此图像:


我有以下结果:

{rect:{w:0.9337246417999268,x:0.03002336621284485,h:0.669991135597229,y:0.31182175874710083},confidenceInClass:0.609375,DetectedClass:人}

通过使用上面的结果,我可以在对象周围创建一个矩形

如何自动裁剪已识别的矩形并将其保存到内存中。

最佳答案

通过使用 image_cropper .

也许这个包有帮助 - Flutter Image cropper
您只需将此权限添加到 AndroidManifest.xml 中即可。

<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

这是使用 image_cropper 的官方示例


import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';

import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ImageCropper',
theme: ThemeData.light().copyWith(primaryColor: Colors.deepOrange),
home: MyHomePage(
title: 'ImageCropper',
),
);
}
}

class MyHomePage extends StatefulWidget {
final String title;

MyHomePage({required this.title});

@override
_MyHomePageState createState() => _MyHomePageState();
}

enum AppState {
free,
picked,
cropped,
}

class _MyHomePageState extends State<MyHomePage> {
late AppState state;
File? imageFile;

@override
void initState() {
super.initState();
state = AppState.free;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: imageFile != null ? Image.file(imageFile!) : Container(),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.deepOrange,
onPressed: () {
if (state == AppState.free)
_pickImage();
else if (state == AppState.picked)
_cropImage();
else if (state == AppState.cropped) _clearImage();
},
child: _buildButtonIcon(),
),
);
}

Widget _buildButtonIcon() {
if (state == AppState.free)
return Icon(Icons.add);
else if (state == AppState.picked)
return Icon(Icons.crop);
else if (state == AppState.cropped)
return Icon(Icons.clear);
else
return Container();
}

Future<Null> _pickImage() async {
final pickedImage =
await ImagePicker().getImage(source: ImageSource.gallery);
imageFile = pickedImage != null ? File(pickedImage.path) : null;
if (imageFile != null) {
setState(() {
state = AppState.picked;
});
}
}

Future<Null> _cropImage() async {
File? croppedFile = await ImageCropper.cropImage(
sourcePath: imageFile!.path,
aspectRatioPresets: Platform.isAndroid
? [
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9
]
: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio5x3,
CropAspectRatioPreset.ratio5x4,
CropAspectRatioPreset.ratio7x5,
CropAspectRatioPreset.ratio16x9
],
androidUiSettings: AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: Colors.deepOrange,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false),
iosUiSettings: IOSUiSettings(
title: 'Cropper',
));
if (croppedFile != null) {
imageFile = croppedFile;
setState(() {
state = AppState.cropped;
});
}
}

void _clearImage() {
imageFile = null;
setState(() {
state = AppState.free;
});
}
}

或者您可以观看如下教程:

关于flutter - 如何在Flutter中裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66829808/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com