Simple Example of accessing Java classes from Python programs, pythonjava

Source: Internet
Author: User

Simple Example of accessing Java classes from Python programs, pythonjava

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, which corresponds to all the methods and field attributes of the Java. util. Stack class in java.

OK. Maybe you want an Android-related example. Here:

from jnius import autoclassfrom time import sleep MediaRecorder = autoclass('android.media.MediaRecorder')AudioSource = autoclass('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()

Now, you can get more examples from the document.

We can map Java/Python types, native arrays, and support method overloading. We use Cython + JNI internally, so the minimum consumption performance is.

At the same time, the Python for android library has been completed and can be obtained from github.

Related Article

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.