Search code examples
androidcameraframe-rate

Ways of getting high FPS for real time computer vision processing


My goal is to get as high FPS as possible. I will be happy with BW small frames, but FPS should be maximum. getSupportedPreviewFpsRange on HTC WildFire S returns (9, 30). But when I try to draw preview image with most simple processing it visually get at about 12-15 FPS max. After setting Parameters and startPreview() method call I do this:

camera.setPreviewCallback(new Camera.PreviewCallback() {
          @Override
          public void onPreviewFrame(byte[] bytes, Camera camera) {
            Bitmap bitmap = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888);
            int[] colors = new int[size.width * size.height];
            for (int i=0; i < colors.length; i++){
              colors[i] = getBWColor(bytes[i]);
            }
            bitmap.setPixels(colors, 0, size.width, 0, 0, size.width, size.height);
            secondView.setImageBitmap(bitmap);
          }
        });
      }

private static int getBWColor(byte b){
    int res = 255;
    for(int i=0;i<3;i++){
      res = (res << 8) + b;
    }
    return res;
  }

Is there way to get it as fast as possible? =(


Solution

  • First, use setPreviewCallbackWithBuffer(), it reuses one buffer and does not have to allocate new buffer on every frame. Check my older answer, there is an example how to use it.

    Than, do not do any expensive opeartion in onPreviewFrame(), the time of the data is limited, it is rewritten on each new frame. You should do the processing in another thread.

    And of course do not create new bitmap a the colors array on each frame, do it just once.