28

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,096
  • 7
  • 33
  • 48

2 Answers2

63

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,396
  • 1
  • 17
  • 25
  • 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
-3
public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

recorder = new MediaRecorder();
initRecorder();

SurfaceView cameraView = new SurfaceView(this);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

cameraView.setClickable(true);
cameraView.setOnClickListener(this);
}

private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile cpHigh = CamcorderProfile
        .get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}

private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());

try {
    recorder.prepare();
} catch (IllegalStateException e) {
    e.printStackTrace();
    finish();
} catch (IOException e) {
    e.printStackTrace();
    finish();
}
}

public void onClick(View v) {
if (recording) {
    recorder.stop();
    recording = false;

    // Let's initRecorder so we can record again
    initRecorder();
    prepareRecorder();
} else {
    recording = true;
    recorder.start();
}
}

public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
    recorder.stop();
    recording = false;
}
recorder.release();
finish();
}
}
Kamal
  • 1,433
  • 13
  • 24
  • 2
    Thank you for your answer but this is not what I am looking for. This example is already available in api demos. I want to do the same thing using SurfaceTexture. – AndroDev Jul 18 '12 at 10:43