logo资料库

Beginning Google Glass Development Jeff Tang.pdf

第1页 / 共358页
第2页 / 共358页
第3页 / 共358页
第4页 / 共358页
第5页 / 共358页
第6页 / 共358页
第7页 / 共358页
第8页 / 共358页
资料共358页,剩余部分请下载后查看
Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Getting Started
Why Glass
What Is Glass and Glassware ?
What Can Glass Do?
What Kinds of Glassware Can You Develop?
Mirror API–Based Glassware
GDK Glassware
When to Use Which
Google’s Glassware Policies
Why This Book?
Who Is This Book For?
For Beginning Android Developers
For Intermediate or Advanced Android Developers
For iOS Developers
For Web Developers
For Other Programmers
For Nonprogrammers
Resources
Questions and Feedback
Summary
Chapter 2: Hello, Glass! Your First GDK App
Setting Up the Development Environment
System Requirements
Android Developer Tools
ADT on Mac
ADT on Windows
ADT on Linux
Playing with Glass
Enabling Debugging Mode on Glass
The adb Command
Installing the USB Driver on Windows
Showing the Glass Screen on a Bigger Screen
Developing with Simulated Devices or Emulators
Testing with GDK Samples
The HelloGlass Glassware
Step-by-Step Tutorial
What’s Going On
The Big Picture
Source Code
AppDrawer.java
AppService.java
AppViewer.java
MenuActivity.java
Resource Files
drawable
start.xml in layout
main.xml in menu
strings.xml in values
AndroidManifest.xml
Summary
Chapter 3: Glass User Interface
Overview
Glass-Styled Cards
Menu
Live Cards
Low-Frequency Live Cards
High-Frequency Live Cards
Inflating Layout
2D Canvas Drawing
Menu
Immersion
2D Canvas Drawing
Gestures and Listeners
Menu and Head Gesture
3D OpenGL ES Drawing
OpenGL ES 1.0
OpenGL ES 2.0
When to Use Which
Theme and UI Widgets
Summary
Chapter 4: Camera and Image Processing
Taking Pictures
The Easy Way
The Custom Method
Preview
Zoom
Upload and E-mail
Photo View
Barcode Recognition
OCR
Image Web Search
OpenCV
The Complete App
Summary
Chapter 5: Video: Basics and Applications
Capturing Video
The Quick Way
The Custom Way
Playing Video
OpenCV Video Processing
FFmpeg Video Processing
Test the Library Client on Glass
Integrating the FFmpeg Library
YouTube Integration
Calling Data API
Displaying Query Result
Making the Voice Query
Playing the Video and Let’s Karaoke
Keeping Code Updated
Running the App
Summary
Chapter 6: Voice and Audio
Voice Input
Sound Effects
Audio Capture and Play
Using MediaRecorder and MediaPlayer
Using AudioRecord and AudioTrack
Improving the Karaoke App
Pitch Detection
Touchtone Detection
Song Identification
Summary
Chapter 7: Networking, Bluetooth, and Social
Making HTTP Requests
HTTP GET
HTTP POST
HTTP File Uploading
Socket Programming
Glass Client vs. Android Server
Glass Server vs. Android Client
Glass Client vs. iOS Server
Glass Server vs. iOS Client
Bluetooth
Classic Bluetooth
Bluetooth Low Energy
Reading Heart Rate from Glass
Using Android Device as Delegate
Using iOS Device as a Delegate
BLE Peripheral
Other Tips
Going Social
Summary
Chapter 8: Location, Map, and Sensors
Location
Getting Location
Showing Address
Showing and Resizing Map
Finding Places Nearby
Searching Shared Pictures
Sensors
Supported Sensors
Collecting Sensor Data
Shake It or Not
Detecting Metal
Developing a Compass
Finding the Planets
Summary
Chapter 9: Graphics, Animation, and Games
Graphics
Canvas Drawing
Drawable Shapes
Finger Painting
Bitmap Manipulation
OpenGL ES Drawing
Finger and Head Movement Support
Animation
Animating Properties in XML and Programmatically
Grouping Animation in XML and Programmatically
Using Animation Listener
Game Engines on Glass
Cocos2d-x 3.0
Porting and Running the Demo Cocos2d-x App on Glass
Creating a New Cocos2d-x App on Glass
libgdx
Creating a New libgdx App on Glass
Running and Playing libgdx Game on Glass
AndEngine
Running and Testing AndEngine Examples on Glass
An AndEngine-based Tower of Hanoi Game
Developing a Sensor-Based Game
Summary
Chapter 10: The Mirror API
Setting Up Your Environment
Using PHP
Using Java
Setting Up Eclipse
Deploying Locally and to App Engine
The Mirror API
Overview
Timeline and Static Cards
Inserting a Simple Text Card
Inserting a Card w ith an Image
Attaching Video to a Card
Adding Menu Items to a Card
Built-in Menu Items
Launching a GDK App from the Mirror API App
Custom Menu Items
Inserting an HTML Card
Getting a Card
Getting a List of Cards
Updating and Patching a Card
Pagination
Bundling
Contacts
Sharing Transcribed Speech
Subscriptions
Locations
Image Processing: The Mirror API Way
Creating and Testing a New Mirror App in Java
The NBA Roster App
Enterprise Apps
Design Principles
Summary
Before Moving On
Index
For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info
Contents at a Glance About the Author ���������������������������������������������������������������������������������������������������������������xiii About the Technical Reviewer �������������������������������������������������������������������������������������������� xv Acknowledgments ������������������������������������������������������������������������������������������������������������ xvii Introduction ����������������������������������������������������������������������������������������������������������������������� xix ■ Chapter 1: Getting Started �������������������������������������������������������������������������������������������������1 ■ Chapter 2: Hello, Glass! Your First GDK App ��������������������������������������������������������������������13 ■ Chapter 3: Glass User Interface ���������������������������������������������������������������������������������������47 ■ Chapter 4: Camera and Image Processing ����������������������������������������������������������������������81 ■ Chapter 5: Video: Basics and Applications ��������������������������������������������������������������������113 ■ Chapter 6: Voice and Audio �������������������������������������������������������������������������������������������147 ■ Chapter 7: Networking, Bluetooth, and Social ���������������������������������������������������������������175 ■ Chapter 8: Location, Map, and Sensors �������������������������������������������������������������������������215 ■ Chapter 9: Graphics, Animation, and Games �����������������������������������������������������������������249 ■ Chapter 10: The Mirror API ��������������������������������������������������������������������������������������������297 Index ���������������������������������������������������������������������������������������������������������������������������������337 www.it-ebooks.info v
Introduction Google Glass is a wearable computer developed by Google. Although Glass is based on Android, it is fundamentally different from existing mobile platforms. The best line to describe Glass is “There when you need it. Out of the way when you don’t.” Glass is designed to complement a smartwatch, smartphone, tablet, or computer. In April 2013, the Google Glass Explorer Edition was made available, for the first time, to Google I/O 2013 developers. There were four Glass-related sessions at Google I/O 2013 held in May 2013: nn Developing for Glass: https://developers.google.com/events/io/sessions/332490621 nn Building Glass Services with the Mirror API: https://developers.google.com/events/io/sessions/332733833 nn Voiding Your Warranty: Hacking Glass: https://developers.google.com/events/io/sessions/332704837 nn Fireside Chat with the Glass Team: https://developers.google.com/events/io/sessions/332695704 Since then, several important events have happened: nn On November 19, 2013, Google officially released the native Android-based Glass Development Kit (GDK) Sneak Peek, so developers can now build native apps in Android 4.0.4 (API Level 15) for Glass. nn On April 15, 2014, Google announced a major upgrade for Glass to Android 4.4.2 (API Level 19). nn On May 15, 2014, Google made the Glass Explorer Edition available to any U.S. resident older than 18 and with a U.S. shipping address. It is almost mid-June 2014 now, and Google I/O 2014 is only a couple of weeks away. More Glass-related sessions on how to design and develop Glass apps (also known as Glassware) and on the Glass platform details have been scheduled (see https://www.google.com/events/io/schedule). www.it-ebooks.info xix
Introduction xx This is a great time to start learning Glass development; if you need more reasons, see the “Why Glass?” section in Chapter 1. You should definitely check out those Google I/O 2013 and 2014 videos on Glass if you are interested in Glass development. In addition, Google Glass’s developer site at https://developers.google.com/glass/ has great documentation on designing, developing, and distributing for Glass. But none of this compares to having step-by-step tutorials with working code examples on every major Glass development topic, using both GDK and the Mirror API. That is exactly what this book is provides. What’s in This Book There are ten chapters in this book, covering every major Glass development topic. Chapter 1, “Getting Started”: In this chapter, I’ll discuss several general topics about Glass and Glassware: Why Glass? What is Glass and Glassware? What can you do with GDK Glassware and Mirror Glassware? Why this book? Who is the book for? I’ll also list popular Glass development web resources. Chapter 2, “Hello, Glass! Your First GDK App”: I’ll first cover the detailed steps of how to set up your GDK Glassware development environments, whether your favorite is Mac, Windows, or Linux. Then I’ll discuss how to set up Glass to get it ready for development and how to run sample GDK apps on it. Finally, I’ll introduce the generic template Glassware, which you’ll use to create new GDK apps, and show you a step-by-step tutorial of building HelloGlass, your first GDK app, with nice features such as menu actions, text-to-speech, and speech-recognition versions of HelloGlass. Chapter 3, “Glass User Interface”: In this chapter, you’ll enter the exciting world of Glass and learn what kinds of UI elements can be built with GDK for a Glass app. The main Glass UI elements—the timeline, Glass-styled cards, live cards, immersions, menu items, and gestures—will be discussed in detail with fully tested sample code that shows you how to render standard UI content, content from an XML layout, and content created using Android’s Canvas 2D and OpenGL ES 1.0 and 2.0. By end of this chapter, you’ll have a basic understanding of what kinds of apps you can build with GDK and when to use which or a combination of them. Chapter 4, “Camera and Image Processing”: In this chapter, you’ll start with how to use the Glass camera to take pictures, both in the easy way and in the custom way, which allows you to preview and zoom before taking a picture. Then I’ll briefly cover how to browse the photos in any directory of your Glass. After that, I’ll discuss step-by-step many common practical image-processing tasks, including barcode and QR code recognition, OCR, image web search, and OpenCV. I’ll cover how to integrate the best open source libraries out there, if needed, to your own app and how to call their APIs from within your app. By the end of this chapter, you’ll be well prepared for exploring your own great app ideas using all kinds of image-processing techniques. Chapter 5, “Video: Basics and Applications”: In this chapter, you’ll start with the basic video capture and custom video capture with preview and then look at how video can play on Glass. Then I’ll discuss how to use OpenCV to add image effects on frames extracted from video. A more powerful video-processing library, FFmpeg, will be introduced with detailed instructions of how to integrate it with your own app. Commands for various video filtering effects will be presented. Finally, a YouTube video search and play app will be covered in detail, which can be used as the foundation of a full-fledged karaoke app. www.it-ebooks.info
Introduction xxi Chapter 6, “Voice and Audio”: In this chapter, I’ll cover a lot of voice- and audio-related topics, from the standard Glass voice input, both high-level and low-level audio capture and playback, to various audio-processing examples, including musical note detection, DTMF touchtone detection, and, finally, song identification. Voice and audio are essential parts of our communication with each other and with devices, so you can expect to see many innovative apps in this area, developed by people like you. Chapter 7, “Networking, Bluetooth, and Social”: In this chapter, I’ll first cover how to implement the basic HTTP GET, POST, and file uploading operations using the recommended HttpURLConnection class. Then I’ll discuss how to accomplish low-level socket programming and let Glass talk with another Android or iOS device for data exchange, both as a client and as a server. After that, I’ll illustrate in detail how to use Classic Bluetooth for communication between Glass and another Android device, without the need of wi-fi. I’ll then introduce the exciting topic of BLE support and how to let Glass act as a BLE client and also how to use the Samsung Galaxy S4 smartphone or an iOS device as a bridge between Glass and BLE devices. Finally, I’ll show you how to use Apple’s push technology to let you share your new picture taken on Glass with your WhatsApp or WeChat friends in seconds. Chapter 8, “Location, Map, and Sensors”: In this chapter, I’ll discuss in detail how to get your current location and show its address and map, how to zoom in and out the map, and how to find nearby business information based on your location information. Then I’ll cover the eight sensors Glass supports and how to detect head movement and direction, how to detect Glass shake, how to develop a metal detector, and how to add the compass support easily to your app. Finally, I’ll outline the steps to build a planet-finder Glass app. Chapter 9, “Graphics, Animation, and Games”: In this chapter, I’ll cover common graphics and animation APIs and show many demos running on Glass, which you can use in your own simple Glass apps. Then I’ll discuss in great detail how to set up and run three popular open source game engines (Cocos2d-x, libgdx, and AndEngine) on Glass. You’ll learn how to run and interact with many examples for the three game engines, as well as how to create new apps using the engines. Finally, you’ll learn how to use the Glass rotation vector sensor to control your game with head movement. By the end of this chapter, you’ll be well armed with these powerful tools before you continue your own exciting game development journey. Chapter 10, “The Mirror API”: In this chapter, I’ll discuss in detail how to set up your environment for Mirror API app development and how to deploy the Glass Mirror API quick-start PHP project to your own server and the Java project to Google App Engine. Then I’ll go through the main building blocks of the Mirror API in detail with many examples, including timeline and static cards, contacts, subscriptions, and locations. I’ll also show you how to build a hybrid app launching the GDK app from the Mirror app and pass information from the Mirror app to the GDK app if needed. You’ll also reuse the Java image-uploading code from Chapter 7 in your Mirror app to upload a picture to a server for further processing. Finally, I’ll demonstrate a complete Mirror API app that lets you view and search for any player in the 16 NBA playoff teams. www.it-ebooks.info
Introduction xxii Before Getting Started I actually wrote this Introduction after I finished writing the book. It took me about six long months of weekday evenings and weekends to learn and get up to speed on the GDK and Mirror API, to develop and test dozens of examples that illustrate what Glass can truly do (as summarized in the previous section), to write step-by-step tutorials on how to run and use the examples, and to review and fix any known issues. No matter what your background and interests are, there should be some examples in the wide range of Glass development topics covered in the book that will inspire you and help you have a quick start on developing your own great Glass apps. I hope the hundreds of hours I spent on the book will save you a lot of time when developing for the exciting Glass platform. Have a wonderful trip in the Glass development world! If you have any questions or comments on the trip, just email me anytime at jeff.x.tang@gmail.com, and you’ll receive my response within 24 hours. www.it-ebooks.info
Getting Started Chapter 1 Welcome to the exciting new world of Google Glass! If you are reading this book, either you are most likely a Google Glass owner or you do not own Glass yet but are intrigued by it. Chances are that you are a mobile, web, or enterprise application developer looking for the next big thing and platform and trying to understand what great apps can be built on the new Glass platform. Or maybe you just want to understand the potential of Glass and see what Glass can truly do. Before you start the journey of Glass development, let’s first discuss some big questions in this chapter: Why Glass? What is Glass and Glassware? What can Glass do? What kinds of Glassware can you develop? I’ll also cover Google’s Glassware Policies and resources for Glass development at the end of this chapter. Why Glass The first generation of iPhone was released in June 2007, and Apple’s App Store was launched a year later. Android Market (renamed to Google Play later) was released in October 2008. It is absolutely amazing how many successful apps and companies have been built on and for the iOS and Android platforms. Could Google Glass be the next big thing, in terms of mobile computing platform? Nobody knows for sure. But the attention that Glass has garnered in 2013 and 2014 is bigger than any other mobile products except iPhone or iPad, so here are several questions to ask yourself:  What if it becomes a big thing and everyone seems to wear their Google Glasses in a few years like they use their iPhones or Android phones now? You probably don’t want to miss the opportunity to jump on Glass and at least learn what it takes to build apps for it. The year 2014 is definitely a great time to experiment, if you missed 2013. www.it-ebooks.info 1
分享到:
收藏