fadedreamz
fadedreamz

Reputation: 1156

Custom Camera view

I am facing difficulties in implementing a custom camera view.

The task I want to achieve is - in camera preview I got a preview picture (in previewcallback function). I zoom it and then I want to draw the zoomed version of the image in the camera view.

In previewcallback function I got image pixel values as byte array. But in canvas.drawImage it accepts int array. Can anyone suggest me how does the pixel data is organized in the byte array within the previewcallback function?

I tried to lock the surfaceview but it didn't help. It returns null so I cannot draw anything on that surface.

Any suggestions? thanks.

Upvotes: 0

Views: 418

Answers (2)

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

As said previously, NV21 is default and has to be provided always - this means you can rely on it and you shal rely on it if you like to be compatible with other devices. As for locking surface view - surface view you are using for preview is off limits for you since it is already locked by camera applciation ( which is nativeand runs in own process ) - if you like to use it, you will have to stop preview on it so camera app releases this surface ( hopefully )

Bad news is that you can not place another surface view on top of the first with frame layout ( this is limitation of surface views, they can not overlap ), but good news is that you can place image view oevr it and draw your bitmap there ( via runOnuiThread() )

You can find example how to do this in our javaOCR project

( see android demos, please use git repo as it is current )

Upvotes: 1

Eddy Talvala
Eddy Talvala

Reputation: 18107

The way the pixel data from the preview callback is organized depends on what your preview format is set to. By default, it is NV21, which is the most compatible option across Android devices. You can find more detail about this format here, for example.

But roughly, the image data is in a YCbCr format - not the more common RGB color encoding that canvas would want. You'll need to

  1. Convert the YCbCr byte values into RGB values (the formula can be found at Wikipedia)
  2. Upsample the Cb and Cr (color) channels into full resolution; they have been reduced to 1/2 the width and height of the Y (luminance) channel. The simplest option is to use nearest-neighbor upsampling - use the same Cb and Cr values for 4 different Y values. So you'd pair pixel (0,0) of the Cb and Cr channels with pixels (0,0), (1,0), (0,1), and (1,1) of the Y channel to form the full-resolution image.

Upvotes: 2

Related Questions