29

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.

AndroDev
  • 3,236
  • 8
  • 35
  • 49

1 Answers1

65

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.

Thiago M Rocha
  • 1,416
  • 1
  • 20
  • 26
  • Could you please guide me on using this in service ? – someone Jul 17 '14 at 18:56
  • @Rhth, what do you mean by using this is service? – Thiago M Rocha Jul 18 '14 at 12:46
  • @Rhth, I suggest you to post your question on StackOverflow instead of asking it in the comments. – Thiago M Rocha Jul 18 '14 at 17:04
  • I have posted question could you please look into [my problem](http://stackoverflow.com/questions/24831267/taking-picture-from-non-activty) @ThiagoMRocha – someone Jul 18 '14 at 18:10
  • This was exceptionally helpful, thank you! One question though, how do you go about making the entire surfaceTexture invisible/ not seeable by the user so that it cannot be seen at all? – PGMacDesign Oct 13 '14 at 16:59
  • @Silmarilos did you try to set the surface texture visibility to GONE? – Thiago M Rocha Oct 13 '14 at 17:50
  • @Thiago , I am attempting that right now: is this the correct way to do so? "mTextureView.setVisibility(0);" – PGMacDesign Oct 13 '14 at 18:33
  • @Silmarilos yep. Try using the own android constants for a better code legibility. I.E: "mTextureView.setVisibility(View.GONE);" or "mTextureView.setVisibility(View.VISIBLE);" – Thiago M Rocha Oct 13 '14 at 18:39
  • Excellent, Thank you. The code that worked best seems to be: mTextureView.setVisibility(View.GONE); – PGMacDesign Oct 13 '14 at 18:51
  • @ThiagoMRocha where does onSurfaceTextureAvailable() get the width and height from? It seems to be giving me the incorrect width and height (That is, I want to use the maximum camera resolution for my view) but it's returning something much lower. Why? – Euridice01 Dec 10 '16 at 03:54