(Ebook) Android Application Programming with OpenCV 3_ Build Android apps to capture, manipulate, and track objects in 2D and 3D by kan - Experience the full ebook by downloading it now
(Ebook) Android Application Programming with OpenCV 3_ Build Android apps to capture, manipulate, and track objects in 2D and 3D by kan - Experience the full ebook by downloading it now
com
https://ebooknice.com/product/android-application-
programming-with-opencv-3-build-android-apps-to-capture-
manipulate-and-track-objects-in-2d-and-3d-50195958
OR CLICK BUTTON
DOWLOAD EBOOK
(Ebook) How to Build Android Apps with Kotlin: A practical guide to developing,
testing, and publishing your first Android apps by Alex Forrester; Eran Boudjnah;
Alexandru Dumbravan; Jomar Tigcal ISBN 9781837634934, 1837634939
https://ebooknice.com/product/how-to-build-android-apps-with-kotlin-a-practical-
guide-to-developing-testing-and-publishing-your-first-android-apps-56378466
ebooknice.com
(Ebook) Sams Teach Yourself Android Application Development in 24 Hours, 2nd Edition
by kan
https://ebooknice.com/product/sams-teach-yourself-android-application-
development-in-24-hours-2nd-edition-50194954
ebooknice.com
https://ebooknice.com/product/android-continuous-integration-build-deploy-test-
automation-for-android-mobile-apps-6730348
ebooknice.com
https://ebooknice.com/product/unity-5-for-android-essentials-a-fast-paced-guide-
to-building-impressive-games-and-applications-for-android-devices-with-
unity-5-50194522
ebooknice.com
(Ebook) Building Android Apps in Python Using Kivy with Android Studio: With
Pyjnius, Plyer, and Buildozer by Ahmed Fawzy Mohamed Gad ISBN 9781484250303,
1484250303
https://ebooknice.com/product/building-android-apps-in-python-using-kivy-with-
android-studio-with-pyjnius-plyer-and-buildozer-50195834
ebooknice.com
https://ebooknice.com/product/android-studio-3-3-development-essentials-
android-9-edition-developing-android-9-apps-using-android-studio-3-3-java-and-
android-jetpack-10469504
ebooknice.com
(Ebook) Android Studio Iguana Essentials - Kotlin Edition: Developing Android Apps
Using Android Studio 2023.2.1 and Kotlin by Neil Smyth
https://ebooknice.com/product/android-studio-iguana-essentials-kotlin-edition-
developing-android-apps-using-android-studio-2023-2-1-and-kotlin-56375076
ebooknice.com
(Ebook) Pro Android Python with SL4A: Writing Android Native Apps Using Python, Lua,
and Beanshell by Ferrill, Paul ISBN 9781430235699, 1430235691
https://ebooknice.com/product/pro-android-python-with-sl4a-writing-android-
native-apps-using-python-lua-and-beanshell-43191386
ebooknice.com
(Ebook) Head First Android Development: A Learner's Guide to Building Android Apps
with Kotlin by Dawn Griffiths, David Griffiths ISBN 9781492076520, 149207652X
https://ebooknice.com/product/head-first-android-development-a-learner-s-guide-
to-building-android-apps-with-kotlin-36451462
ebooknice.com
www.it-ebooks.info
Android Application
Programming with OpenCV 3
Joseph Howse
BIRMINGHAM - MUMBAI
www.it-ebooks.info
Android Application Programming with OpenCV 3
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78528-538-7
www.packtpub.com
www.it-ebooks.info
Credits
Indexer
Commissioning Editor
Tejal Daruwale Soni
Veena Pagare
Production Coordinator
Acquisition Editor
Manu Joseph
Vivek Anantharaman
Cover Work
Content Development Editor
Manu Joseph
Parita Khedekar
Technical Editor
Ryan Kochery
www.it-ebooks.info
About the Author
Joseph Howse lives in Canada. During the cold winters, he grows a beard and his
four cats grow thick coats of fur. He combs the cats every day. Sometimes, the cats
pull his beard.
Joseph has authored OpenCV for Secret Agents, OpenCV Android Application
Programming, and OpenCV Computer Vision with Python. When he is not writing
books or grooming cats, Joseph provides consulting, training, and software
development services. His company is Nummist Media (http://nummist.com).
I dedicate my work to Sam, Jan, Bob, Bunny, and the cats, who have
been my lifelong guides and companions.
www.it-ebooks.info
About the Reviewers
Jared Burrows started working on Android development in 2011 when he got his
first smartphone. He learned Java quickly and started putting applications on Google
Play (the Android market). During this time, he was in a college and was interning at
Northrop Grumman; currently, he works there as a full-time software engineer.
As his programming skills have matured through the years, he has produced 1-2 new
apps each year and constantly remains active on websites such as StackOverflow,
developing a good reputation and helping others. When he bought a Google Glass
back in 2013, the first thing he created with an open source repository on GitHub
named OpenQuartz, and he has worked on implementing OpenCV into a few
example applications with Google Glass.
www.it-ebooks.info
Manav Kedia is a final-year undergraduate student of the department of Computer
Science and Engineering at the Indian Institute of Technology, Kharagpur. He has
interned with Adobe Research Labs, Bengaluru, and ETH Zurich previously. He is a
passionate programmer and software enthusiast. Android application development is
his forte among other things, in which he has bagged laurels from various hackathons
organized by IBM and Shephertz. He is proficient in programming languages such as
C++, Java, Python, AngularJS, and MySQL. He always ventures into new stuff. You
can reach him at [email protected].
I would like to thank the author for this brilliantly written book.
Reviewing this book was a great learning experience. I would
like to thank Milton Dsouza for coordinating with me throughout
the review. I would also like to thank my parents and friends for
supporting me in everything I do.
Yati Sagade is a programmer interested in, and working on, problems around
image analysis and computer vision. He has developed several computer vision
apps on the Android platform, including a work-in-progress "Air Piano" app named
Dirac, which along with his other projects can be found at https://github.com/
yati-sagade/.
www.it-ebooks.info
www.PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a discount on the eBook copy. Get in
touch with us at [email protected] for more details.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface iii
Chapter 1: Setting Up OpenCV 1
System requirements 2
Setting up a development environment 3
Getting prebuilt OpenCV4Android 5
Building OpenCV4Android from source 6
Building the OpenCV samples with Eclipse 8
Troubleshooting Eclipse projects 18
Troubleshooting the USB connection 21
Finding the documentation and help 23
Summary 23
Chapter 2: Working with Camera Frames 25
Designing our app – Second Sight 25
Creating the Eclipse project 28
Enabling camera and disk access in the manifest 33
Creating menu and string resources 36
Previewing and saving photos in CameraActivity 38
Deleting, editing, and sharing photos in LabActivity 51
Summary 55
Chapter 3: Applying Image Effects 57
Adding files to the project 57
Defining the Filter interface 60
Mixing color channels 60
Making subtle color shifts with curves 64
Mixing pixels with convolution filters 71
Adding the filters to CameraActivity 74
Summary 80
[i]
www.it-ebooks.info
Table of Contents
[ ii ]
www.it-ebooks.info
Preface
This book will show you how to use OpenCV in an Android app that displays a
camera feed, saves and shares photos, manipulates colors and edges, and tracks
real-world objects in 2D or 3D. Integration with OpenGL is also introduced so that
you can start building augmented reality (AR) apps that superimpose virtual 3D
scenes onto tracked objects in the camera feed.
Android is a mobile operating system that is mostly open source. For Java developers,
it offers a high-level application framework called Android SDK. Android apps are
modular insofar as they have standard, high-level interfaces to launch each other and
share data. Mobility, a high level of abstraction, and data sharing are great starting
points for a photo sharing app, similar to the one we will build.
Although OpenCV and Android provide a lot of high-level abstractions (and a lot of
open source code for curious users to browse), they are not necessarily easy to use
for newcomers. Setting up an appropriate development environment and translating
the libraries' broad functionality into application features are both daunting tasks.
This concise book helps us by placing an emphasis on a clean setup, clean application
design, and a simple understanding of each function's purpose.
The need for a book on this subject is particularly great because OpenCV's Java
and Android bindings are quite new and their documentation is not yet mature.
Little has been written about the steps for integrating OpenCV with Android's
standard camera, media, and graphics APIs. Surely, integration is a major part
of an app developer's work, so it is a major focus of this book.
[ iii ]
www.it-ebooks.info
Preface
By the end of our journey together, you will have a taste of the breadth of application
features that are made possible by integrating OpenCV with other Android libraries.
You will have your own small library of reusable classes that you can extend or modify
for your future computer vision projects. You will have a development environment
and the knowledge to use it, and you will be able to make more apps!
Chapter 2, Working with Camera Frames, shows how to integrate OpenCV into an
Android Java app that can preview, capture, save, and share photos.
Chapter 5, Combining Image Tracking with 3D Rendering, improves upon our previous
tracking technique by determining the target's position and rotation in real 3D
space. We expand our app so that it sets up an OpenGL 3D scene with the same
perspective as the Android device's real camera. Then, we draw a 3D cube atop
any tracked target.
Chapter 6, Mixing Java and C++ via JNI, demonstrates the use of Java Native Interface
(JNI) to call C++ functions from Java. We convert some of our application's filters
to C++ in order to learn about writing efficient, cross-platform code with OpenCV's
C++ interface.
[ iv ]
www.it-ebooks.info
Preface
You need a mobile device running Android 2.2 (Froyo) or greater, and it must have a
camera. Preferably, it should have two cameras, front and rear.
Conventions
In this book, you will find a number of text styles that distinguish between different
kinds of information. Here are some examples of these styles and an explanation of
their meaning.
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"It will interface with the other apps on the device, via Android's MediaStore and
Intent classes."
When we wish to draw our attention to a particular part of a code block, the relevant
lines or items are set in bold:
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
[v]
www.it-ebooks.info
Preface
New terms and important words are shown in bold. Words that you see on the
screen, for example, in menus or dialog boxes, appear in the text like this: "These
steps should be repeated for all the native (C++) projects, which include OpenCV
Sample – face-detection and OpenCV Tutorial 2 - Mixed Processing."
Reader feedback
Feedback from our readers is always welcome. Let's know what you think about this
book—what you liked or disliked. Reader feedback is important for us as it helps us
develop titles that you will really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to
help you get the most from your purchase.
[ vi ]
www.it-ebooks.info
Preface
Errata
Although we took every care to ensure the accuracy of our content, mistakes do
happen. If you find a mistake in one of our books—maybe a mistake in the text or
code—we would be grateful if you could report this to us. By doing so, you can save
other readers from frustration and help us improve subsequent versions of this book.
If you find any errata, please report them by visiting http://www.packtpub.com/
submit-errata, selecting your book, clicking on the Errata Submission Form link,
and entering the details of your errata. Once your errata are verified, your submission
will be accepted and the errata will be uploaded to our website or added to any list of
existing errata under the Errata section of that title.
Piracy
Piracy of the copyrighted material on the Internet is an ongoing problem across all
media. At Packt, we take the protection of our copyright and licenses very seriously.
If you come across any illegal copies of our works in any form on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors and our ability to bring you
valuable content.
[ vii ]
www.it-ebooks.info
Preface
Questions
If you have a problem with any aspect of this book, you can contact us at
[email protected], and we will do our best to address the problem.
You can also contact the author directly at [email protected] or you
can check his website, http://nummist.com/opencv/, for answers to common
questions about the book.
[ viii ]
www.it-ebooks.info
Setting Up OpenCV
This chapter is a quick guide to setting up a development environment for Android
and OpenCV. We will also look at the OpenCV sample applications, documentation,
and community.
By the end of this chapter, our development environment will include the following
components:
• Java Development Kit (JDK) 7: This includes tools for Java programming.
JDK 7 is the exact version that we require. The more recent version, JDK 8,
is not yet supported for Android development.
• Cygwin 1.7 or greater (Windows only): This is a compatibility layer that
provides Unix-like programming tools on Windows. We need it in order
to develop in C++ on Android.
• Android Software Development Kit (Android SDK) r24.0.2 or greater:
This includes tools for programming Android apps in Java.
• Android Native Development Kit (Android NDK) r10d or greater:
This includes tools for programming Android apps in C++.
• Eclipse 4.4.2 (Luna) or greater: This is an integrated development
environment (IDE). Although Google has started to recommend Android
Studio as an IDE for Android development, Eclipse is still supported too.
The OpenCV library and official samples are preconfigured as Eclipse projects,
so for our purposes, Eclipse is a bit more convenient than Android Studio.
• Java Development Tools (JDT): This is an Eclipse plugin for Java
programming (already included in most Eclipse distributions).
• C/C++ Development Tooling (CDT) 8.2.0 or greater: This is an Eclipse
plugin for C/C++ programming.
[1]
www.it-ebooks.info
Setting Up OpenCV
There are many possible ways to install and configure these components. We
will cover several common setup scenarios, but if you are interested in further
options, see OpenCV's official documentation at http://docs.opencv.org/doc/
tutorials/introduction/android_binary_package/O4A_SDK.html.
System requirements
All the development tools for Android and OpenCV are cross-platform. The following
operating systems are supported with almost identical setup procedures:
To run the OpenCV samples and, later on, our own application, we should have an
Android device with the following specifications:
Android Virtual Devices (AVDs) are not recommended. Some parts of OpenCV rely
on low-level camera access and might fail with virtualized cameras.
[2]
www.it-ebooks.info
Chapter 1
Let's start by looking at the setup steps for a general-purpose Android development
environment. We will not delve into very much detail here because good instructions
are available at the given links and, as an Android or Java developer, you have
probably been through similar steps before.
[3]
www.it-ebooks.info
Setting Up OpenCV
[4]
www.it-ebooks.info
Chapter 1
[5]
www.it-ebooks.info
Setting Up OpenCV
• Git: This is a Source Control Management (SCM) tool, which we will use
to obtain OpenCV's source code. On Windows or Mac, download and install
Git from http://git-scm.com/. On Linux, install it using your package
manager. For example, on Debian or Ubuntu, open Terminal and run $ sudo
apt-get install git-core.
• CMake: This is a set of build tools. On Windows or Mac, download and
install CMake from http://www.cmake.org/cmake/resources/software.
html. On Linux, install it using your package manager. For example, on
Debian or Ubuntu, open Terminal and run $ sudo apt-get install
cmake.
• Apache Ant 1.8.0 or greater: This is a set of build tools for Java. On Linux,
just install Ant using your package manager. For example, on Debian
or Ubuntu, open Terminal and run $ sudo apt-get install ant.
On Windows or Mac, download Ant from http://ant.apache.org/
bindownload.cgi and unzip it to any destination, which we will refer to as
<ant>. Make the following changes to your environment variables:
°° Add <ant>/bin to Path (Windows) or PATH (Unix).
°° Create a variable ANT_HOME with the value <ant>.
• Python 2.6 or greater (but not 3.0 or greater): This is a scripting language
that is used by some of the OpenCV build scripts. An appropriate version
of Python comes preinstalled on Mac and most Linux systems, including
Debian and Ubuntu. On Windows, download and install Python from
http://www.python.org/getit/. If you have installed multiple versions of
Python on your system, ensure that an installation of Python 2.6 or greater
(but not 3.0 or greater) is the only one in Path (Windows) or PATH (Unix).
The OpenCV build scripts do not run properly with Python 3.0 or greater.
[6]
www.it-ebooks.info
Chapter 1
Once we have these prerequisites, we can download the OpenCV source code to any
location, which we will refer to as <opencv_source>. Then, we can build it using an
included script. Specifically, we should take the following steps:
On Windows, open Git Bash (Git's command prompt). On Mac, Debian, Ubuntu, or
other Unix-like systems, open Terminal (or another command-line shell).
The –j8 flag specifies that the make command will use 8 threads, which is typically
a good number for a quad-core processor. For a dual-core processor, a better choice
might be the –j4 flag (4 threads).
You might wonder what the cmake_android_arm.sh build script is doing. Actually,
it just creates a build directory and runs a CMake command to populate the directory
with a particular configuration of OpenCV. Here are the entire contents of the script
file:
#!/bin/sh
cd `dirname $0`/..
mkdir -p build_android_arm
cd build_android_arm
cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -
DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake $@ ../..
Advanced users, who are familiar with CMake, might want to copy and modify this
script to create a custom configuration of OpenCV. Refer to the code in <opencv_
source>/CMakeLists.txt for definitions of OpenCV's CMake options.
[7]
www.it-ebooks.info
Setting Up OpenCV
[8]
www.it-ebooks.info
Chapter 1
Now, we should see a window with several panels, including Package Explorer.
If we are not using TAPD, we need to import the OpenCV sample projects into
our new workspace. Right-click on Package Explorer and select Import… from
the context menu:
[9]
www.it-ebooks.info
Setting Up OpenCV
The Import window should appear. Navigate to General | Existing Projects into
Workspace, and then click on Next >:
On the second page of the Import window enter <opencv> in the Select root
directory: field. Under the Projects: label, a list of detected projects should appear.
(If not, click on Refresh.) The list should include the OpenCV library, samples, and
tutorials. They should all be selected by default.
[ 10 ]
www.it-ebooks.info
Chapter 1
This means that Eclipse has found the OpenCV library, samples, and tutorials and
has recognized them as Eclipse projects. Do not select Copy projects into workspace
because the OpenCV sample and tutorial projects rely on a relative path to the
library project, and this relative path will not be preserved if the projects are copied
into the workspace. Click on Finish to import the projects:
[ 11 ]
www.it-ebooks.info
Setting Up OpenCV
Once the projects are imported, we might need to fix some configuration issues.
Our development environment might have different paths and different versions
of Android SDK than the ones in the samples' default configuration:
Any resulting errors will be reported in the Problems tab. For likely solutions,
see the section Troubleshooting Eclipse projects, later in this chapter.
Once the OpenCV projects no longer show any errors, we can prepare to test them
on an Android device. Recall that the device must have Android 2.2 (Froyo) or greater
and a camera. To let Eclipse communicate with the device, we must enable the device's
USB debugging option. On the Android device, perform the following steps:
[ 12 ]
www.it-ebooks.info
Chapter 1
Now, we need to install an Android app called OpenCV Manager 3, which takes care
of checking for OpenCV library updates when we run any OpenCV application. At the
time of writing, OpenCV Manager 3 is not yet available from the Play Store. However,
in the <opencv>/apk folder of our development environment, we can find prebuilt
application bundles (.apk files) for various architectures. Choose an .apk file whose
name matches your Android device's architecture. At the time of writing, ARMv7-A
is a popular architecture for Android devices. For this architecture, OpenCV 3.0 offers
the OpenCV_3.0.0_Manager_3.00_a.apk file. Open a command prompt and enter
a command, such as the following, to install the appropriate .apk to your Android
device via USB:
$ adb install <opencv>/apk/OpenCV_3.0.0_Manager_3.00_armeabi-v7a.apk
[ 13 ]
www.it-ebooks.info
Setting Up OpenCV
Plug the Android device into your computer's USB port. In Eclipse, select one of the
OpenCV sample projects in Package Explorer. Then, from the menu system, navigate
to Run | Run as… | Android Application:
An Android Device Chooser window should appear. Your Android device should
be listed under Choose a running Android device. If the device is not listed, refer to
the section Troubleshooting the USB connection, later in this chapter.
[ 14 ]
www.it-ebooks.info
Chapter 1
If the Auto Monitor Logcat window appears, select the Yes radio button and the
verbose drop-down option, and click on OK. This option ensures that all the log
output from the application will be visible in Eclipse:
[ 15 ]
www.it-ebooks.info
Setting Up OpenCV
On the Android device, you might get a message: OpenCV library package was
not found! Try to install it? Make sure that the device is connected to the Internet
and then touch the Yes button on your device. The Play Store will open to show an
OpenCV package. Install the package and then press the hardware back button to
return to the sample application, which should be ready for use.
For OpenCV 3.0, the samples and tutorials have the following functionality:
• Sample – native-activity: This displays a camera feed using native (C++) code.
[ 16 ]
www.it-ebooks.info
Chapter 1
• Tutorial 1 – Camera Preview: This displays a camera feed. The user can
press the … menu to select a different camera feed implementation (Java or
native C++).
• Tutorial 2 – Mixed Processing: This applies filters to a camera feed using
native (C++) code. The user can press the … menu to select from a list of
filters. One of the filters draws red circles around interest points or features
in a camera feed. Generally speaking, interest points or features lie along
the high-contrast edges in an image. They are potentially useful in image
recognition and tracking applications, as we will see later in this book.
• Tutorial 3 – Camera Control: This applies filters to a camera feed, which has
a customizable resolution. The user can press the … menu to select from a list
of filters and a list of resolutions.
[ 17 ]
www.it-ebooks.info
Setting Up OpenCV
Feel free to browse the projects' source code via Package Explorer to see how they
were made. Alternatively, you might want to return to the official samples and
tutorials later, once we have built our own application over the course of this book.
Sometimes, Eclipse fails to recognize that a project needs to be rebuilt after the
project or one of its dependencies has changed (or after a dependency has been
imported). When in doubt, try cleaning all the projects by navigating to Project |
Clean… | Clean all projects | OK in the menu system. This will force Eclipse
to rebuild everything, thus ensuring that all errors, warnings, and successes are
up-to-date.
If a set of cleaned projects still has mysterious errors, then a configuration problem
might be the cause.
The target Android version might not be properly specified. The symptoms are that
imports from the java and android packages fail, and there are error messages such
as The project was not built since its build path is incomplete. The solution is to
right-click on the project in Package Explorer, select Properties from the context
menu, select the Android section, and checkmark one of the available Android
versions. These steps should be repeated for all the projects.
[ 18 ]
www.it-ebooks.info
Chapter 1
At compile time, OpenCV and its samples must target Android 3.0 (API level 11) or
greater, though at runtime they also support Android 2.2 (API level 8) or greater:
[ 19 ]
www.it-ebooks.info
Setting Up OpenCV
[ 20 ]
www.it-ebooks.info
Chapter 1
[ 21 ]
www.it-ebooks.info
Setting Up OpenCV
Many connection problems are intermittent and can be resolved by restoring the
USB connection to an initial state. Try the following steps and, after each step,
test whether the problem is resolved:
1. Unplug the Android device from the host computer's USB port.
Then, plug it back in.
2. Disable and re-enable the device's USB debugging option, as described
earlier in the section Building the OpenCV samples with Eclipse.
3. On Mac or Linux, run the following command in Terminal (or another
command prompt):
sudo sh -c "adb kill-server && start-server"
On Windows, we might need to manually install the USB drivers for the Android
device. Different vendors and devices have different drivers. The official Android
documentation provides links to the various vendors' driver download sites at
http://developer.android.com/tools/extras/oem-usb.html#Drivers.
On Linux, before connecting an Android device via USB, we might need to specify
the device's vendor in a permissions file. Each vendor has a unique ID number, as
listed in the official Android documentation at http://developer.android.com/
tools/device.html#VendorIds. We will refer to this ID number as <vendor_
id>. To create the permissions file, open a command prompt application (such as
Terminal) and run the following commands:
$ cd /etc/udev/rules.d/
$ sudo touch 51-android.rules
$ sudo chmod a+r 51-android-rules
Note that the permissions file needs to have root ownership, so we use sudo while
creating or modifying it. Now, open the file in an editor such as gedit:
$ sudo gedit 51-android-rules
For each vendor, append a new line to the file. Each of these lines should have the
following format:
SUBSYSTEM=="usb", ATTR{idVendor}=="<vendor_id>", MODE="0666",
GROUP="plugdev"
[ 22 ]
www.it-ebooks.info
Chapter 1
If the documentation does not seem to answer your question, try talking to the
OpenCV community. Here are some sites where you will find helpful people:
Summary
By now, we should have an Android and OpenCV development environment that
can do everything we need for the application described in this book's remaining
chapters. Depending on the approach we took, we might also have a set of tools
that we can use to reconfigure and rebuild OpenCV for our future needs.
We know how to build the OpenCV Android samples in Eclipse. These samples
cover a different range of functionality to this book's project, but they are useful as
additional learning aids. We also know where to find the documentation and help.
Now that we have the necessary tools and reference materials to hand, our first
ambition as application developers is to control a camera! Throughout the next
chapter, we will use Android SDK and OpenCV to preview, capture, and share
photographs.
[ 23 ]
www.it-ebooks.info
www.it-ebooks.info
Working with Camera Frames
In this chapter, we will focus on building a basic photo capture app, which uses
OpenCV to capture the frames of camera input. Our app will enable the user to
preview, save, edit, and share photos. It will interface with the other apps on the
device, via Android's MediaStore and Intent classes. Thus, we will learn how
to build bridges between OpenCV and standard Android. In subsequent chapters,
we will expand our app, using more functionality from OpenCV.
For this app, I chose the name Second Sight, a phrase that is sometimes used in
mythology to refer to supernatural and symbolic visions.
[ 25 ]
www.it-ebooks.info
Working with Camera Frames
At its core, Second Sight is a camera app. It will enable the user to preview, save, and
share photos. Like many other camera apps, it will also let the user apply filters to
the previewed and saved photos. However, many of the filters will not be traditional
photographic effects. For example, the more complex filters will enable the user to see
the stylized edges or even rendered objects that blend with the real scene (augmented
reality).
For this chapter, we will build the basic camera and sharing functions of Second
Sight, without any filters. Our first version of the app will contain two activity classes
named CameraActivity and LabActivity. The CameraActivity class will show the
preview and provide menu actions so that the user may select a camera (if the device
has multiple cameras), an image size (under the … section of the menu, if the camera
supports multiple image sizes), and take a photo. Then, the LabActivity class will
open to show the saved photo and provide the menu actions so that the user may
delete the photo or send it to another app for editing or sharing.
To get a better sense of our goal, let's look at some screenshots. Our first version of
CameraActivity will appear as follows:
[ 26 ]
www.it-ebooks.info
Chapter 2
When the user clicks on the Take Photo menu item, the LabActivity class will be
opened. It will look like the following screenshot:
When the user presses the Share menu item, an intent chooser (a dialog for choosing a
destination app) will appear over the top of the photo, as in the following screenshot:
For example, by pressing the Google+ tile, the user could open the photo in the
Google+ app, in order to share it over the social network. Thus, we have a complete
example of typical usage. With a few touch interactions, the user can snap a photo
and share it.
[ 27 ]
www.it-ebooks.info
Working with Camera Frames
Open Eclipse in a workspace that contains the library project. Then, from the menu
system, navigate to File | New | Android Application Project. The New Android
Application window should appear. Enter the options that are shown in the
following screenshot:
[ 28 ]
www.it-ebooks.info
Other documents randomly have
different content
hehkuvin harrastuksin sotatapahtumia, olimme ilosta sykkivin
sydämin katselemassa, kun valloitetut tykit tuotiin ja sotajoukot
marssivat kaupunkiin ja luulimme olevamme oikeutettuja tuntemaan,
että meissä asui samaa henkeä, joka Tanskan taistelutantereilla oli
joukoillemme tuottanut menestyksen. Oliko ihme, että sitten tuskin
maltoimme odottaa sitä päivää, jona itse pääsisimme armeijan
riveihin?
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com