0

I am developing a custom Camera App using Camera2 API and have tried to enable the manual focusing by a seekbar. I referenced several articles but am facing an error that may come from the my java coding.

I saw a message of "error: unreported exception CameraAccessException; must be caught or declared to be thrown" from the line of "CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());"

Does anyone of you have an idea ?

       public void onStartTrackingTouch(SeekBar seekBar) {

            previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);

        }


        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChangedValue = progress;


            CameraManager manager = (CameraManager)getSystemService(CAMERA_SERVICE);
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());

            float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
            float num = (((float)progress) * minimumLens / 100);
            previewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);

        }
H. Yu
  • 1
  • Do you have permissions for accessing the camera enabled in your manifest? It sounds as though the access violation is leaning towards a permission issue. – Jay Snayder Oct 05 '18 at 16:54

1 Answers1

0

This is normal Java programming, not related to the camera APIs specifically.

The getCameraCharacteristics method can throw the checked CameraAccessException exception. The Java langugage requires that all checked exceptions are either handled by the function calling a method that may throw a checked exception, or the function has to also declare it may throw that checked exception.

So you can either surround the getCameraCharacteristics call in a 'try...catch' block which catches the CameraAccessException, or you can add 'throws CameraAccessException' to the end of 'public void onProgressChanged...'.

You probably want the former case:

CameraCharacteristics characteristics;
try {
    characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
} catch (CameraAccessException e) {
    // Code ends up here if getCameraCharacteristics can't get the information.
    // The getReason method on CameraAccessException will tell you why
    switch (e.getReason()) {
        case CameraAccessException.CAMERA_DISABLED:
          ....
        case CameraAccessException.CAMERA_DISCONNECTED:
           ...
    }
}
Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47