Taking picture from camera without preview

前端 未结 9 1187
北荒
北荒 2020-11-22 13:01

I am writing an Android 1.5 application which starts just after boot-up. This is a Service and should take a picture without preview. This app will log the ligh

相关标签:
9条回答
  • 2020-11-22 13:21

    In the "Working Example by Sam" (Thank you Sam... )

    if at istruction "wm.addView(preview, params);"

    obtain exception "Unable to add window android.view.ViewRoot -- permission denied for this window type"

    resolve by using this permission in AndroidManifest:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    
    0 讨论(0)
  • 2020-11-22 13:28

    On Android 4.0 and above (API level >= 14), you can use TextureView to preview the camera stream and make it invisible so as to not show it to the user. Here's how:

    First create a class to implement a SurfaceTextureListener that will get the create/update callbacks for the preview surface. This class also takes a camera object as input, so that it can call the camera's startPreview function as soon as the surface is created:

    public class CamPreview extends TextureView implements SurfaceTextureListener {
    
      private Camera mCamera;
    
      public CamPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
       }
    
      @Override
      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
        setLayoutParams(new FrameLayout.LayoutParams(
            previewSize.width, previewSize.height, Gravity.CENTER));
    
        try{
          mCamera.setPreviewTexture(surface);
         } catch (IOException t) {}
    
        mCamera.startPreview();
        this.setVisibility(INVISIBLE); // Make the surface invisible as soon as it is created
      }
    
      @Override
      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
          // Put code here to handle texture size change if you want to
      }
    
      @Override
      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return true;
      }
    
      @Override
      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
          // Update your view here!
      }
    }
    

    You'll also need to implement a callback class to process the preview data:

    public class CamCallback implements Camera.PreviewCallback{
      public void onPreviewFrame(byte[] data, Camera camera){
         // Process the camera data here
      }
    }
    

    Use the above CamPreview and CamCallback classes to setup the camera in your activity's onCreate() or similar startup function:

    // Setup the camera and the preview object
    Camera mCamera = Camera.open(0);
    CamPreview camPreview = new CamPreview(Context,mCamera);
    camPreview.setSurfaceTextureListener(camPreview);
    
    // Connect the preview object to a FrameLayout in your UI
    // You'll have to create a FrameLayout object in your UI to place this preview in
    FrameLayout preview = (FrameLayout) findViewById(R.id.cameraView); 
    preview.addView(camPreview);
    
    // Attach a callback for preview
    CamCallback camCallback = new CamCallback();
    mCamera.setPreviewCallback(camCallback);
    
    0 讨论(0)
  • 2020-11-22 13:31

    We solved this problem by using a dummy SurfaceView (not added to actual GUI) in versions below 3.0 (or let's say 4.0 as a camera service on a tablet does not really make sense). In versions >= 4.0 this worked in the emulator only ;( The use of SurfaceTexture (and setSurfaceTexture()) instead of SurfaceView (and setSurfaceView()) worked here. At least this works on Nexus S.

    I think this really is a shortcoming of the Android framework.

    0 讨论(0)
  • 2020-11-22 13:40

    I found the answer to this in the Android Camera Docs.

    Note: It is possible to use MediaRecorder without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.

    You can find the step by step instructions at the link above. After the instructions, it will state the quote that I have provided above.

    0 讨论(0)
  • 2020-11-22 13:41

    it is really weird that camera on android platform can't stream video until it given valid preview surface. it seems that the architects of the platform was not thinking about 3rd party video streaming applications at all. even for augmented reality case the picture can be presented as some kind of visual substitution, not real time camera stream.

    anyway, you can simply resize preview surface to 1x1 pixels and put it somewhere in the corner of the widget (visual element). please pay attention - resize preview surface, not camera frame size.

    of course such trick does not eliminate unwanted data streaming (for preview) which consumes some system resources and battery.

    0 讨论(0)
  • 2020-11-22 13:41

    Actually it is possible, but you have to fake the preview with a dummy SurfaceView

    SurfaceView view = new SurfaceView(this);
    c.setPreviewDisplay(view.getHolder());
    c.startPreview();
    c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
    

    Update 9/21/11: Apparently this does not work for every Android device.

    0 讨论(0)
提交回复
热议问题