This is the second post about mobile development with Python. It’s a short tutorial, just to help you to understand and learn how to work with Python for Mobile. In this post you will see how to take pictures using Python and Android(Not compatible with iOS).
I – About the tutorials
(If you had read another tutorial, you can go to the topic III, because I and II is the same in all tutorials)
All my next posts will be about this tutorial series. I’ll show you how to use each mobile API, like compass, camera, sensors, sound, and others. After the API, we will start to test some Python libs, as PyGame and OpenCV. I’m using Android with buildozer, but some libs are compatible with iOS too(I’ll tell you when it’s compatible), so if you test it, send a feedback if possible.
In each post, I’ll show a sample app using on of them. When I finish all the tutorial, you will be able to develop almost any app with Python. In the principle, I’m not going to show the design stuff, but I recommend you to check Kivy and PySide. After finishing theses tutorials, maybe I can start a tutorial series about Kivy, for the design, but it’s easier to find at Google 🙂
You can check the Github repo here, and if you like it, please, star it 🙂
To be able to read all the tutorials, please follow my blog or subscribe your email to get updates. Another important thing if your feedback. I’m Brazilian and trying to write in English, so if you have any problem, please, just let me know. To make the tutorials better ,try to comment always that is possible, showing your experiences, tips for a better tutorial, and this kind of stuff.
Before start, you can check this links to configure all the prerequisites:
- How to build Python application for Android / iOS and Desktop(Required)
- [Tutorial] Android development with Python and Kivy – Introduction
- [Tutorial] How to build Python for Android with Ubuntu and Buildozer
II – Index
You will be able to get the links for all tutorials here, I’ll be updating when a new tutorial is published.
III – About this sample
Unfortunately Plyer camera still under development, so we will not be able to develop complex apps with it now. It’s only compatible with Android, and we are not able to get camera preview.
In this version, we can start the camera intent with the command take_picture and choose a place to save the photo. So the app will open the Android camera, take a picture, and if you need to, you can read this image when necessary.
To show you how to use this class, I’ll show a simple sample, when the user click in the screen, a new image will be taken. A label will show where the image was saved. So if you are using Kivy or PySide for the UI, you can get this link and show the image.
IV – Creating our app
I’ll try to post a tutorial each two days. We are in the second tutorial, with basic examples, if you want to read all the tutorials, and at the end be able to develop any mobile app using Python, just follow my blog or subscribe you email to get updates. We are in the beginning posts, it’ll became more complex soon 🙂
Create a new folder and create a new file named main.py. Now, open your terminal and run the command bellow to create buildozer spec file.
$ buildozer init
Open your buildozer.spec and edit it. In this tutorial we are going to use Plyer, so make sure to have the following line:
requirements = kivy, plyer
You can check my version here:
[app] title = Python Camera package.name = camera package.domain = com.wordpress.bytedebugger source.dir = . source.include_exts = py,png,jpg,kv,atlas version = 1.0 requirements = kivy, plyer fullscreen = 1 [buildozer] log_level = 2
V – Let’s code it
First, check the app version and import all necessary libs:
from kivy.app import App #for the main app from kivy.uix.floatlayout import FloatLayout #the UI layout from kivy.uix.label import Label #a label to show information from plyer import camera #object to read the camera
Now, create our basic UI:
class UI(FloatLayout):#the app ui def __init__(self, **kwargs): super(UI, self).__init__(**kwargs) self.lblCam = Label(text="Click to take a picture!") #create a label at the center self.add_widget(self.lblCam) #add the label at the screen def on_touch_down(self, e): camera.take_picture('/storage/sdcard0/example.jpg', self.done) #Take a picture and save at this location. After will call done() callback def done(self, e): #receive e as the image location self.lblCam.text = e; #update the label to the image location
Create our app class:
class Camera(App): #our app def build(self): ui = UI()# create the UI return ui #show it def on_pause(self): #when the app open the camera, it will need to pause this script. So we need to enable the pause mode with this method return True def on_resume(self): #after close the camera, we need to resume our app. pass
Camera().run() #start our app
This picture was save at /storage/sdcard0/example.jpg.
Unfortunately camera still under development, so we are not able to create complex apps with it. And it’s compatible with Android only.
If you want to, follow the blog and/or subscribe your email to get a notification about the next post.
Thank you for reading, if you need something, feel free to comment here.
Hey!! I’m available for freelances!! If you need anything, just contact me!