16

I have been trying to figure out if there is a way to use camera to take either video/pictures without defining surfaceview or textureview. I found this link: Use Android Camera without surface View

I used this trick with textureview on my nexus tablet but no luck! Also, http://handycodeworks.com/?p=19 says that this approach doesn't work on all the devices.

Does anyone have any idea if there is a way to do it at all? or its just something which android framework doesn't support at all and the GUI has to have some surface/texture element in layout? Then the only option is to just manipulate the layout so its not visible on the screen as per the app requirements.

EDIT 1: As explained in the above link http://handycodeworks.com/?p=19, I tried the below code:

public class CameraCapture {

  // I pass the getApplicationContext() from the main activity.
  public void startCameraCapture(Context contx) {
    SurfaceView sv = new SurfaceView(contx);
    mCamera = Camera.open();
    mCamera.setPreviewDisplay(sv.getHolder());
    mCamera.setPreviewCallback(new PreviewCallback() {
      @Override
      public void onPreviewFrame(byte[] data, Camera camera) {
        Log.v("TAG", "on preview frame called");
      }
    Thread.sleep(1000);
    mCamera.startPreview();
}

However, onPreviewFrame() never invoked. Am I missing something?

EDIT 2:

Is it possible to do in native code? Capturing video/using camera without GUI element (surfaceview/textureview) using OpenCV? I looked at this link: http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization. However, they also show the sample code with some camera view element defined in main layout xml file.

Community
  • 1
  • 1
pree
  • 2,297
  • 6
  • 37
  • 55
  • Possible duplicate of [How to take pictures from the camera without preview when my app starts?](http://stackoverflow.com/questions/20684553/how-to-take-pictures-from-the-camera-without-preview-when-my-app-starts) – GSerg Apr 08 '16 at 07:52

2 Answers2

12

I'm able to do it by defining SurfaceTexture object as below:

SurfaceTexture surfaceTexture = new SurfaceTexture(10);
<camera object>.setPreviewTexture(surfaceTexture);

In this, I don't need to define a GUI element in the app.

pree
  • 2,297
  • 6
  • 37
  • 55
  • 3
    Does the number matter? OpenGL does care about the texture numbering you use, and I'd think the thread you're on is significant as well. – Andrew Wyld Dec 22 '15 at 12:56
5

This comes up periodically.

It depends on what API level you're targeting. API 11 (Honeycomb) introduced the SurfaceTexture class, which directs incoming frames to a GLES texture rather than a visible window. You can see it in used in CameraToMpegTest, which does a "headless" recording of video to .mp4 (requires API 18 for all the video stuff).

If you're targeting 2.3.x, you will need a different solution.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • I'm not sure but the 2nd link given in my question provides the same solution as the 1st link in your response. Isn't it? If that is true, not sure if it will work on all the devices. Also, I tried setting onPreviewFrame() in addition to this approach which got never invoked. Any idea about that? – pree Mar 18 '14 at 02:26
  • 2
    Yes... somebody found a solution that worked sometimes and everybody has followed it. If you look through the comments on the blog posting you'll note a few entries that said it worked reliably if they added a delay, which makes me think it's failing because they're not giving the SurfaceView time to create the actual Surface. (Normally you wait for surfaceCreated() to fire before using the Surface.) – fadden Mar 18 '14 at 04:06