Custom Camera PictureCallback does not run if flas

2019-07-21 18:34发布

问题:

I have written a basic custom camera which I recently changed to use the flash (where available) on the device. Once I had made the change the code ran fine as before on my HTC One Mini 2, but no longer worked on my Galaxy S4. After stepping through I found my jpeg callback is never reached on the S4, instead the raw callback is run but with a null byte[] supplied.

I found this similar question: S4 Flash Problem

Which seems to confirm there is an issue and solution on these devices but the solution has not been documented.

To re-iterate, the code works fine on the S4 so long as I don't turn on the flash with:

Camera.Parameters p = mCamera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);//Or FLASH_MODE_ON
mCamera.setParameters(p);

Can anybody with experience on these devices provide the solution?

回答1:

After a lot of trial and error over the last week I have stumbled upon two possible solutions to this problem, the first of which doesn't make any sense to me at all:

  1. Force the zoom to be greater than x1. For some reason if I set the zoom to x1.2 etc. the camera works again as I would expect.

  2. Ensure the aspect ratio of the selected picture size matches the native aspect ratio of the device. The S4 has a resolution of 1920x1080 so any picture size that matches this 16:9 ratio works with the flash on. This makes a bit more sense but why the flash makes the difference and why anything that isn't supported should be returned by getSupportedPictureSizes() I don't know (albeit they do seem to be without the flash.)

Currently I have chosen to use option 2 and am testing on my other devices. Might also be worth mentioning that my next step on the S4 was to allow the user to turn the flash on/off/auto themselves but this required destroying and recreating the camera each time.