Friday, December 12, 2014

A Tutorial on android.hardware.camera2.*


Since is deprecated after API V21, we use android.hardware.camera2.*. The related reference is . But this is a kind of a dictionary. We need simple tutorials. There is no good sample code even in .

After long googling I found good one : . And I made my own document focused on only Camera Preview.

All of my code is here :

Setup All of related objects

The following list is as follows:

  • CameraManager
    • Select Camera
    • Create CameraDevice
  • CameraDevice
    • Create CaptureRequest
    • Create CameraCaptureSession
  • CaptureRequest, CaptureRequest.CameraBuilder
    • Link Surface for Viewing
    • Make CaptureRequest
  • CameraCaptureSession
    • Capture Camare Image and put the result on the Surface registered in CaptureRequest.

One of reasons why the related objects are a little bit complex is most of set-up process is done asynchronously. For example when we create CameraDevice, at first we register a callback function on CameraManager. When CameraDevice is created and the callback is called, we get CameraDevice in the callback code.

Repeated Capture by using HandlerThread

Main thread should not be blocked by Camera Capture. So we set up a background thread using HandlerThread and Handler

This handler in background thread do repeatedly Capturing set by CaptureRequest and CameraCaptureSession.

11 comments: said...

are you able to take a picture and save it to a directory

Unknown said...

In Android Studio, I found a sample program (File->Import Sample) that probably does the same basic things as this. I had trouble getting your code to work, thought I didn't put much effort into it since I found the sample while trying to figure out how to import your code. It just relays the camera to a Fragment.

However, camera2 doesn't work on my development phone, a Galaxy 1, which reports SDK 16 and camera2 appears to need SDK 21.

Alfa said...

Thanks, I found the process diagrams useful in understanding the components involved in camera2

Alfa said...

Thanks, I found the process diagrams useful in understanding the components involved in camera2

Sushil Das said...

Android Chat Head Library
Chat head is new feature which floats on screen instead of residing inside conventional application. This feature is very continent for multitasking as user can work and chat at the same time.
Android Chat Head Library

Unknown said...

I went to your github account and there are 2 samples : 1 which discribe how to work with FaceDetetion (CameraAPI) and 2 which discribe how to work with Preview (Camera2API), but i looking for solution how implement face detection in Preview camera2API... As far as i understand now it's impossible... Maybe you can suggest me how to implement it? Sorry for my english

Jae-Young Lee said...

Sorry, I'm late. Unfortunately I made FaceDetection sample using CameraAPI v1. and I don't have any answer about face detection implementation based on CameraAPI v2. I have not developed any Android App for a long time.

How about using Vision Library? As I know Computer Vision Libraries such as MetaIO support face detection feature and Android SDK. However as my experience you cannot use their API freely in the way you want. Their implementation is provided as binaries and for example you cannot integrate their API with Android native camera preview system.

Sankar said...

How can I get actual size image, when I save image it gets compressed.

Waled Mohamed said...

برنامج شئون الطلاب

Waled Mohamed said...

"مكاتب الاستقدام فى السعودية

Mr. Waled Mohamed said...

برنامج محاسبة شركات المقاولات