基于Android Studio的Android-Python 混合开发

基于Android Studio的Android-Python 混合开发,第1张

build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        maven { url "https://chaquo.com/maven" }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath "com.chaquo.python:gradle:10.0.1"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
build.gradle(Module)
plugins {
    id 'com.android.application'
    id 'com.chaquo.python'
}


android {
    compileSdk 32


    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        python {
            // 指定python路径
            buildPython "C:\Users\Oceans\AppData\Local\Programs\Python\Python310\python.exe"
            pip{
                install "opencv-python"
                install "numpy"
//                install "wave"
//                install "scipy"
//                install "matplotlib"
                install "opencv-contrib-python"
                install "pillow"
            }

        }
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {


    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

安装python插件
File --> setting --> Plugins , 搜索Python, 选中Python Community Edition插件, 安装,重新启动AS后。

创建python文件夹

初始化Python

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPython();
    }
    private void initPython(){
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }

    }
调用Python方法

Python.getInstance() : 得到python的对象
py.getModule(“xxx”) : 是调用名为xxx的.py文件
callAttr(“photo”,new Kwarg(“jpg”,paths)): 第一个参数表示调用的python中的方法名photo,后面是传入方法的参数(jpg为方法的参数名,paths为传入的变量)
obj.toJava 将PyObject类型转为java中的类型

Python py = Python.getInstance();
PyObject obj = py.getModule("process").callAttr("photo",new Kwarg("jpg",paths));
byte[] py_photo =obj.toJava(byte[].class);

https://www.jianshu.com/p/a875b0f37a88

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/992624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存