How to get raw preview data from Camera object at least 15 frames per second in Android?

后端 未结 6 1326
忘了有多久
忘了有多久 2020-11-30 19:32

I need to obtain raw preview data from Camera object at least 15 frame per second, but I can only get a frame in 110 milliseconds which means I can get only

相关标签:
6条回答
  • 2020-11-30 20:15

    I usually declare a global boolean lockCameraUse. The the callback function usually looks like this.

      public void onPreviewFrame(byte[] data, Camera camera) {
        if (lockCameraUse) {
          camera.addCallbackBuffer(data);
          return;
        }
        lockCameraUse = true;
        // processinng data
    
        // done processing data
        camera.addCallbackBuffer(data);
        lockCameraUse = false;
        return;
      }
    
    0 讨论(0)
  • 2020-11-30 20:28

    I'm affraid, you can not. Preview framerate setting is hint for camera appplication ( which runs in separate process) - and it is free to accept or silently ignore it. It is also not related with preview frame retrieval

    When you request preview frame, you just say external application that you would like to have it. Buffer for it is allocated in camera application and then passed to your activity via mmaped memory segment - this takes time.

    You may get desired performance on some devices, but not necessarily on one you are playing with.

    If you need defined frame rate, you will have to capture video and then parse / decompress resulting binary stream.

    0 讨论(0)
  • 2020-11-30 20:28

    This should not be a problem. My androangelo (it's in the market) app get's up to 30 frames per second (at least I implemented a rate-brake to slow it down).

    Please check carefully, whether Your log is filled with garbage-collector statements. This is the case if too few buffers are added. This had be the trick for me. At least I came up to add 20! buffers to the camera.

    Then the processing of each frame should take place in a separate thread. While an image is in the thread for processing, the callback should skip the current frame.

    0 讨论(0)
  • 2020-11-30 20:29

    In my understanding, Android does not allow user to a set a fixed framerate, nor guarantee the value of fps that you specify will be respected is due to the frame exposure time which is set by the camera hardware or firmware. The frame rate you observe may be a function of lighting conditions. For example, a certain phone may give you a 30fps preview rate in a day light but only 7 fps if you are filming in a low light condition.

    0 讨论(0)
  • 2020-11-30 20:30

    Me experience with the camera stuff has been fiddly and hardware dependent. So try running it on other hardware sometime if you can.

    Also might be worth trying some more camera settings.

    • Checkout setRecordingHint (boolean hint).
    • Could even try FOCUS_MODE.

    Thanks for including a code sample btw.

    0 讨论(0)
  • 2020-11-30 20:31

    One thing that seems to increase the fluidity of the preview, if not the actual FPS necessarily, is setting the previewFormat to YV12 if supported. It's fewer bytes to copy, 16-byte aligned, and possibly optimized in other ways:

        // PREVIEW FORMATS
        List<Integer> supportedPreviewFormats = parameters.getSupportedPreviewFormats();
        Iterator<Integer> supportedPreviewFormatsIterator = supportedPreviewFormats.iterator();
        while(supportedPreviewFormatsIterator.hasNext()){
            Integer previewFormat =supportedPreviewFormatsIterator.next();
            // 16 ~ NV16 ~ YCbCr
            // 17 ~ NV21 ~ YCbCr ~ DEFAULT
            // 4  ~ RGB_565
            // 256~ JPEG
            // 20 ~ YUY2 ~ YcbCr ...
            // 842094169 ~ YV12 ~ 4:2:0 YCrCb comprised of WXH Y plane, W/2xH/2 Cr & Cb. see documentation
            Log.v("CameraTest","Supported preview format:"+previewFormat);
            if (previewFormat == ImageFormat.YV12) {
                parameters.setPreviewFormat(previewFormat);
                Log.v("CameraTest","SETTING FANCY YV12 FORMAT");                
            }
        }
    

    http://developer.android.com/reference/android/graphics/ImageFormat.html#YV12 describes the format. This plus a few spare buffers gives me "Time Gaps" of as low as 80...which is still not "good enough", but ... better? (actually I've got one at 69...but really, they're more around 90 on average). Not sure how much logging is slowing things down?

    Setting the previewSize to 320x240 (versus 1280x720) gets things down to the 50-70msec range...so maybe that's what you need to do? Admittedly, that little data may be a lot less useful.

    // all tested on Nexus4

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