sd_jpeg代码分享 上周在帮客户调试sd_jpeg工程代码的过程中,在原工程基础上增加了libjpeg的图片压缩函数,实现从SD卡读取jpg图片并将图像元数据另外存储成jpg图片后,再通过LCD屏显示,其中图片压缩函数代码如下所示。 - /*!
- * @brief Jpeg compress
- * @param file: pointer to the jpg raw data
- * e
- */
- status_t JpegCompress(uint8_t* buffer)
- {
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- uint8_t* scanlines = buffer;
- JSAMPROW row_pointer;
- uint32_t row_stride = 0; /* physical row width in image buffer */
- FRESULT error;
- FILE fJpeg;
- char JpegFilename[20];
- static uint32_t JpegFilenameIndex = 0u;
- PRINTF("Start of JPEG Compression... ");
- if(JpegFilenameIndex > 9999u)
- {
- JpegFilenameIndex = 0u;
- }
- sprintf(JpegFilename, "/IMG_%04d.jpg", ++JpegFilenameIndex);
- error = f_open(&fJpeg, _T(JpegFilename), (FA_WRITE | FA_CREATE_ALWAYS));
- if (error)
- {
- if (error == FR_EXIST)
- {
- PRINTF("File exists.\r\n");
- }
- else
- {
- PRINTF("Open file failed.\r\n");
- return -1;
- }
- }
- /* Initialize the JPEG compression object with default error handling. */
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_compress(&cinfo);
- /* Initialize JPEG parameters.*/
- cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
- jpeg_set_defaults(&cinfo);
- /* Specify the source format. */
- cinfo.in_color_space = JCS_RGB;
- cinfo.input_components = 3u;
- cinfo.data_precision = 8u;
- cinfo.image_width = (JDIMENSION)APP_FB_WIDTH;
- cinfo.image_height = (JDIMENSION)APP_FB_HEIGHT;
- /* Now that we know input colorspace, fix colorspace-dependent defaults */
- jpeg_default_colorspace(&cinfo);
- /* Specify data destination for compression */
- jpeg_stdio_dest(&cinfo, &fJpeg);
- jpeg_set_quality(&cinfo, 100u, true);
- /* Start compressor */
- jpeg_start_compress(&cinfo, TRUE);
- row_stride = APP_FB_STRIDE_BYTE;
- /* Process data */
- while (cinfo.next_scanline < cinfo.image_height)
- {
- row_pointer = (JSAMPROW)(scanlines+cinfo.next_scanline * row_stride);
- jpeg_write_scanlines(&cinfo, &row_pointer, COMPRESS_LINES);
- }
- /* Finish compression and release memory */
- jpeg_finish_compress(&cinfo);
- jpeg_destroy_compress(&cinfo);
- f_close(&fJpeg);
- PRINTF("Done: %s is saved. \r\n", JpegFilename);
- return kStatus_Success;
- }
复制代码
evkbimxrt1050_sd_jpeg.zip
(2.45 MB, 下载次数: 3)
|