New android Camera2 Api on nexus 7
I have a problem with the Camera2 API Android site on a Nexus 7. I have developed an app on Android 4.4.4 that uses the camera to take pictures and I want to update it to update Lollipop. I followed the code from this link to make the new camera api work: https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment .java
I tested the code on a Nexus 5 and it works well, but if I try it on a Nexus 7 something goes wrong. This is the error log:
12-17 17:01:15.517: W/LegacyRequestMapper(24382): convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
12-17 17:01:15.517: W/LegacyRequestMapper(24382): Only received metering rectangles with weight 0.
12-17 17:01:15.518: W/LegacyRequestMapper(24382): Only received metering rectangles with weight 0.
12-17 17:01:15.519: W/LegacyRequestMapper(24382): mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH;camera does not support it
12-17 17:01:15.519: W/LegacyRequestMapper(24382): convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
12-17 17:01:15.955: I/CameraDeviceState(24382): Legacy camera service transitioning to state CAPTURING
12-17 17:01:21.198: I/RequestQueue(24382): Repeating capture request cancelled.
12-17 17:01:21.198: I/RequestQueue(24382): Repeating capture request set.
12-17 17:01:21.215: W/LegacyRequestMapper(24382): convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
12-17 17:01:21.215: W/LegacyRequestMapper(24382): Only received metering rectangles with weight 0.
12-17 17:01:21.215: W/LegacyRequestMapper(24382): Only received metering rectangles with weight 0.
12-17 17:01:21.216: W/LegacyRequestMapper(24382): mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH;camera does not support it
12-17 17:01:21.216: W/LegacyRequestMapper(24382): convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
12-17 17:01:21.495: E/AndroidRuntime(24382): FATAL EXCEPTION: CameraBackground
12-17 17:01:21.495: E/AndroidRuntime(24382): Process: com.example.newapicamera, PID: 24382
12-17 17:01:21.495: E/AndroidRuntime(24382): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
12-17 17:01:21.495: E/AndroidRuntime(24382): at com.example.newapicamera.Camera2BasicFragment$4.process(Camera2BasicFragment.java:285)
12-17 17:01:21.495: E/AndroidRuntime(24382): at com.example.newapicamera.Camera2BasicFragment$4.onCaptureCompleted(Camera2BasicFragment.java:324)
12-17 17:01:21.495: E/AndroidRuntime(24382): at java.lang.reflect.Method.invoke(Native Method)
12-17 17:01:21.495: E/AndroidRuntime(24382): at java.lang.reflect.Method.invoke(Method.java:372)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.hardware.camera2.dispatch.InvokeDispatcher.dispatch(InvokeDispatcher.java:39)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.hardware.camera2.dispatch.HandlerDispatcher$1.run(HandlerDispatcher.java:65)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.os.Handler.handleCallback(Handler.java:739)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.os.Looper.loop(Looper.java:135)
12-17 17:01:21.495: E/AndroidRuntime(24382): at android.os.HandlerThread.run(HandlerThread.java:61)
12-17 17:01:21.542: I/RequestQueue(24382): Repeating capture request cancelled.
12-17 17:01:21.580: E/BufferQueueProducer(24382): [unnamed-24382-2] dequeueBuffer: BufferQueue has been abandoned
12-17 17:01:21.580: E/BufferQueueProducer(24382): [unnamed-24382-2] dequeueBuffer: BufferQueue has been abandoned
12-17 17:01:21.596: E/BufferQueueProducer(24382): [unnamed-24382-2] queueBuffer: BufferQueue has been abandoned
12-17 17:01:21.605: W/Camera-JNI(24382): callback on dead camera object
12-17 17:01:21.620: E/BufferQueueProducer(24382): [unnamed-24382-2] queueBuffer: BufferQueue has been abandoned
12-17 17:01:21.651: E/BufferQueueProducer(24382): [unnamed-24382-2] queueBuffer: BufferQueue has been abandoned
12-17 17:01:21.688: E/BufferQueueProducer(24382): [unnamed-24382-2] queueBuffer: BufferQueue has been abandoned
12-17 17:01:21.721: E/BufferQueueProducer(24382): [unnamed-24382-2] queueBuffer: BufferQueue has been abandoned
The line that throws the exception:
int aeState = result.get(CaptureResult.CONTROL_AE_STATE);
What's going wrong?
source to share
The Nexus 7 is a LEGACY device using the new Camera API2, and LEGACY devices have many restrictions regarding devices that implement either the LIMITED or FULL levels of the new API.
There is a note in the documentation for CONTROL_AE_STATE:
Limited capability - Present on all camera devices that report being at least HARDWARE_LEVEL_LIMITED devices in the android.info.supportedHardwareLevel key
which means that it is not guaranteed to be present for LEGACY devices, and in fact it will not be, since such devices do not provide AE ββstatus information.
source to share
Before using any function of the Camera2 API, you first need to check if the function is supported by the device or not. For your case, you can check as below:
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
mState = STATE_WAITING_NON_PRECAPTURE;
captureStillPicture();
} else {
runPrecaptureSequence();
}
For more information, you can check the official code sample from Google here: https://github.com/googlesamples/android-Camera2Basic
source to share
I am assuming it result
is a package, and it get
returns an object Integer
(as opposed to a primitive int
like on the left side of the statement). Seems CaptureResult.CONTROL_AE_STATE
not included. This may mean that something is not supported on your device, but it is unclear.
The crash is due to Java automatically unpacking Integer
into int
, but Integer
here null
! This will throw a null pointer exception because Java is trying to convert a null object to int, so the warning is:
Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
The safer way to deal with this is to aeState
have Integer
, so you don't force java to automatically disable the right side of the statement. Java tries to help you, but it can be confusing!
source to share