如何在Flutter中使用starflut导入Python包?

6

我正在尝试将Python脚本集成到我的Flutter应用程序中,并且需要在Python代码中导入cv2包,但它找不到该包。

是否有一种使用starflut在Flutter中导入Python包的方法?

以下是我用来将Python代码链接到我的Flutter应用程序的代码:

dynamic python = null;
String _outputString = "python 3.6";
StarSrvGroupClass SrvGroup;
String docPath;
String resPath;
StarServiceClass Service;

Future<void> testCallPython() async {
  //Initialize starecore
  StarCoreFactory starcore = await Starflut.getFactory();
  Service = await starcore.initSimple("test", "123", 0, 0, []);

  //Register callback function
  await starcore.regMsgCallBackP(
      (int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
    print("FIRST PRINT $serviceGroupID  $uMsg   $wParam   $lParam");
    return null;
  });
  SrvGroup = await Service["_ServiceGroup"];

  /*---script python--*/
  //Initialize python
  // for android, before run python, share libraries must be copied to app’s local folder.
  bool isAndroid = await Starflut.isAndroid();
  if (isAndroid == true) {
    await Starflut.copyFileFromAssets("Main_darts.py",
        "flutter_assets/starfiles", "flutter_assets/starfiles");
    await Starflut.copyFileFromAssets("python3.6.zip",
        "flutter_assets/starfiles", null); //desRelatePath must be null
    await Starflut.copyFileFromAssets("zlib.cpython-36m.so", null, null);
    await Starflut.copyFileFromAssets("unicodedata.cpython-36m.so", null, null);
    await Starflut.loadLibrary("libpython3.6m.so");
  }

  docPath = await Starflut.getDocumentPath();
  print("docPath = $docPath");

  resPath = await Starflut.getResourcePath();
  print("resPath = $resPath");

// Initialize python
  dynamic rr1 = await SrvGroup.initRaw("python36", Service);

  print("initRaw = $rr1");

// PYTHON SCRIPT
  var Result1 = await SrvGroup.loadRawModule("python", "",
      resPath + "/flutter_assets/starfiles/" + "Main_darts.py", false);
  print("loadRawModule = $Result1");

  python = await Service.importRawContext("python", "", false, "");
  print("python = " + await python.getString());
}

提前感谢。

1个回答

2
如果您想在应用程序中集成Python包,我强烈建议使用Flutter的chaquopy插件,因为它易于使用并支持Python包。请注意保留HTML标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接