Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

Tech Guides - Android Programming

18 Articles
article-image-opencv-and-android-making-your-apps-see
Raka Mahesa
07 Jul 2016
6 min read
Save for later

OpenCV and Android: Making Your Apps See

Raka Mahesa
07 Jul 2016
6 min read
Computer vision might sound like an exotic term, but it's actually a piece of technology that you can easily find in your daily life. You know how Facebook can automatically tag your friends in a photo? That's computer vision. Have you ever tried Google Image Search? That's computer vision too. Even the QR Code reader app in your phone employs some sort of computer vision technology. Fortunately, you don't have to conduct your own researches to implement computer vision, since that technology is easily accessible in the form of SDKs and libraries. OpenCV is one of those libraries, and it's open source too. OpenCV focuses on real-time computer vision, so it feels very natural when the library is extended to Android, a device that usually has a camera built in. However, if you're looking to implement OpenCV in your app, you will find the official documentations for the Android version a bit lagging behind the ever evolving Android development environment. But don't worry; this post will help you with that. Together we're going to add the OpenCV Android library and use some of its basic functions on your app. Requirements Before you get started, let’s make sure you have all the following requirements: Android Studio v1.2 or above Android 4.4 (API 19) SDK or above OpenCV for Android library v3.1 or above An Android device with a camera Importing the OpenCV Library All right, let's get started. Once you have downloaded the OpenCV library, extract it and you will find a folder named "sdk" in it. This "sdk" folder should contain folders called "java" and "native". Remember the location of these 2 folders, since we will get back to them soon enough. So now you need to create a new project with blank activity on Android Studio. Make sure to set the minimum required SDK to API 19, which is the lowest version that's compatible with the library. Next, import the OpenCV library. Open the File > New > Import Module... menu and point it to the "java" folder mentioned earlier, which will automatically copy the Java library to your project folder. Now that you have added the library as a module, you need to link the Android project to the module. Open the File > Project Structure... menu and select app. On the dependencies tab, press the + button, choose Module Dependency, and select the OpenCV module on the list that pops up. Next, you need to make sure that the module will be built with the same setting as your app. Open the build.gradle scripts for both the app and the OpenCV module. Copy the SDK version and tools version values in the app graddle script to the OpenCV graddle script. Once it's done, sync the gradle scripts and rebuild the project. Here are the values of my graddle script, but your script may differ based on the SDK version you used. compileSdkVersion 23 buildToolsVersion "23.0.0 rc2" defaultConfig { minSdkVersion 19 targetSdkVersion 23 } To finish importing OpenCV, you need to add the C++ libraries to the project. Remember the "native" folder mentioned earlier? There should be a folder named "libs" inside it. Copy the "libs" folder to the <project-name>/OpenCVLibrary/src/main folder and rename it to "jniLibs" so that Android Studio will know that the files inside that folder are C++ libraries. Sync the project again, and now OpenCV should have been imported properly to your project. Accessing the Camera Now that you’re done importing the library, it's time for the next step: accessing the device's camera. The OpenCV library has its own camera UI that you can use to easily access the camera data, so let’s use that. To do that, simply replace the layout XML file for your main activity with this one. Then you'll need to ask permission from the user to access the camera. Add the following line to the app manifest. <uses-permission android_name="android.permission.CAMERA"/> And if you're building for Android 6.0 (API 23), you will need to ask for permission inside the app. Add the following line to the onCreate() function of your main activity to ask for permission. requestPermissions(new String[] { Manifest.permission.CAMERA }, 1); There are two things to note about the camera UI from the library. First, by default, it will not show anything unless it's activated in the app by calling the enableView() function. And second, on portrait orientation, the camera will display a rotated view. Fixing this last issue is quite a hassle, so let’s just choose to lock the app to landscape orientation. Using OpenCV Library With the preparation out of the way, let's start actually using the library. Here's the code for the app's main activity if you want to see how the final version works. To use the library, initialize it by calling the OpenCVLoader.initAsync() method on the activity's onResume() method. This way the activity will always check if the OpenCV library has been initialized every time the app is going to use it. //Create callback protected LoaderCallbackInterface mCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { //If not success, call base method if (status != LoaderCallbackInterface.SUCCESS) super.onManagerConnected(status); else { //Enable camera if connected to library if (mCamera != null) mCamera.enableView(); } } }; @Override protected void onResume() { //Super super.onResume(); //Try to init OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mCallback); } The initialization process will check if your phone already has the full OpenCV library. If it doesn't, it will automatically open the Google Play page for the OpenCV Manager app and ask the user to install it. And if OpenCV has been initialized, it simply activates the camera for further use.   If you noticed, the activity implements the CvCameraViewListener2 interface. This interface enables you to access the onCameraFrame() method, which is a function that allows you to read what image the camera is capturing, and to return what image the interface should be showing. Let's try a simple image processing and show it on the screen. @Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { //Get edge from the image Mat result = new Mat(); Imgproc.Canny(inputFrame.rgba(), result, 70, 100); //Return result return result; } Imgproc.Canny() is an OpenCV function that does Canny Edge Detection, which is a process to detect all edges in a picture. As you can see, it's pretty simple; you simply need to put the image from the camera (inputFrame.rgba()) into the function and it will return another image that shows only the edges. Here's what the app’s display will look like. And that's it! You've implemented a pretty basic feature from the OpenCV library on an Android app. There are still many image processing features that the library has, so check out this exhaustive list of features for more. Good luck! About the author Raka Mahesa is a game developer at Chocoarts who is interested in digital technology in general. Outside of work hours, he likes to work on his own projects, with Corridoom VR being his latest released game. Raka also regularly tweets as @legacy99.
Read more
  • 0
  • 0
  • 7234

article-image-android-your-mobile-platform-choice
Richard Gall
21 Mar 2016
2 min read
Save for later

