diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 1c0669cc..22895dbd 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -1042,9 +1042,6 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat) } } - - - #ifdef _DEBUG cv::Scalar scalarRed(0, 0, 255); // red diff --git a/app/src/main/cpp/camera2/ndkcamera.cpp b/app/src/main/cpp/camera2/ndkcamera.cpp index c7d1d267..57c08e9f 100644 --- a/app/src/main/cpp/camera2/ndkcamera.cpp +++ b/app/src/main/cpp/camera2/ndkcamera.cpp @@ -17,7 +17,9 @@ #include #include #include +#include #include +#include #include "mat.h" #include "gpu.h" #include "Camera2Helper.h" @@ -211,9 +213,7 @@ int NdkCamera::open(const std::string& cameraId) { // DisplayDimension maxJPG(0, 0); foundIt = false; - unsigned long long minRatio = -1; DisplayDimension temp; - unsigned long long pixels = (unsigned long long)mWidth * mHeight; for (int i = 0; i < e.count; i += 4) { @@ -226,12 +226,9 @@ int NdkCamera::open(const std::string& cameraId) { DisplayDimension res(e.data.i32[i + 1], e.data.i32[i + 2]); if (!disp.IsSameRatio(res)) { - unsigned long long ps = res.width() * res.height(); - unsigned long long ratio = (ps >= pixels) ? (ps - pixels) : (pixels - ps); - if (ratio < minRatio) + if (res.width() >= mWidth && res.height() >= mHeight) { temp = res; - minRatio = ratio; } continue; } @@ -240,9 +237,7 @@ int NdkCamera::open(const std::string& cameraId) { { foundIt = true; foundRes = res; - }/* else if (format == AIMAGE_FORMAT_JPEG && res > maxJPG) { - maxJPG = res; - }*/ + } } } @@ -260,6 +255,8 @@ int NdkCamera::open(const std::string& cameraId) { return 1; } + // foundRes.Flip(); + // query faceing acamera_metadata_enum_android_lens_facing_t facing = ACAMERA_LENS_FACING_FRONT; { @@ -400,7 +397,8 @@ int NdkCamera::open(const std::string& cameraId) { // setup imagereader and its surface { - media_status_t mstatus = AImageReader_new(foundRes.width(), foundRes.height(), AIMAGE_FORMAT_YUV_420_888, /*maxImages*/2, &image_reader); + // media_status_t mstatus = AImageReader_new(foundRes.width(), foundRes.height(), AIMAGE_FORMAT_YUV_420_888, /*maxImages*/2, &image_reader); + media_status_t mstatus = AImageReader_new(foundRes.org_width(), foundRes.org_height(), AIMAGE_FORMAT_YUV_420_888, /*maxImages*/2, &image_reader); if (mstatus == AMEDIA_OK) { @@ -800,10 +798,29 @@ void NdkCamera::on_image(const unsigned char* nv21, int nv21_width, int nv21_hei } // nv21_rotated to rgb - cv::Mat rgb(h, w, CV_8UC3); - // ncnn::yuv420sp2rgb(nv21_rotated.data, w, h, rgb.data); - ncnn::yuv420sp2rgb_nv12(yuv420data, w, h, rgb.data); - + cv::Mat rgb; + if (w == mWidth && h == mHeight) + { + rgb.create(h, w, CV_8UC3); + // ncnn::yuv420sp2rgb(nv21_rotated.data, w, h, rgb.data); + ncnn::yuv420sp2rgb_nv12(yuv420data, w, h, rgb.data); + } + else + { + cv::Mat org(h, w, CV_8UC3); + ncnn::yuv420sp2rgb_nv12(yuv420data, w, h, org.data); + if (w * mHeight == h * mWidth) // Same Ratio + { + cv::resize(org, rgb, cv::Size(mWidth, mHeight)); + } + else + { + // Crop image + int left = (w - mWidth) / 2; + int top = (h - mHeight) / 2; + rgb = org(cv::Range(top, top + mHeight), cv::Range(left, left + mWidth)); + } + } on_image(rgb); }