Skip to content

Commit 79d8793

Browse files
committed
camera: Android device orientation support.
1 parent e282fcf commit 79d8793

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/camera/android/SDL_camera_android.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ struct SDL_PrivateCameraData
149149
ACaptureRequest *request;
150150
ACameraCaptureSession *session;
151151
SDL_CameraSpec requested_spec;
152+
int rotation; // degrees to rotate clockwise to get from camera's static orientation to device's native orientation. Apply this plus current phone rotation to get upright image!
152153
};
153154

154155
static bool SetErrorStr(const char *what, const char *errstr, const int rc)
@@ -366,6 +367,17 @@ static SDL_CameraFrameResult ANDROIDCAMERA_AcquireFrame(SDL_Camera *device, SDL_
366367

367368
pAImage_delete(image);
368369

370+
int dev_rotation = 0;
371+
switch (Android_JNI_GetDisplayCurrentOrientation()) {
372+
case SDL_ORIENTATION_PORTRAIT: dev_rotation = 0; break;
373+
case SDL_ORIENTATION_LANDSCAPE: dev_rotation = 90; break;
374+
case SDL_ORIENTATION_PORTRAIT_FLIPPED: dev_rotation = 180; break;
375+
case SDL_ORIENTATION_LANDSCAPE_FLIPPED: dev_rotation = 270; break;
376+
default: SDL_assert(!"Unexpected device rotation!"); dev_rotation = 0; break;
377+
}
378+
379+
*rotation = dev_rotation - device->hidden->rotation; // current phone orientation, static camera orientation in relation to phone.
380+
369381
return result;
370382
}
371383

@@ -494,10 +506,23 @@ static bool PrepareCamera(SDL_Camera *device)
494506
imglistener.context = device;
495507
imglistener.onImageAvailable = onImageAvailable;
496508

509+
510+
const char *devid = (const char *) device->handle;
511+
512+
device->hidden->rotation = 0;
513+
ACameraMetadata *metadata = NULL;
514+
ACameraMetadata_const_entry orientationentry;
515+
if (pACameraManager_getCameraCharacteristics(cameraMgr, devid, &metadata) == ACAMERA_OK) {
516+
if (pACameraMetadata_getConstEntry(metadata, ACAMERA_SENSOR_ORIENTATION, &orientationentry) == ACAMERA_OK) {
517+
device->hidden->rotation = (int) (*orientationentry.data.i32 % 360);
518+
}
519+
pACameraMetadata_free(metadata);
520+
}
521+
497522
// just in case SDL_OpenCamera is overwriting device->spec as CameraPermissionCallback runs, we work from a different copy.
498523
const SDL_CameraSpec *spec = &device->hidden->requested_spec;
499524

500-
if ((res = pACameraManager_openCamera(cameraMgr, (const char *) device->handle, &dev_callbacks, &device->hidden->device)) != ACAMERA_OK) {
525+
if ((res = pACameraManager_openCamera(cameraMgr, devid, &dev_callbacks, &device->hidden->device)) != ACAMERA_OK) {
501526
return SetCameraError("Failed to open camera", res);
502527
} else if ((res2 = pAImageReader_new(spec->width, spec->height, format_sdl_to_android(spec->format), 10 /* nb buffers */, &device->hidden->reader)) != AMEDIA_OK) {
503528
return SetMediaError("Error AImageReader_new", res2);

0 commit comments

Comments
 (0)