Android NDK Camera2Api 图像采集

目前Android平台除了V4l2这一种图像采集方式还有Camera2api 这种Android ndk的图像采集方式,这种采集方式比V4l2好用。

Camera2Reader.h

/*
 * Copyright 2015 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef __CAMERA2_READER_H__
#define __CAMERA2_READER_H__
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraError.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraMetadataTags.h>
#include <media/NdkImageReader.h>
#include <android/log.h>
#define  LOG_TAG    "native-camera2-jni"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define FMT_NUM_PLANES 1
 
// struct CameraParam
// {
//     int id;
//     const char *device;
//     RK_U32 bufcnt;
//     RK_U32 width;
//     RK_U32 height;
//     MppFrameFormat fmt;
// };
 
class Camera2Reader
{
private:
    AImageReader *mAImageReader;
    ANativeWindow *theNativeWindow;
    ACameraDevice *cameraDevice;
    ACaptureRequest *captureRequest;
    ACameraOutputTarget *cameraOutputTarget;
    ACaptureSessionOutput *sessionOutput;
    ACaptureSessionOutputContainer *captureSessionOutputContainer;
    ACameraCaptureSession *captureSession;
 
    ACameraDevice_StateCallbacks deviceStateCallbacks;
    ACameraCaptureSession_stateCallbacks captureSessionStateCallbacks;
 
    int sessionSequenceId;
 
    int mCameraId = 0;
    char mDevice[20];
    int mBufcnt;
    int mWidth;
    int mHeight;
    int mFmt;
 
    unsigned char *mYuv720p = NULL;
    unsigned char *mYuv420i = NULL;
    unsigned char *mArgb1080p = NULL;
    // Create a new context to capture frames from <fname>. Returns NULL on error.
    int Init(const char *device, int bufcnt, int width, int height, int fmt);
    
    // Stop capturing and free a context.
    int Deinit();
    bool readyToRun();
    bool threadLoop();
    ACameraCaptureSession_stateCallbacks *GetSessionListener();
    void ImageCallback(AImageReader *reader);
    static void OnImageCallback(void *ctx, AImageReader *reader);
 
    static void camera_device_on_disconnected(void *context, ACameraDevice *device)
    {
        LOGI("Camera(id: %s) is diconnected.\n", ACameraDevice_getId(device));
    }
 
    static void camera_device_on_error(void *context, ACameraDevice *device, int error)
    {
        LOGI("Error(code: %d) on Camera(id: %s).\n", error, ACameraDevice_getId(device));
    }
 
    static void capture_session_on_ready(void *context, ACameraCaptureSession *session)
    {
        LOGI("Session is ready. %p\n", session);
    }
 
    static void capture_session_on_active(void *context, ACameraCaptureSession *session)
    {
        LOGI("Session is activated. %p\n", session);
    }
 
    static void capture_session_on_closed(void *context, ACameraCaptureSession *session)
    {
        LOGI("Session is closed. %p\n", session);
    }
 
public:
    bool Open();
    Camera2Reader(const char *device, int bufcnt, int width, int height, int fmt);
    Camera2Reader();
    void *readImage(int chn);
    // Camera2Reader &operator=(CameraParam *cameraParam);
    ~Camera2Reader();
    void start();
    void stop();
};
#endif /* __CAMERA_READER_H__ */


Camera2Reader.cc

