Python _ How to Use python to access Java classes (1)

Source: Internet
Author: User

I'm glad to introduce you to a pyjnius project. This is a python library that can be used on the desktop or android to access Java classes.

AD:

I'm glad to introduce you
Pyjnius project. This is a python library that can be used on the desktop or android to access Java classes.

Source code: github.com/kivy/pyjnius

Document: pyjnius.readthedocs.org

There are also some other libraries, such
Jpype or
Py4j, which is not well designed and available. And use
Jython is not another option because we want to use
Python Android development project.

Now let me tell you how to simply use pyjnius:

 
 
  1. >>> from jnius import autoclass  
  2. >>> Stack = autoclass('java.util.Stack')  
  3. >>> stack = Stack()  
  4. >>> stack.push('hello')  
  5. >>> stack.push('world')  
  6. >>> stack.pop()  
  7. 'world' 
  8. >>> stack.pop()  
  9. 'hello' 

In the above Code, we use
The autoclass function creates a type proxy that 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:

 
 
  1. from jnius import autoclass  
  2. from time import sleep  
  3.    
  4. MediaRecorder = autoclass('android.media.MediaRecorder')  
  5. AudioSource = autoclass('android.media.MediaRecorder$AudioSource')  
  6. OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')  
  7. AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')  
  8.    
  9. # Record the Microphone with a 3GP recorder  
  10. mRecorder = MediaRecorder()  
  11. mRecorder.setAudioSource(AudioSource.MIC)  
  12. mRecorder.setOutputFormat(OutputFormat.THREE_GPP)  
  13. mRecorder.setOutputFile('/sdcard/testrecorder.3gp')  
  14. mRecorder.setAudioEncoder(AudioEncoder.ARM_NB)  
  15. mRecorder.prepare()  
  16.    
  17. # Record 5 seconds  
  18. mRecorder.start()  
  19. sleep(5)  
  20. mRecorder.stop()  
  21. mRecorder.release() 

Now, you can get more examples from the document.

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

At the same time,
The Python for Android library has been completed. You can obtain it from GitHub.

Original English,
Oschina. Net Translation

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.