//LOGI("Received image with width: %d height: %d", width, height);
uint8_t *srcLumaPtr = reinterpret_cast<uint8_t *>(env->GetDirectBufferAddress(bufferY));
if (srcLumaPtr == nullptr) {
LOGE("blit NULL pointer ERROR");
return;
}
cv::Mat mYuv(height + height / 2, width, CV_8UC1, srcLumaPtr);
//LOGI("YUV : %d", mYuv.rows);
ANativeWindow *win = ANativeWindow_fromSurface(env, surface);
ANativeWindow_acquire(win);
ANativeWindow_Buffer buf;
int rotatedWidth = height; // 480
int rotatedHeight = width; // 640
ANativeWindow_setBuffersGeometry(win, width, height, 0);
if (int32_t err = ANativeWindow_lock(win, &buf, NULL)) {
LOGE("ANativeWindow_lock failed with error code %d\n", err);
ANativeWindow_release(win);
return;
}
// LOGI("buf.stride: %d", buf.stride);
uint8_t *dstPtr = reinterpret_cast<uint8_t *>(buf.bits);
Mat dstRgba(height, buf.stride, CV_8UC4, dstPtr); // TextureView buffer, use stride as width
Mat srcRgba(height, width, CV_8UC4);
Mat rotatedRgba(rotatedHeight, rotatedWidth, CV_8UC4);
// convert YUV to RGBA
if(bufferLength < height * width * 1.6f) {
cv::cvtColor(mYuv, srcRgba, COLOR_YUV2BGRA_I420, 3);
}else {
cv::cvtColor(mYuv, srcRgba, COLOR_YUV2BGRA_NV21, 3);
}
// Rotate 90 degree
cv::rotate(srcRgba, rotatedRgba, cv::ROTATE_90_CLOCKWISE);
assert(rotatedRgba.size().width == height);
assert(rotatedRgba.size().height == width);
//process image here
//cv::circle(rotatedRgba, cv::Point(150,100), 5, Scalar(255,0,0,255), 2, LINE_8, 0);
if (patternDetected) {
patternDetector.detect(rotatedRgba);
//patternDetector.trackStatus;
}else{
if(isScanning) {
scanEffectHelper.scanEffect(rotatedRgba);
}
//__android_log_print(ANDROID_LOG_VERBOSE, "patternDetector", "no image");
}
cv::rotate(rotatedRgba, srcRgba, cv::ROTATE_90_COUNTERCLOCKWISE);
// copy to TextureView surface
uchar *dbuf = dstRgba.data;
uchar *sbuf = srcRgba.data;
int i;
for (i = 0; i < srcRgba.rows; i++) {
dbuf = dstRgba.data + i * buf.stride * 4;
memcpy(dbuf, sbuf, srcRgba.cols * 4); //TODO: threw a SIGSEGV SEGV_ACCERR once
sbuf += srcRgba.cols * 4;
}
ANativeWindow_unlockAndPost(win);
ANativeWindow_release(win);
//LOGI("Draw image with width: %d height: %d", srcRgba.cols, srcRgba.rows);
in log it sbows like this like this
Available Preview Size 176 x 144 Max =640x480 Aspect =4000x2250
Preview Size 640 x 480
onImageAvailable 640 x 480 imageBytes.length 981886 UV_rowStride 1024 Y_rowStride 1024 U_pixelStride 2 Y_pixelStride 1