A simple example of accessing Java classes from a Python program

Source: Internet
Author: User
From Jnius import autoclass>>> stack = autoclass (' java.util.Stack ') >>> stack = stack () >>> Stack.push (' hello ') >>> Stack.push (' World ') >>> stack.pop () ' World ' >>> stack.pop () ' Hello '

In the above code, we use the Autoclass function to create a type proxy that corresponds to all the methods and field properties of the Java.util.Stack class in Java.

OK, maybe you want an android-related example, look here:

From Jnius import autoclassfrom time import sleep Mediarecorder = Autoclass (' android.media.MediaRecorder ') Audiosource = a Utoclass (' Android.media.mediarecorder$audiosource ') OutputFormat = Autoclass (' android.media.mediarecorder$ OutputFormat ') Audioencoder = Autoclass (' Android.media.mediarecorder$audioencoder ') # Record the microphone with a 3GP Recordermrecorder = Mediarecorder () mrecorder.setaudiosource (audiosource.mic) Mrecorder.setoutputformat ( OUTPUTFORMAT.THREE_GPP) mrecorder.setoutputfile ('/sdcard/testrecorder.3gp ') Mrecorder.setaudioencoder ( AUDIOENCODER.ARM_NB) Mrecorder.prepare () # Record 5 Secondsmrecorder.start () sleep (5) Mrecorder.stop () Mrecorder.release ()

Well, you can get more examples from the documentation.

We can already map the types of Java/python, native arrays, support method overloads, and so on. We use Cython + JNI internally, so the consumption performance is minimal.

Meanwhile, the Python for Android Library has been completed and you can get it from GitHub.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.