Python for Android Tutorial #2 - Taking a picture

Welcome!

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

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 major part are compatible with iOS as well.

Must read:

Source code

Get updates Follow @aron-bordin
Star it: Star
Contribute: Fork
Download: Download

II - Index

Available tutorials:

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.

Let’s code!

IV - Creating the app

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:

__version__ = '1.0' #declare the app version. Will be used by buildozer

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 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

App screens:

Initial screen
Camera Intent

This picture was save at /storage/sdcard0/example.jpg.

Unfortunately camera still under development, so it’s not easy to create complex apps with it. And it’s compatible with Android only.

Thank you for reading, if you need something, feel free to comment here.

Aron Bordin

Aron Bordin
Computer Science Student and AI researcher. Always coding something fun :)

[Tutorial] Developing Android Background Services

### Welcome!In this post, I'll show you how to develop background services on Android Studio. We'll see two type of services: `Service` a...… Continue reading