0

I want to display a camera preview in a circular shape using the camera2 api. I want to display the preview in a circular shape, but I dont't want the image to be captured in a circular shape.

The captured image would be a face( later want to implement face detection and auto capture). I did have a look at few questions already asked, but none of them are with the new camera2 api's and most of them talk about having an overlay image cropped with a transparent circle. But this will not work in a case where I need to auto detect a face(as the face may appear out side the cropped circular image).

Is there any way I can implement this ? I did try an example with TextureView and set it to a LinearLayout with fixed width and height, but the preview appeared a bit squeezed and in a square shape.

user2234
  • 1,282
  • 1
  • 21
  • 44
  • If you are using ImageView for the preview then see this answer [How to make a circular imageview](https://stackoverflow.com/questions/22105775/imageview-in-circular-through-xml) – Sayok Majumder Nov 08 '17 at 03:58

1 Answers1

3

I don't see why face detection matters here - if you enable the camera API's face detector, it'll run on the full image no matter what you do in drawing it inside a circle.

You can either use a circle overlay on top of a correctly-shaped TextureView or SurfaceView, or do your own OpenGL rendering of a circle with the camera preview as a EGL texture.

The latter you'll probably want a GLSurfaceView for the OpenGL drawing context, and a SurfaceTexture to send camera data to and expose it as a EGL texture.

JPEGs captured will still be full-FOV, and the camera API will know nothing about your circular preview drawing, so face detection and everything else will work on the full field of view.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • I didn't understand what you mean by correctly shaped TextureView, do you mean I should shape the TextureView in a circle ? Can you please give me some sample code if you know. I am stuck at making the preview appear in a circular shape. – user2234 Nov 11 '17 at 07:16
  • You can't make Android Views into circles; they are always rectangles. So either you have to make what you draw into one a circle, which requires your on OpenGL or other drawing code, or you need to overlay an ImageView or something similar with a transparent circle drawn in it on top of your TextureView. – Eddy Talvala Nov 13 '17 at 17:42
  • I followed a similar approach, I set the camera preview at the background and overlay an image with a transparent circle and it worked all good. Thanks much! – user2234 Nov 15 '17 at 22:48
  • Can you please suggest, i want to capture user face only when it comes inside circle. – Chetan Chaudhari May 13 '19 at 09:21