目前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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 . 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.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.
*/
#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 .
// 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