现在大量图片使用jpg格式保存,要使用虹软SDK进行人脸识别,则需要解压。libjpeg是一个很好的解压库。
准备
在代码文件夹中建立一个jpg文件夹,把一张头像图片命名为100000000000000001.jpg放进去备用。
本次使用的图片来源于网络
安装好libgpeg
调用libjpeg进解压
把libjpeg的解压功能单独写一个文件
jpeg.h
#ifdef __cplusplus
extern "C" {
#endif
int jpg2bgr(unsigned char* pjpg_data, int jpeg_size, unsigned char** ppbgr_data, int* width, int* height);
#ifdef __cplusplus
}
#endif
jpeg.c
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <string.h>
#include "jpeg.h"
//解码jpg数据到bgr888
int jpg2bgr(unsigned char* pjpg_data, int jpeg_size, unsigned char** ppbgr_data, int* width, int* height){
//
struct jpeg_decompress_struct dinfo;
struct jpeg_error_mgr jerr;
dinfo.err = jpeg_std_error(&jerr);
//设置jpeg解码器的参数
jpeg_create_decompress(&dinfo);
jpeg_mem_src (&dinfo, pjpg_data, jpeg_size);
jpeg_read_header(&dinfo, TRUE);
*width = dinfo.image_width;
*height = dinfo.image_height;
//bgr格式宽度要4对齐,高度不限。
if(*width%4)
{
*width = *width + 4 - *width%4;
}
//height = height + height%4;
//printf("photo width: %d, height: %d, clolor space: %d, components: %d\n", dinfo.image_width, dinfo.image_height, dinfo.jpeg_color_space, dinfo.num_components);
//printf("photo width: %d, height: %d\n", *width, *height);
dinfo.scale_num=1;
dinfo.scale_num=1;
dinfo.out_color_space=JCS_EXT_BGR;
jpeg_start_decompress(&dinfo);
//一行缓冲区
int row_stride = dinfo.output_width * dinfo.output_components;
JSAMPROW row_pointer[1];
row_pointer[0] = (unsigned char*)malloc(row_stride);
//rgb全部数据
*ppbgr_data = (unsigned char*)realloc(*ppbgr_data, *width * *height * 3);
/* 逐行解压缩图像 */
while (dinfo.output_scanline < dinfo.output_height) {
jpeg_read_scanlines(&dinfo, row_pointer, 1);
memcpy(*ppbgr_data + (dinfo.output_scanline - 1) * *width * 3, row_pointer[0], row_stride);
}
/* 完成解压缩 */
jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
/* 释放一行缓冲区 */
free(row_pointer[0]);
return 0;
}
调用
int ret = -1;
unsigned char* pjpg_data = NULL;
unsigned char* pbgr_data = (unsigned char*)malloc(1);
int width, height;
//文件名
char jpg_name[300];
sprintf(jpg_name, "../jpg/100000000000000001.jpg");
//打开文件
FILE* fpjpg = fopen(jpg_name, "rb");
//查文件的长度
fseek(fpjpg, 0, SEEK_END);
long file_length = ftell(fpjpg);
fseek(fpjpg, 0, SEEK_SET);
//分配内存
pjpg_data = (unsigned char*)malloc(file_length);
//读取数据
fread(pjpg_data, 1, file_length, fpjpg); //jpeg文件数据
//关闭文件
fclose(fpjpg);
//把jpg图片解码成bgr数据
jpg2bgr(pjpg_data, file_length, &pbgr_data, &width, &height);
//释放文件缓存
free(pjpg_data);
printf("pic %s width: %d, height: %d \n", jpg_name, width, height);
return 0;
编译测试
cmake文件增加debug,源代码,库
set(CMAKE_BUILD_TYPE "Debug") #设置为debug
add_executable(arcsoft_face_engine_test
./inc/amcomdef.h
./inc/arcsoft_face_sdk.h
./inc/asvloffscreen.h
./inc/merror.h
./database.c
./database.h
./jpeg.h
./jpeg.c
./samplecode.cpp)
target_link_libraries(arcsoft_face_engine_test
arcsoft_face
arcsoft_face_engine
sqlite3
jpeg
)
cmake并且make,运行
face/samplecode/ASFTestDemo/build$ ./arcsoft_face_engine_test
pic ../jpg/100000000000000001.jpg width: 1200, height: 901
能正常拿到图片的宽度,高度数值。