mirror of
https://github.com/opencv/opencv.git
synced 2025-08-06 06:26:29 +08:00
Merge pull request #22332 from komakai:android-cam-stride
This commit is contained in:
commit
66b3155a48
@ -181,6 +181,7 @@ class AndroidCameraCapture : public IVideoCapture
|
|||||||
std::shared_ptr<ACameraCaptureSession> captureSession;
|
std::shared_ptr<ACameraCaptureSession> captureSession;
|
||||||
CaptureSessionState sessionState = CaptureSessionState::INITIALIZING;
|
CaptureSessionState sessionState = CaptureSessionState::INITIALIZING;
|
||||||
int32_t frameWidth = 0;
|
int32_t frameWidth = 0;
|
||||||
|
int32_t frameStride = 0;
|
||||||
int32_t frameHeight = 0;
|
int32_t frameHeight = 0;
|
||||||
int32_t colorFormat;
|
int32_t colorFormat;
|
||||||
std::vector<uint8_t> buffer;
|
std::vector<uint8_t> buffer;
|
||||||
@ -307,8 +308,11 @@ public:
|
|||||||
AImage_getPlaneData(image.get(), 1, &uPixel, &uLen);
|
AImage_getPlaneData(image.get(), 1, &uPixel, &uLen);
|
||||||
AImage_getPlaneData(image.get(), 2, &vPixel, &vLen);
|
AImage_getPlaneData(image.get(), 2, &vPixel, &vLen);
|
||||||
AImage_getPlanePixelStride(image.get(), 1, &uvPixelStride);
|
AImage_getPlanePixelStride(image.get(), 1, &uvPixelStride);
|
||||||
|
int32_t yBufferLen = yLen;
|
||||||
|
|
||||||
if ( (uvPixelStride == 2) && (uPixel == vPixel + 1) && (yLen == frameWidth * frameHeight) && (uLen == ((yLen / 2) - 1)) && (vLen == uLen) ) {
|
if ( (uvPixelStride == 2) && (uPixel == vPixel + 1) && (yLen == (yStride * (frameHeight - 1)) + frameWidth) && (uLen == (uvStride * ((frameHeight / 2) - 1)) + frameWidth - 1) && (uvStride == yStride) && (vLen == uLen) ) {
|
||||||
|
frameStride = yStride;
|
||||||
|
yBufferLen = frameStride * frameHeight;
|
||||||
colorFormat = COLOR_FormatYUV420SemiPlanar;
|
colorFormat = COLOR_FormatYUV420SemiPlanar;
|
||||||
if (fourCC == FOURCC_UNKNOWN) {
|
if (fourCC == FOURCC_UNKNOWN) {
|
||||||
fourCC = FOURCC_NV21;
|
fourCC = FOURCC_NV21;
|
||||||
@ -326,8 +330,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
buffer.insert(buffer.end(), yPixel, yPixel + yLen);
|
buffer.insert(buffer.end(), yPixel, yPixel + yBufferLen);
|
||||||
buffer.insert(buffer.end(), vPixel, vPixel + yLen / 2);
|
buffer.insert(buffer.end(), vPixel, vPixel + yBufferLen / 2);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,8 +340,8 @@ public:
|
|||||||
if (buffer.empty()) {
|
if (buffer.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Mat yuv(frameHeight + frameHeight/2, frameWidth, CV_8UC1, buffer.data());
|
|
||||||
if (colorFormat == COLOR_FormatYUV420Planar) {
|
if (colorFormat == COLOR_FormatYUV420Planar) {
|
||||||
|
Mat yuv(frameHeight + frameHeight/2, frameWidth, CV_8UC1, buffer.data());
|
||||||
switch (fourCC) {
|
switch (fourCC) {
|
||||||
case FOURCC_BGR:
|
case FOURCC_BGR:
|
||||||
cv::cvtColor(yuv, out, cv::COLOR_YUV2BGR_YV12);
|
cv::cvtColor(yuv, out, cv::COLOR_YUV2BGR_YV12);
|
||||||
@ -356,18 +360,20 @@ public:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (colorFormat == COLOR_FormatYUV420SemiPlanar) {
|
} else if (colorFormat == COLOR_FormatYUV420SemiPlanar) {
|
||||||
|
Mat yuv(frameHeight + frameHeight/2, frameStride, CV_8UC1, buffer.data());
|
||||||
|
Mat tmp = (frameWidth == frameStride) ? yuv : yuv(Rect(0, 0, frameWidth, frameHeight + frameHeight / 2));
|
||||||
switch (fourCC) {
|
switch (fourCC) {
|
||||||
case FOURCC_BGR:
|
case FOURCC_BGR:
|
||||||
cv::cvtColor(yuv, out, cv::COLOR_YUV2BGR_NV21);
|
cv::cvtColor(tmp, out, cv::COLOR_YUV2BGR_NV21);
|
||||||
break;
|
break;
|
||||||
case FOURCC_RGB:
|
case FOURCC_RGB:
|
||||||
cv::cvtColor(yuv, out, cv::COLOR_YUV2RGB_NV21);
|
cv::cvtColor(tmp, out, cv::COLOR_YUV2RGB_NV21);
|
||||||
break;
|
break;
|
||||||
case FOURCC_GRAY:
|
case FOURCC_GRAY:
|
||||||
cv::cvtColor(yuv, out, cv::COLOR_YUV2GRAY_NV21);
|
cv::cvtColor(tmp, out, cv::COLOR_YUV2GRAY_NV21);
|
||||||
break;
|
break;
|
||||||
case FOURCC_NV21:
|
case FOURCC_NV21:
|
||||||
yuv.copyTo(out);
|
tmp.copyTo(out);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOGE("Unexpected FOURCC value: %d", fourCC);
|
LOGE("Unexpected FOURCC value: %d", fourCC);
|
||||||
|
Loading…
Reference in New Issue
Block a user