/*
 * Copyright 2015 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define MODULE_TAG "CameraReader"
 
#include "mpp_log.h"
#include "mpp_mem.h"
#include "Camera2Reader.h"
 
Camera2Reader::Camera2Reader(const char *device, int bufcnt, int width, int height, int fmt)
    : mBufcnt(bufcnt),
      mWidth(width),
      mHeight(height),
      mFmt(fmt)
{
    strcpy(mDevice, device);
}
Camera2Reader::Camera2Reader() 
{
}
 
int Camera2Reader::Init(const char *device, int bufcnt, int width, int height, int format)
{
    return MPP_NOK;
}
 
// Free a context to capture frames from <fname>.
// Returns NULL on error.
int Camera2Reader::Deinit()
{
    return 0;
}
 
bool Camera2Reader::Open()
{
    ACameraIdList *cameraIdList = NULL;
    ACameraMetadata *cameraMetadata = NULL;
 
    const char *selectedCameraId = NULL;
    camera_status_t camera_status = ACAMERA_OK;
    ACameraManager *cameraManager = ACameraManager_create();
 
    camera_status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList);
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to get camera id list (reason: %d)\n", camera_status);
        return false;
    }
 
    if (cameraIdList->numCameras < 1) {
        LOGI("No camera device detected.\n");
        return false;
    }
 
    selectedCameraId = cameraIdList->cameraIds[0];
 
    LOGI("Trying to open Camera2 (id: %s, num of camera : %d)\n", selectedCameraId,
         cameraIdList->numCameras);
 
    camera_status = ACameraManager_getCameraCharacteristics(cameraManager, selectedCameraId,
                                                            &cameraMetadata);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to get camera meta data of ID:%s\n", selectedCameraId);
    }
 
    deviceStateCallbacks.onDisconnected = camera_device_on_disconnected;
    deviceStateCallbacks.onError = camera_device_on_error;
 
    camera_status = ACameraManager_openCamera(cameraManager, selectedCameraId,
                                              &deviceStateCallbacks, &cameraDevice);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to open camera device (id: %s)\n", selectedCameraId);
    }
 
    camera_status = ACameraDevice_createCaptureRequest(cameraDevice, TEMPLATE_PREVIEW,
                                                       &captureRequest);
 
    if (camera_status != ACAMERA_OK) {
        LOGI("Failed to create preview capture request (id: %s)\n", selectedCameraId);
    }
 
    ACaptureSessionOutputContainer_create(&captureSessionOutputContainer);
 
    captureSessionStateCallbacks.onReady = capture_session_on_ready;
    captureSessionStateCallbacks.onActive = capture_session_on_active;
    captureSessionStateCallbacks.onClosed = capture_session_on_closed;
 
    ACameraMetadata_free(cameraMetadata);
    ACameraManager_deleteCameraIdList(cameraIdList);
    ACameraManager_delete(cameraManager);
 
    
    media_status_t status;
    status = AImageReader_new(1920, 1080, AIMAGE_FORMAT_YUV_420_888, 5, &mAImageReader);
    if (status != AMEDIA_OK)
    {
        LOGI("AImageReader_new error\n");
        return false;
    }
 
    AImageReader_ImageListener listener{
            .context = this,
            .onImageAvailable = OnImageCallback,
    };
    AImageReader_setImageListener(mAImageReader, &listener);
 
    //ANativeWindow *mNativeWindow;
    status = AImageReader_getWindow(mAImageReader, &theNativeWindow);
    if (status != AMEDIA_OK)
    {
        LOGI("AImageReader_getWindow error\n");
        return false;
    }
 
    LOGI("Surface is prepared in %p.\n", theNativeWindow);
 
    ACameraOutputTarget_create(theNativeWindow, &cameraOutputTarget);
    ACaptureRequest_addTarget(captureRequest, cameraOutputTarget);
 
    ACaptureSessionOutput_create(theNativeWindow, &sessionOutput);
    ACaptureSessionOutputContainer_add(captureSessionOutputContainer, sessionOutput);
 
    ACameraDevice_createCaptureSession(cameraDevice, captureSessionOutputContainer,
                                       &captureSessionStateCallbacks, &captureSession);
 
    ACameraCaptureSession_setRepeatingRequest(captureSession, NULL, 1, &captureRequest, NULL);
    LOGI("Surface is prepared in here.\n");
    return true;
}
 
ACameraCaptureSession_stateCallbacks *Camera2Reader::GetSessionListener()
{
    static ACameraCaptureSession_stateCallbacks sessionListener = {
        .context = this,
        .onClosed = Camera2Reader::capture_session_on_closed,
        .onReady = Camera2Reader::capture_session_on_ready,
        .onActive = Camera2Reader::capture_session_on_active,
    };
    return &sessionListener;
}
 
void Camera2Reader::ImageCallback(AImageReader *reader)
{
    int32_t format;
    AImage *image = nullptr;
    media_status_t status = AImageReader_acquireNextImage(reader, &image);
    LOGI("ImageCallback\n");
    if (status == AMEDIA_OK && image)
    {
        LOGI("ImageCallback\n");
        AImage_delete(image);
    }
}
void Camera2Reader::OnImageCallback(void *ctx, AImageReader *reader)
{
    AImage *image = nullptr;
    media_status_t status = AImageReader_acquireNextImage(reader, &image);
    if (status == AMEDIA_OK && image)
    {
        AImage_delete(image);
    }
}
 
bool Camera2Reader::readyToRun()
{
    if (!Init(mDevice, mBufcnt, mWidth, mHeight, mFmt))
    {
        LOGI("Init false\n");
        return false;
    }
 
    return Open();
}
void Camera2Reader::start()
{
    //run();
}
 
void Camera2Reader::stop()
{
    //threadStop();
}
 
bool Camera2Reader::threadLoop()
{
    usleep(1000);
    return true;
}
 
Camera2Reader::~Camera2Reader()
{
    LOGI("~CameraReader\n");
}
 
void *Camera2Reader::readImage(int chn)
{
    return NULL;
}


CmakeListst.txt加上camera2ndk mediandk
                        
原文链接:https://blog.csdn.net/yinsui1839/article/details/128442093

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值