查看: 826|回复: 0

[原创] sd_jpeg代码分享

[复制链接]

该用户从未签到

656

主题

6312

帖子

0

超级版主

Rank: 8Rank: 8

积分
20015
最后登录
2024-4-25
发表于 2021-8-16 17:55:07 | 显示全部楼层 |阅读模式
sd_jpeg代码分享

上周在帮客户调试sd_jpeg工程代码的过程中,在原工程基础上增加了libjpeg的图片压缩函数,实现从SD卡读取jpg图片并将图像元数据另外存储成jpg图片后,再通过LCD屏显示,其中图片压缩函数代码如下所示。

  1. /*!
  2. * @brief  Jpeg compress
  3. * @param  file:    pointer to the jpg raw data
  4. * e
  5. */
  6. status_t JpegCompress(uint8_t* buffer)
  7. {
  8.     struct jpeg_compress_struct cinfo;
  9.     struct jpeg_error_mgr jerr;
  10.     uint8_t* scanlines = buffer;
  11.     JSAMPROW row_pointer;
  12.     uint32_t row_stride     = 0;   /* physical row width in image buffer */
  13.     FRESULT error;

  14.     FILE fJpeg;
  15.     char JpegFilename[20];
  16.     static uint32_t JpegFilenameIndex = 0u;

  17.     PRINTF("Start of JPEG Compression... ");

  18.     if(JpegFilenameIndex > 9999u)
  19.     {
  20.         JpegFilenameIndex = 0u;
  21.     }

  22.     sprintf(JpegFilename, "/IMG_%04d.jpg", ++JpegFilenameIndex);

  23.     error = f_open(&fJpeg, _T(JpegFilename), (FA_WRITE | FA_CREATE_ALWAYS));
  24.     if (error)
  25.     {
  26.         if (error == FR_EXIST)
  27.         {
  28.             PRINTF("File exists.\r\n");
  29.         }
  30.         else
  31.         {
  32.             PRINTF("Open file failed.\r\n");
  33.             return -1;
  34.         }
  35.     }

  36.     /* Initialize the JPEG compression object with default error handling. */
  37.     cinfo.err = jpeg_std_error(&jerr);
  38.     jpeg_create_compress(&cinfo);

  39.     /* Initialize JPEG parameters.*/
  40.     cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
  41.     jpeg_set_defaults(&cinfo);

  42.     /* Specify the source format. */
  43.     cinfo.in_color_space = JCS_RGB;
  44.     cinfo.input_components = 3u;
  45.     cinfo.data_precision = 8u;
  46.     cinfo.image_width = (JDIMENSION)APP_FB_WIDTH;
  47.     cinfo.image_height = (JDIMENSION)APP_FB_HEIGHT;

  48.     /* Now that we know input colorspace, fix colorspace-dependent defaults */
  49.     jpeg_default_colorspace(&cinfo);

  50.     /* Specify data destination for compression */
  51.     jpeg_stdio_dest(&cinfo, &fJpeg);

  52.     jpeg_set_quality(&cinfo, 100u, true);

  53.     /* Start compressor */
  54.     jpeg_start_compress(&cinfo, TRUE);
  55.     row_stride = APP_FB_STRIDE_BYTE;
  56.     /* Process data */
  57.     while (cinfo.next_scanline < cinfo.image_height)
  58.     {
  59.         row_pointer = (JSAMPROW)(scanlines+cinfo.next_scanline * row_stride);
  60.         jpeg_write_scanlines(&cinfo, &row_pointer, COMPRESS_LINES);
  61.     }

  62.     /* Finish compression and release memory */
  63.     jpeg_finish_compress(&cinfo);
  64.     jpeg_destroy_compress(&cinfo);

  65.     f_close(&fJpeg);

  66.     PRINTF("Done: %s is saved. \r\n", JpegFilename);

  67.     return kStatus_Success;
  68. }
复制代码

evkbimxrt1050_sd_jpeg.zip (2.45 MB, 下载次数: 3)

回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2024-4-25 20:23 , Processed in 0.133554 second(s), 19 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表