Building Android Apps in Python Using Kivy with Android Studio with Pyjnius, Plyer, and Buildozer
Total Page:16
File Type:pdf, Size:1020Kb
Building Android Apps in Python Using Kivy with Android Studio With Pyjnius, Plyer, and Buildozer Ahmed Fawzy Mohamed Gad Building Android Apps in Python Using Kivy with Android Studio Ahmed Fawzy Mohamed Gad Faculty of Computers & Information, Menoufia University, Shibin El Kom, Egypt ISBN-13 (pbk): 978-1-4842-5030-3 ISBN-13 (electronic): 978-1-4842-5031-0 https://doi.org/10.1007/978-1-4842-5031-0 Copyright © 2019 by Ahmed Fawzy Mohamed Gad This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Celestin Suresh John Development Editor: Rita Fernando Coordinating Editor: Divya Modi Cover designed by eStudioCalamar Cover image designed by Pixabay Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit http://www.apress.com/ rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-5030-3. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper To this person who opens his mouth after hearing that I wrote a book. Thanks for not believing in me. Table of Contents About the Author ..................................................................................................... xi About the Technical Reviewer ............................................................................... xiii Acknowledgments ...................................................................................................xv Introduction ...........................................................................................................xvii Chapter 1: Preparing Kivy for Android Application Development ............................. 1 What Is Kivy? ................................................................................................................................ 1 Kivy Installation ............................................................................................................................. 2 Building a Simple Desktop Kivy Application .................................................................................. 3 Installing Buildozer and Creating the buildozer.init File ............................................................... 5 Buildozer Templates .................................................................................................................... 10 build.gradle ........................................................................................................................... 10 AndroidManifest.xml ............................................................................................................. 11 strings.xml ............................................................................................................................. 12 Preparing Android SDK and NDK ................................................................................................. 13 Preparing Python Requirements ................................................................................................. 16 Building and Running the Simple Kivy Android Application ........................................................ 18 Summary..................................................................................................................................... 19 Chapter 2: Using KV Language for Separation of Logic and GUI ............................. 21 Adding the TextInput Widget to the GUI ....................................................................................... 21 Enriching the GUI Application by Adding More Widgets Inside Python ........................................ 23 Handling a Button Press ........................................................................................................ 25 Receiving Data from Text Input .............................................................................................. 27 Displaying Text on a Text Label .............................................................................................. 28 Nested Widgets ........................................................................................................................... 29 v TABLE OF CONTENTS Using the KV Language ............................................................................................................... 32 Invoking KV Using load_file() ................................................................................................. 34 Invoking KV Using load_string() ............................................................................................. 35 Summary..................................................................................................................................... 37 Chapter 3: Sharing Android Camera to an HTTP Server ......................................... 39 Kivy Camera Widget .................................................................................................................... 40 Accessing Android Camera ......................................................................................................... 41 Kivy Canvas ................................................................................................................................. 42 canvas.before ........................................................................................................................ 50 canvas.after, PushMatrix, and PopMatrix .............................................................................. 53 Camera Rotation ......................................................................................................................... 54 Capturing and Saving an Image Using Kivy ................................................................................ 57 Building the HTTP Server Using Flask ......................................................................................... 60 Using Requests for Uploading Files to the HTTP Server ............................................................. 62 Upload the Camera Captured Image Using the Kivy Android Application .................................... 64 Dynamic IP Address in Requests................................................................................................. 66 Capturing and Storing a Camera Image to the Memory .............................................................. 69 Posting the Captured Image to the Flask Server Using a HTTP POST Message ......................... 73 Before Sending Images to the Server ......................................................................................... 75 Processing the Received Image at the Server ............................................................................ 79 Saving and Displaying the Received Images Using an HTML Page ....................................... 81 Displaying the Received Image Without Being Saved ........................................................... 84 Continuously Uploading Images to the Server ............................................................................ 86 Controlling Image Upload Rate Using Clock .......................................................................... 90 Building the Live Camera Preview Android Application .............................................................. 93 Summary..................................................................................................................................... 98