Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - use camera without surfaceview or textureview

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.

like image 302
pree Avatar asked Mar 17 '14 18:03

pree


People also ask

How to set camera preview orientation in Android?

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

What is camera preview in Android?

PreviewView is a subclass of FrameLayout . 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?

Live preview is a feature that allows a digital camera's display screen to be used as a viewfinder. This provides a means of previewing framing and other exposure before taking the photograph.

What is TextureView in Android?

The TextureView class is a view object that combines a view with a SurfaceTexture.


2 Answers

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.

like image 69
pree Avatar answered Oct 05 '22 23:10

pree


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.

like image 27
fadden Avatar answered Oct 06 '22 00:10

fadden