Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  *
     16  */
     17 
     18 #ifndef ANDROID_CAMERATESTHELPERS_H
     19 #define ANDROID_CAMERATESTHELPERS_H
     20 
     21 // Must come before NdkCameraCaptureSession.h
     22 #include <camera/NdkCaptureRequest.h>
     23 
     24 #include <camera/NdkCameraCaptureSession.h>
     25 #include <camera/NdkCameraDevice.h>
     26 #include <camera/NdkCameraError.h>
     27 #include <camera/NdkCameraManager.h>
     28 #include <cstdlib>
     29 #include <cstring>
     30 #include <jni.h>
     31 #include <media/NdkImage.h>
     32 #include <media/NdkImageReader.h>
     33 
     34 // A helper class which can be used to initialize/control the camera.
     35 class CameraHelper {
     36 public:
     37   ~CameraHelper();
     38 
     39   int initCamera(ANativeWindow *imgReaderAnw);
     40   bool isCapabilitySupported(
     41       acamera_metadata_enum_android_request_available_capabilities_t cap);
     42   bool isCameraReady() { return mIsCameraReady; }
     43   void closeCamera();
     44   int takePicture();
     45 
     46   static void onDeviceDisconnected(void * /*obj*/, ACameraDevice * /*device*/) {
     47   }
     48   static void onDeviceError(void * /*obj*/, ACameraDevice * /*device*/,
     49                             int /*errorCode*/) {}
     50   static void onSessionClosed(void * /*obj*/,
     51                               ACameraCaptureSession * /*session*/) {}
     52   static void onSessionReady(void * /*obj*/,
     53                              ACameraCaptureSession * /*session*/) {}
     54   static void onSessionActive(void * /*obj*/,
     55                               ACameraCaptureSession * /*session*/) {}
     56 
     57 private:
     58   ACameraDevice_StateCallbacks mDeviceCb{this, onDeviceDisconnected,
     59                                          onDeviceError};
     60   ACameraCaptureSession_stateCallbacks mSessionCb{
     61       this, onSessionClosed, onSessionReady, onSessionActive};
     62 
     63   ANativeWindow *mImgReaderAnw{nullptr}; // not owned by us.
     64 
     65   // Camera manager
     66   ACameraManager *mCameraManager{nullptr};
     67   ACameraIdList *mCameraIdList{nullptr};
     68   // Camera device
     69   ACameraMetadata *mCameraMetadata{nullptr};
     70   ACameraDevice *mDevice{nullptr};
     71   // Capture session
     72   ACaptureSessionOutputContainer *mOutputs{nullptr};
     73   ACaptureSessionOutput *mImgReaderOutput{nullptr};
     74   ACameraCaptureSession *mSession{nullptr};
     75   // Capture request
     76   ACaptureRequest *mCaptureRequest{nullptr};
     77   ACameraOutputTarget *mReqImgReaderOutput{nullptr};
     78 
     79   bool mIsCameraReady{false};
     80   const char *mCameraId{nullptr};
     81 };
     82 
     83 #endif // ANDROID_CAMERATHELPERS_H
     84