【问题标题】:How to Crop Image in Flutter?如何在 Flutter 中裁剪图像?
【发布时间】:2021-06-24 01:00:29
【问题描述】:

我正在使用 tflite 进行图像识别

对于这张图片:


我有以下结果:

{rect: {w: 0.9337246417999268, x: 0.03002336621284485, h: 0.669991135597229, y: 0.31182175874710083}, confidenceInClass: 0.609375, detectedClass: person}

通过使用上述结果,我可以在对象周围制作一个矩形

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

【问题讨论】:

  • 是您正在寻找的以下答案

标签: flutter image-processing crop


【解决方案1】:
image = cv2.imread('sample.jpg')
#To make box around the image
for (x, y, w, h) in rects:
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
#for writting Text
cv2.putText(image, "{}".format(i), (int(x), int(y)),
                cv2.FONT_HERSHEY_SIMPLEX,
                1, (0, 0, 255), 2,
                lineType=cv2.LINE_AA)

cv2.imwrite('image.jpg',image)

我想你正在寻找这个

【讨论】:

    【解决方案2】:

    通过使用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;
        });
      }
    }
    
    

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

    【讨论】:

      【解决方案3】:

      如果您想裁剪图像而不使用 Image_cropper。相反,您可以使用图像包图像:^3.0.2。

      image_package

      你需要加载图像并将图像解码成一个列表,使用copyCrop函数进行裁剪,然后将其编码回来

      【讨论】:

        猜你喜欢
        • 2019-05-25
        • 1970-01-01
        • 2015-02-02
        • 2012-11-16
        • 1970-01-01
        • 2021-07-24
        • 2020-08-14
        • 2012-02-02
        • 2011-09-21
        相关资源
        最近更新 更多