Android: Your Mobile Platform of Choice

Richard Gall
21 Mar 2016
2 min read
It’s been a long week of argument and debate, strong words and opinions – and that’s just in the Packt office. But, now the votes have been counted we can announce that Android is the Packt customer’s mobile platform of choice. Across oour website poll and our Twitter poll, Android was the clear winner. Throughout the week, it also proved to be the most popular platform with customers, with sales of our Android eBooks exceeding those for iOS.  As you can see, our Twitter poll, delivered a particularly significant win for Android. Clearly there was a lot of love for Android. But what we really loved about the week was hearing some interesting perspectives from mobile developers around the world. This tweet in particular summed up why we think Android dominated the vote: Fundamentally, it’s all about customization – with Android you have more freedom as a developer, which, for many developers is central to the sheer pleasure of the development experience. Of course, the freedom you get with Android is only a certain type of freedom – and there are, of course trade-offs if you want the openness of such a platform. This article from October 2015 suggested that Android development is ‘30% more expensive than iOS development’ due to the longer amount of time Android projects take – the writers estimate that, on average, you write 40% more code when working with Android over iOS. But with new tools on the horizon likely to make Android development even more efficient (after all, think about what it was like to build for Android back in 2013!), it’s unsurprising that it should prove so popular with many developers. We’re celebrating Android’s win with an additional week of offers – which means you’ve now got another week to pick up our very best Android titles and get ready for a bright and exciting future in the mobile development world!
Read more
  • 0
  • 0
  • 1743

article-image-why-android-will-rule-our-future
Sam Wood
11 Mar 2016
5 min read
Save for later

Why Android Will Rule Our Future

Sam Wood
11 Mar 2016
5 min read
We've been joking for years that in the future we'll be ruled by Android Overlords - we just didn't think it would be an operating system. In 2015, it's predicted that Android shipped over one billion devices - a share of the mobile market equating to almost 80%. In our 2015 Skill Up survey, we also discovered that Android developers were by far the highest paid of mobile application developers. Android dominates our present - so why is it likely going to be vital to the world of tomorrow too? IoT Will Run On Android (Probably) Ask any group of developers what the Next Big Thing will be, and I bet you that more than one of them is going to say Internet of Things. In 2015, Google announced Android stepping into the ring of IoT operating systems when it showed us Brillo. Based on the Android kernal but 'scrubbed down by a Brillo pad', Brillo offers the possibility of a Google-backed cohesive platform for IoT - something potentially vital to a tech innovation increasingly marred by small companies attempting to blaze their own trail off in different directions. If IoT needs to be standardized, what better solution than with Android, the operating system that's already the go-to choice for open-source mobile devices? We've already got Smart Fridges running on Android, smart cars running on Android, and tons of smart-watches running on Android - the rest of the Internet of Things is likely just around the corner. Android is Colonizing Desktop Microsoft is still the King of Desktop, and Windows isn't going anywhere any time soon. However, its attempts to enter the mobile space have been miserable-at-best - a 2.8% share of the mobile market in 2015. What has been more successful is the idea of hybridizing the desktop and the mobile, in particular with the successful line of Surface laptops-come-tablets. But is the reverse likely to happen? Just like we're seeing Android moving from being a mobile OS to being used for IoT, we're also seeing the rise of ideas of Android Desktop. The Remix OS for PC operating system is created by former Google developers, and promises an "Android for PC" experience. Google-proper's own experiments in desktop are currently all based on the Chrome OS - which is growing fast in its market share, particularly among the education and student sectors. I'm an enthusiastic Chromebook owner and user, and when it falls short of meeting the full requirements of a major desktop OS, I'll often turn to my Android device to bridge the gap. According to the Wall Street journal, Google may be thinking similar and is considering folding Chrome OS and Android into one product. Consider the general praise that Microsoft received for Windows 10 mobile, and the successful unification of their platforms under a single OS. It's easy to imagine the integration of Google's mobile and desktop projects into a similar single user experience - and that this hybrid-Android would make a serious impact in the marketplace. For Apple, the Only Way Is Down Apple has banked on being the market in luxury for its mobile devices - and that might spell its doom. The pool of new buyers in the smartphone market is shrinking, and those late adopters are more likely to be price-conscious and enamored with the cheaper options available on Android. (After all, if your grandmother still complains about how much milk costs these days, is she really going to want to shell out $650 for an iPhone?) If Apple wants a bigger share of the market, it's going to need to consider a 'budget option' - and as any brand consultant will tell you, nothing damages the image of luxury like the idea that there's a 'cheap version'. Apple is aware of this, and has historically protested that it's never happening. But in 2015, we saw the number people switching from Android to iOS fall from from 13% to 11%. Even larger, the number of first-time smartphone buyers contributing to Apple's overall sales went from 20% to 11% over the same period. Those are worrying figures - especially when it also looks like more people switched from iOS to Android, than switched from Android to iOS. Apple may be a little damned-if-it-does, damned-if-it-doesn't in the face of Android. You can get a lot for your money if you're willing to buy something which doesn't carry an Apple logo. It's easy to see Android's many producers creating high-powered luxury devices; it's harder to see Apple succeeding by doing the opposite. And are we really ever going to see something like the iFridge? Android's Strength is its Ubiquity Principal to Android's success in the future is its ubiquity. In just six years, it's gone from being a new and experimental venture to over a billion downloads and being used across almost every kind of smart device out there. As an open source OS, the possibilities of Android are only going to get wider. When Androids rule our future, it may be on far more than just our phones. Dive into developing for Android all this week with our exclusive Android Week deals! Get 50% off selected titles, or build your own bundle of any five promoted Android books for only $50.
Read more
  • 0
  • 0
  • 2489
Banner background image
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime