diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 2f22a189..cc2a17de 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -1023,6 +1023,7 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat) else if (thickness > 4) thickness = 4; cv::Scalar scalar(255, 255, 255); // white int fontSize = (int)(24.0 * ratio); + cv::Point pt; std::string fontPath; if (existsFile("/system/fonts/NotoSansCJK-Regular.ttc")) @@ -1075,24 +1076,60 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat) }; #endif cv::Scalar borderColor(m_pRecognizationCfg->borderColor & 0xFF, (m_pRecognizationCfg->borderColor & 0xFF00) >> 8, (m_pRecognizationCfg->borderColor & 0xFF0000) >> 16); - for (std::vector::const_iterator it = objs.cbegin(); it != objs.cend(); ++it) + cv::Scalar textColor(m_pRecognizationCfg->textColor & 0xFF, (m_pRecognizationCfg->textColor & 0xFF00) >> 8, (m_pRecognizationCfg->textColor & 0xFF0000) >> 16); + for (std::vector::const_iterator it = objs.cbegin(); it != objs.cend();) { if (it->label >= m_pRecognizationCfg->items.size()) { + it = objs.erase(it); continue; } const IDevice::CFG_RECOGNIZATION::ITEM& item = m_pRecognizationCfg->items[it->label]; if (item.enabled == 0 || it->prob < item.prob) { + it = objs.erase(it); continue; } + + if ((mPhotoInfo.recognization & 0x2) != 0) + { + cv::Rect rc(it->x, it->y, it->w, it->h); + cv::rectangle(mat, rc, borderColor, m_pRecognizationCfg->thickness); + textSize = ft2->getTextSize(item.name, fontSize, thickness, &baseline); + textSize.height += baseline; + if (it->y > textSize.height) + { + pt.y = it->y - textSize.height - 4 - m_pRecognizationCfg->thickness; + } + else if (mat.rows - it->y - it->h > textSize.height) + { + pt.y = it->y + it->h + 4 + m_pRecognizationCfg->thickness; + } + else + { + // Inner + pt.y = it->y + 4 + m_pRecognizationCfg->thickness; + } + if (mat.cols - it->x > textSize.width) + { + pt.x = it->x; + } + else + { + pt.x = it->x + it->w - textSize.width; + } + #ifdef _DEBUG - ALOGD("Label: %d=%s (%f,%f)-(%f,%f)", it->label, item.name.c_str(), it->x, it->y, it->w, it->h); + char buf[128]; + snprintf(buf, sizeof(buf), "Draw Label: %d=%s (%f,%f)-(%f,%f) Text:(%d,%d)-(%d,%d)", + it->label, item.name.c_str(), it->x, it->y, it->w, it->h, pt.x, pt.y, textSize.width, textSize.height); + ALOGI(buf); #endif - cv::Rect rc(it->x, it->y, it->w, it->h); - cv::rectangle(mat, rc, borderColor, m_pRecognizationCfg->thickness); - // putText + + ft2->putText(mat, item.name, pt, fontSize, textColor, thickness, cv::LINE_AA, false); + } + ++it; } } } @@ -1115,17 +1152,8 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat) textSize = ft2->getTextSize(str, fontSize, -1, &baseline); ft2->putText(mat, str, cv::Point(0, mat.rows - fontSize - 20 * ratio), fontSize, scalarRed, -1, cv::LINE_AA, false); - - // text.putText(mat, str.c_str(), cv::Point(0, mat.rows - 20), scalar); - - // puttext::Pixel32 colorText = {255, 255, 255}; - // puttext::Pixel32 colorBorder = {0, 0, 0}; - - // textPaint.DrawText((puttext::Pixel32*)mat.data, wstr.c_str(), 16, colorText, colorBorder, 3); - #endif - cv::Point pt; for (vector::const_iterator it = mOsds.cbegin(); it != mOsds.cend(); ++it) { if (it->text.empty())