You’d better use other frameworks other than OpenCV. OpenCV is extremely heavy for this kind of job. It’s mainly focused on image processing.
Maybe you can use OpenImageIO, freeimage or other libs.
You can refer to these posts:
Reading an image file in C/C++
solved How to build opencv that only support decode jpeg?