Top Banner
AN INTRODUCTION TO ANDROID DEVELOPMENT CS231M | Alejandro Troccoli
22

AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Aug 16, 2018

Download

Documents

vuongthuan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

AN INTRODUCTION TO ANDROID DEVELOPMENTCS231M | Alejandro Troccoli

Page 2: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Outline

Running task on separate threads

Calling native code from your application

Introduction to the Camera2 API.

Page 3: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Long running task

Long running tasks on the main thread can block the UI

App looks unresponsive

Page 4: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Use a separate Thread instead

Page 5: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Use Handlers to update UI

Page 6: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Add a Progress dialog

Page 7: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Outline

Running task on separate threads

Calling native code from your application

Introduction to the Camera2 API.

Page 8: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Adding native code: Java Native Interface

In the Java class, add a method without implementation and the native prefix

Create the jni headers:

> javah –d jni –classpath .\bin\classes edu.stanford.cs231m.helloandroid.HelloAndroidActivity

Page 9: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Adding native support

Right-click on project -> Android Tools -> Add Native Support

Page 10: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Android.mk

Makefile for NDK

Page 11: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

HelloAndroid.cpp

Page 12: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Let’s run it!

Modify the Java code to call square and run the app…

Page 13: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Unfortunately, HelloAndroid has stopped.

Need to load the native library into the Java virtual machine!

Page 14: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Application.mk

Makefile options that are applied to all modules!

Target ABI

Choice of STL implementation

Global compiler options..

Page 15: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Debugging Native Code

Enable debug build with NDK_DEBUG

Page 16: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Launch: Debug as Native app

Need to wait

for debugger to attach

Page 17: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Little trick to wait for debugger

Android.mk

Define waitForDebugger() and

insert a call to wait in your

program. Once the debugger

attaches, pause the program and

set _debug to 0.

Page 18: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Outline

Running task on separate threads

Calling native code from your application

Introduction to the Camera2 API.

Page 19: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

Camera 2 API

See Lecture 3 tutorial on wiki

Understand:

CameraManager

CameraDevice

CameraCaptureSession

Request

Page 20: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

CameraManager

Lists available cameras in the system

Provides access to cameras properties

Allows you to open cameras

Note: Need to add CAMERA permission in AndroidManifest.xml to

open a camera.

Page 21: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

CameraDevice

Gives you exclusive access to a camera.

Allows you to create a CameraCaptureSession.

Release the camera device when you are done with it!

Page 22: AN INTRODUCTION TO ANDROID DEVELOPMENT - … · Outline Running task on separate threads Calling native code from your application Introduction to the Camera2 API.

CameraCaptureSession

Sets up the image capture data flow.

Queues requests to the camera system

Single

Bursts