opencv nv12转jpg, 拿到jpg数据

typedef struct tagRect
{
    int CX;
    int CY;
    int Width;
    int Height;
}RECT;
typedef struct tagRect_ex {
    RECT rects[12];
}RECT_EX;

int test_nv12_2_jpeg(unsigned char* jpg_file_addr, unsigned char* nv12_addr, int nv12_width, int nv12_height,
    RECT* rects, int rect_nums)
{

    if (!jpg_file_addr || !nv12_addr || nv12_width <= 0 || nv12_height <= 0)
        return -1;
    int yuvNV12_size = nv12_width * nv12_height * 3 / 2;

    cv::Mat yuvNV12;
    cv::Mat rgb24;

    yuvNV12.create(nv12_height * 3 / 2, nv12_width, CV_8UC1);
    memcpy(yuvNV12.data, nv12_addr, yuvNV12_size);
    // nv12 mat to rgb24 mat
    cv::cvtColor(yuvNV12, rgb24, cv::COLOR_YUV2BGR_NV12);
    // draw react
    for (int i = 0; i < rect_nums; i++) {
        RECT* pRect = rects + i;
        cv::rectangle(rgb24, cv::Rect(pRect->CX, pRect->CY, pRect->Width, pRect->Height), cv::Scalar(0, 0, 255), 2, 8, 0);
}
//cv::imwrite("./cv_test.jpg", rgb24); /*int rgb24_size = mat_2_jpg(rgb24, (char*)jpg_file_addr); return rgb24_size;*/ { //方法1 IplImage image(rgb24); int params[] = { CV_IMWRITE_JPEG_QUALITY, 80, 0 }; CvMat* pJpeg = cvEncodeImage(".jpeg", &image, 0); memcpy(jpg_file_addr, pJpeg->data.ptr, pJpeg->rows * pJpeg->cols); return pJpeg->rows * pJpeg->cols; } // 方法2 if (rgb24.empty()) { return -2; } std::vector<uchar> buff_jpg; buff_jpg.resize(rgb24.rows * rgb24.cols); std::vector<int> param = std::vector<int>(2); param[0] = CV_IMWRITE_JPEG_QUALITY; param[1] = 50; //param[2] = 0; cv::imencode(".jpg", rgb24, buff_jpg, param); memcpy(jpg_file_addr, buff_jpg.data(), buff_jpg.size()); return buff_jpg.size(); }

 

int main(int argc, char* argv[])
{
    FILE* nv12fp = NULL;
    const int SIZE = 200;
    char filename[SIZE] = { 0 };
    unsigned char* pNV12Buffer = NULL;
    int  dwNV12Size = 0;
    snprintf(filename, SIZE, "C:/Users/walt/Desktop/fsdownload/vdec.yuv"); //nv12
    nv12fp = fopen(filename, "rb");
    if (nv12fp)
    {
        fseek(nv12fp, 0, SEEK_END);
        dwNV12Size = ftell(nv12fp);
        pNV12Buffer = (unsigned char*)malloc(dwNV12Size * sizeof(unsigned char));
        memset(pNV12Buffer, 0x00, dwNV12Size);
        rewind(nv12fp);
        dwNV12Size = fread(pNV12Buffer, 1, dwNV12Size, nv12fp);

        unsigned char jpgBuf[512 * 1024] = { 0 };
        RECT_EX item;
        item.rects[0].CX = 930;
        item.rects[0].CY = 387;
        item.rects[0].Width = 240;
        item.rects[0].Height = 315;

        item.rects[1].CX = 552;
        item.rects[1].CY = 669;
        item.rects[1].Width = 105;
        item.rects[1].Height = 102;
        int jpg_size = test_nv12_2_jpeg(jpgBuf, pNV12Buffer, 1920, 1080, item.rects, 2);
        if (jpg_size > 0)
        {
            remove("success_2.jpg");
            FILE* wFp = fopen("success_2.jpg", "wb+");
            if (wFp)
            {
                fwrite(jpgBuf, jpg_size, 1, wFp);
                fclose(wFp);
                wFp = NULL;
            }
        }

        free(pNV12Buffer);
        pNV12Buffer = NULL;
        fclose(nv12fp);
        nv12fp = NULL;
    }

    getchar();
    return 0;
}

 

 
posted @ 2023-03-09 09:22  一夜梦想  阅读(569)  评论(0编辑  收藏  举报