Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android 8.1 (XA2) compatibility + zoom implementation #115

Open
wants to merge 3 commits into
base: camera2
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions droidmediacamera2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#include "droidmediacamera.h"

#include <camera/CameraParameters.h>
#include <camera/NdkCaptureRequest.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these were sorted alphabetically before

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue with includes in Android versions < 10. In my wip branch I have a comment like this before this include "// This needs to be first because of broken includes in Android < 10"

#include <camera/NdkCameraCaptureSession.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraError.h>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraMetadata.h>
#include <camera/NdkCameraMetadataTags.h>
#include <camera/NdkCaptureRequest.h>
#include <media/hardware/MetadataBufferType.h>
#include <media/NdkImage.h>
#include <media/NdkImageReader.h>
Expand All @@ -42,6 +42,10 @@
#undef LOG_TAG
#define LOG_TAG "DroidMediaCamera"

#if ANDROID_MAJOR <= 9
typedef ANativeWindow ACameraWindowType;
#endif

namespace android {
int32_t getColorFormat(const char* colorFormat) {
if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
Expand Down Expand Up @@ -418,11 +422,13 @@ bool droid_media_camera_get_info(DroidMediaCameraInfo *info, int camera_number)
goto fail;
}

#if ANDROID_MAJOR >= 9
if (ACameraMetadata_isLogicalMultiCamera(camera_metadata,
&num_physical_cameras,
&physical_camera_ids)) {
ALOGI("Multicamera with physical camera count %zu", num_physical_cameras);
}
#endif

status = ACameraMetadata_getConstEntry(camera_metadata,
ACAMERA_LENS_FACING,
Expand Down Expand Up @@ -1322,6 +1328,7 @@ const char *focus_mode_enum_to_string(uint8_t focus_mode, bool fixed_lens, bool
}
}

/* translates QCamera2 paramerter namings into android camera api2 parameter namings */
int param_key_string_to_enum(const char *key)
{
return
Expand Down Expand Up @@ -1352,7 +1359,11 @@ int param_key_string_to_enum(const char *key)
!strcmp(key, android::CameraParameters::KEY_METERING_AREAS) ?
ACAMERA_CONTROL_AE_REGIONS :
!strcmp(key, android::CameraParameters::KEY_ZOOM) ?
ACAMERA_CONTROL_ZOOM_RATIO :
#if ANDORID_MAJOR >= 9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

ACAMERA_CONTROL_ZOOM_RATIO :
#else
ACAMERA_SCALER_CROP_REGION :
#endif
!strcmp(key, android::CameraParameters::KEY_VIDEO_STABILIZATION) ?
ACAMERA_CONTROL_VIDEO_STABILIZATION_MODE :
-1;
Expand Down Expand Up @@ -1416,7 +1427,19 @@ void update_request(DroidMediaCamera *camera, ACaptureRequest *request, std::uno
int32_t key;
if ((key = param_key_string_to_enum(key_s.c_str())) >= 0) {
switch (key) {
case ACAMERA_CONTROL_AE_ANTIBANDING_MODE: {
case ACAMERA_SCALER_CROP_REGION: {
if (int32_t zoom_level = std::stoi(value_s)) {
int32_t *area = new int32_t[4];
area[0] = camera->image_width/(2 * 10) * (zoom_level);
area[1] = camera->image_height/(2 * 10) * (zoom_level);
area[2] = camera->image_width - (2 * area[0]);
area[3] = camera->image_height - (2 * area[1]);
ALOGI("setting crop for zoom level %d to %d,%d,%d,%d from w%dh%d", zoom_level, area[0], area[1], area[2], area[3], camera->image_width, camera->image_height);
ACaptureRequest_setEntry_i32(request, key, 4, area);
delete[] area;
}
}
case ACAMERA_CONTROL_AE_ANTIBANDING_MODE: {
uint8_t mode;
if ((mode = ab_mode_string_to_enum(value_s.c_str())) != -1) {
ACaptureRequest_setEntry_u8(request, key, 1, &mode);
Expand Down Expand Up @@ -1515,12 +1538,14 @@ void update_request(DroidMediaCamera *camera, ACaptureRequest *request, std::uno
ACaptureRequest_setEntry_u8(request, key, 1, &value);
break;
}
#if ANDROID_MAJOR >= 9
case ACAMERA_CONTROL_ZOOM_RATIO:
if (float value = std::stof(value_s)) {
ACaptureRequest_setEntry_float(request, key, 1, &value);
}
break;
case ACAMERA_FLASH_MODE: {
#endif
case ACAMERA_FLASH_MODE: {
uint8_t mode;
if (!strcmp(value_s.c_str(), android::CameraParameters::FLASH_MODE_TORCH)) {
mode = ACAMERA_CONTROL_AE_MODE_ON;
Expand Down Expand Up @@ -1586,7 +1611,7 @@ bool droid_media_camera_set_parameters(DroidMediaCamera *camera, const char *par
parse_pair_int32(value_s, 'x', camera->preview_width, camera->preview_height);
} else if (!strcmp(key_s.c_str(), "video-size")) {
parse_pair_int32(value_s, 'x', camera->video_width, camera->video_height);
}
}
}
}

Expand Down Expand Up @@ -1783,9 +1808,11 @@ char *droid_media_camera_get_parameters(DroidMediaCamera *camera)
params += "video-stabilization-supported = true;";
}
break;
case ACAMERA_CONTROL_ZOOM_RATIO_RANGE:
params += "max-zoom="+std::to_string(entry.data.f[1])+";";
break;
#if ANDROID_MAJOR >= 9
case ACAMERA_CONTROL_ZOOM_RATIO_RANGE:
params += "max-zoom="+std::to_string(entry.data.f[1])+";";
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return max-zoom also for older devices?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parameter means the max zoom of optical zooms. This does not really make sense if zoom is only done by cropping the image

Minimum and maximum zoom ratios supported by this camera device.
https://developer.android.google.cn/ndk/reference/group/camera#group___camera_1gga49cf3e5a3deefe079ad036a8fac14627af00bd3268478899b538e6ae1323720fc

#endif
case ACAMERA_FLASH_INFO_AVAILABLE:
if (entry.data.u8[0] == ACAMERA_FLASH_INFO_AVAILABLE_FALSE) {
params += "flash-mode-values=off;";
Expand Down