Introduction to Android
Programming
Xin Yang
2016-05-06
Outline
• Utilizing Camera Hardware in Your App
• Drawing 2D Graphics on Screen
2
Using Camera in Android App
• Two ways to invoke
– Use existing camera app via Intent
Minimal coding, limited design flexibility
– Implement your own using Camera API
More coding, customized interface and features
Use Existing Camera Apps via Intent
• Intent
– A messaging object which facilitates communication
between activities
Application intent
Intent.putExtra(key, data) getIntent()
new Intent (Activity_A.class) Intent.getExtra()
Main Activity Activity_A
4
http://developer.android.com/guide/components/intents-filters.html
Intent
• Intent Types
– Explicit intents: specify component to start by name. It is
used to start component in your own app.
– Implicit intents: specify component by declaring general
action to perform.
Intent Intent
intent.setAction(Intent.ACTI All Apps
ON_VIEW);
Create Intent startActivity() Search Intent onCreate()
Activity A Android System Activity B
5
Fig. Illustration of how an implicit intent is delivered to start another activity
Using Existing Camera Apps
• Compose a Camera Intent
– MediaStore.ACTION_IMAGE_CAPTURE
– MediaStore.ACTION_VIDEO_CAPTURE
• Start the Camera Intent
– StartActivityForResult()
• Receive the Intent Result
– onActivityResult()
6
Example Code: Step 1 & 2
Intent action type for
requesting an image from
// create Intent to1. take a picture
Compose and return control to
a camera intent
an existing camera app
the calling application
Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
File tempFile = File.creatTempFile(“cameraImg”, “.jpg”);
Uri fileUri = Uri.fromeFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
2. Start the camera intent and display camera app interface
//start the image capture intent
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
7
Exampe Code: Step 3
3. Receive the intent result (i.e. callback and data)
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//display image
Bitmap img = (Bitmap) data.getExtras().get(“data”);
ImageView imageTakePic =
(ImageView)findViewById(R.id.imageView);
imageTakePic.setImageBitmap(img);
}
else if(resultCode == RESULT_CANCELLED){
//User cancelled the image capture
}
}
8
Build Your Own Camera App using Camera API
• General steps to build a camera app
1. Detect and access camera
⁻ <uses-feature android:name=“android.hardware.camera“
android:required =“true”/>
⁻ <uses-permssion
android:name=“android.permission.CAMERA”>
⁻ Check at runtime: PackageManager.hasSystemFeature
(PackageManager.FEATURE_CAMERA))
⁻ Camera.open()
http://developer.android.com/guide/topics/media/camera.html#custom-camera 9
Detecting Camera Hardware
• If your app does not specifically require a camera
using manifest declaration, you should check a
camera is available at runtime
10
Accessing Cameras
• Access a camera by getting instance of Camera object
11
Customize Camera Interface using Camera API
• General steps to build a camera app
1. Detect and access camera
2. Create a camera preview class
⁻ SurfaceView : display the live image data
⁻ SurfaceHolder.Callback: capture the callback events for
creating and destroying the view
12
Creating a Preview Class
13
14
Customize Camera Interface using Camera API
• General steps to build a camera app
1. Detect and access camera Preview Layout
2. Create a camera preview class
3. Build a preview layout Camera
Preview
Button
15
Placing Preview in a Layout
• Camera preview class must be placed in the layout of
an activity along with UI controls for taking pictures
FrameLayout element is a container
for camera preview class
16
17
Customize Camera Interface using Camera API
• General steps to build a camera app
1. Detect and access camera
2. Create a camera preview class
3. Build a preview layout
4. Setup listeners for capture
⁻ Call Camera.takePicture()
18
Capturing Pictures
• To retrieve a picture, use Camera.takePicture() method
– Implements Camera.PictureCallback interface to receive data
19
Adding a Listener to the Capture Button
20
Customize Camera Interface using Camera API
• General steps to build a camera app
1. Detect and access camera
2. Create a camera preview class
3. Build a preview layout
4. Setup listeners for capture
5. Capture and save file
6. Release the camera
http://developer.android.com/guide/topics/media/camera.html#custom-camera 21
2D Graphics Drawing
Screen
• Where to draw and How to draw?
Window2 Window1
• Terminologies Surface Surface
(rendering buffer) (rendering buffer)
– Surface
• An object (i.e. rending buffer) in which contents
of a window (e.g. dialog, full-screen activity,
status bar) is rendered
• Every window has its own surface
• It has more than one buffer for double-buffered
rendering
http://source.android.com/devices/graphics.html 22
2D Graphics Drawing
• Terminologies Window
– Canvas Surface
(rendering buffer)
• An interface to Surface upon which your
graphics will be drawn
• It provides a set of drawing methods, e.g.
drawBitmap(), drawCircle(), drawPath() etc
• Each Canvas maps to a Bitmap to store the
content on the surface
23
2D Graphics Drawing
• Terminologies Window
– View View
Root
• An interactive UI element (e.g. ImageView)
inside of window
View View
• View objects within a window are organized by
Surface
view hierarchy and share a single surface
(rendering buffer)
– SurfaceView
• A special implementation of View that creates its
own dedicated Surface to directly draw into
24
2D Graphics Drawing
• Two ways to draw 2D graphics
– Draw with a canvas on a View
– Draw with a canvas on a SurfaceView
http://developer.android.com/guide/topics/graphics/2d-graphics.html 25
Draw on a View
Window
View
Root
Surface
Canvas (rendering buffer)
View View with
Bitmap
• Go through view hierarchy drawing process
• For apps which do not require frequent redraw
26
Class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLUE);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(10, 10, 90, 10, paint);
}
}
Will be called when we call DrawView.invalidate().
27
Layout file include DrawView
(activity_draw_view.xml)
<View
class=“DrawView"
android:id ="@+id/drawing_area"
android:layout_width ="match_parent"
android:layout_height ="match_parent" />
<Button
android:onClick="redraw"
android:layout_width="match_parent"
android:layout_height="wrap_content”/>
28
DrawActivity
Class DrawActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_view);
mDrawingArea =
(DrawView) findViewById(R.id.drawing_area);
//handle events for button
void redraw(){
mDrawingArea.invalidate();
}
}
} 29
Main Activity
Class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//handle events for button
{
Intent intent = new Intent(this, DrawActivity.class);
startActivity(intent);
}
}
}
30
Draw on a SurfaceView
• Has dedicated surface, does not need to go
through View hierarchy drawing process
• For apps which require frequent redraw
http://developer.android.com/guide/topics/graphics/2d-graphics.html 31
Class DrawSurfaceView extends SurfaceView
implements SurfaceHolder.Callback{
SurfaceHolder mHolder;
DrawSuraceView(Context context){
mHolder = getHolder;
mHolder.addCallback(this);
}
void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = mHolder.lockCanvas();
canvas.drawCircle(100, 200, 50, paint);
mHolder.unlockCanvasAndPost(canvas); }
}
32
Questions???
33