1

I have an innerclass CameraView that extends SurfaceView.

Inside the surfaceChanged() function I use the following code to set Camera parameters:

Camera.Parameters camParams = mCamera.getParameters();
List<Camera.Size> sizes = camParams.getSupportedPreviewSizes();
// Sort the list in ascending order
Collections.sort(sizes, new Comparator<Camera.Size>() {

    public int compare(final Camera.Size a, final Camera.Size b) {
        return a.width * a.height - b.width * b.height;
    }
});

for (int i = 0; i < sizes.size(); i++) {
    if ((sizes.get(i).width >= imageWidth && sizes.get(i).height >= imageHeight) || i == sizes.size() - 1) {
        imageWidth = sizes.get(i).width;
        imageHeight = sizes.get(i).height;
        Log.e(LOG_TAG, "Resolution: " + imageWidth + " x " + imageHeight);
        break;
    }
}
camParams.setPreviewSize(imageWidth, imageHeight);

for(int[] arr : camParams.getSupportedPreviewFpsRange ()){
    Log.e(LOG_TAG, "Supported range: " + arr[0] + " - " + arr[1]);
}

camParams.setPreviewFrameRate(frameRate);
mCamera.setParameters(camParams);

The for loop inside this print the following range: Supported range: 4000 - 60000, which means that my device should support between 4 and 60 fps.

Now when I set frameRate = 45 (which is well withing the range), my app crashes with the following exception:

java.lang.RuntimeException: setParameters failed at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:1876)

This does not happen if frameRate = 30. Can someone explain why is my app is crashing? Can the getSupportedPreviewFpsRange() not be trusted?

Edit 1

I've now also tried camParams.setPreviewFpsRange(frameRate*1000, frameRate*1000);, same crash result.

Gooey
  • 4,740
  • 10
  • 42
  • 76

2 Answers2

1

I'm afraid you cannot force the fps value for the camera hardware (at least not with any value between 4000 and 60000.

Looking at the documentation, the method is deprecated since api level 9.

As they suggest, you will need to use setPreviewFpsRange in replacement with the exact range returned by camParams.getSupportedPreviewFpsRange.

You can use this question also as reference.

Community
  • 1
  • 1
mmark
  • 1,204
  • 12
  • 19
  • Hmmm, yes I've seen this question too. The problem is I require a stable amount of FPS, and like I said just setting the FPS to 30 by force works... so any idea? – Gooey May 25 '15 at 20:51
  • @Gooey what happens if you use setPreviewFpsRange (45000, 45000)? – mmark May 25 '15 at 21:02
  • That was my edit; the framerate is set to 45 there, resulting in``setPreviewFpsRange (45000, 45000)``, it crashes with the same error. – Gooey May 25 '15 at 21:04
1

Apparently if you set the fps in a second, different call (So getParameters and then only set the fps and assign the params to the camera) it works. No idea why, but at least that solves this issue...

Gooey
  • 4,740
  • 10
  • 42
  • 76
  • Interesting. I am working on a similar problem in an application where I'd like to manually change camera settings such as `p.setZoom(10);`, `p.setRotation(270);`, and `p.setPreviewFpsRange(4000, 6000);`. The first two settings work fine, setPreviewFpsRange however crashes after 1 update. – portsample Oct 26 '15 at 19:26