Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Camera preview using SurfaceTexture in Android

Tags:

android

I am trying to render camera preview using SurfaceTexture. I read the document but unable to understand how it works.

Can anyone provide one sample example(very basic one) or link which uses SurfaceTexture to preview camera. I googled this but not found what I am looking for.

Thanks in advance.

like image 675
AndroDev Avatar asked Jul 18 '12 10:07

AndroDev


People also ask

What is camera preview in android?

PreviewView — Implementation modes To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.

What is preview in camera?

The camera image is rotated to align with the app UI, cropped to match the aspect ratio of the camera preview, and then scaled to fill the preview. Inset portrait mode is triggered when the aspect ratio of the camera image sensor and the aspect ratio of the application's primary activity do not match.

How do I change camera preview orientation on android?

To force portrait orientation: set android:screenOrientation="portrait" in your AndroidManifest. xml and call camera. setDisplayOrientation(90); before calling camera.


1 Answers

If you want to use the Camera with TextureSurface you can implement SurfaceTextureListener interface. You'll have to implement 4 methods:

1) onSurfaceTextureAvailable - Here you setup your camera

2)onSurfaceTextureSizeChanged - In your case, the Android's camera will handle this method

3)onSurfaceTextureDestroyed - Here you destroy all camera stuff.

4) onSurfaceTextureUpdated- Update your texture here when you have something to change!

Check the example below:

    public class MainActivity extends Activity implements SurfaceTextureListener{      private Camera mCamera;     private TextureView mTextureView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          mTextureView = new TextureView(this);         mTextureView.setSurfaceTextureListener(this);          setContentView(mTextureView);     }      @Override     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {         mCamera = Camera.open();          Camera.Size previewSize = mCamera.getParameters().getPreviewSize();         mTextureView.setLayoutParams(new FrameLayout.LayoutParams(                 previewSize.width, previewSize.height, Gravity.CENTER));          try {             mCamera.setPreviewTexture(surface);         } catch (IOException t) {         }          mCamera.startPreview();      }      @Override     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {         // Ignored, the Camera does all the work for us     }      @Override     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {         mCamera.stopPreview();         mCamera.release();         return true;     }      @Override     public void onSurfaceTextureUpdated(SurfaceTexture surface) {         // Update your view here!     } } 

Two more things: Don't forget to add the camera permissions in your project's manifest and the SurfaceTexture is available from API 11.

like image 111
Thiago M Rocha Avatar answered Oct 17 '22 13:10

Thiago M Rocha