在虹软SDK中调用libjpeg

现在大量图片使用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

能正常拿到图片的宽度,高度数值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值