qq_23515451 2017-09-22 08:44 采纳率: 0%
浏览 1570
已结题

linux下使用IJG压缩bmp到jpg时报错,求解决办法!

错误提示是:Empty JPEG image (DNL not supported)

  • 写回答

1条回答 默认 最新

  • weixin_40254892 2017-09-22 09:55
    关注

    一、建立编译环境
      所谓建立编译环境,其实非常简单,就是把上面提到的4个文件拷贝到你的项目文件夹下,把libjpeg.lib 添加到你的项目中,然后在你完成 压缩功能的那个文件里加入#include "jpeglib.h",需要注意的是,libjpeg.lib是用c语言开发的,如果要用在你的C++程序里,需要用到extern "C",如下:
    // TestLibjpeg.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include "memory.h"
    extern "C" {
    #include "jpeglib.h"
    }
      二、压缩步骤
      1、申请并初始化jpeg压缩对象,同时要指定错误处理器
    struct jpeg_compress_struct jcs;
    // 声明错误处理器,并赋值给jcs.err域
    struct jpeg_error_mgr jem;
    jcs.err = jpeg_std_error(&jem);
    jpeg_create_compress(&jcs);
      2、指定压缩后的图像所存放的目标文件,注意,目标文件应以二进制模式打开
    f=fopen("03.jpg","wb");
    if (f==NULL)
    {
    delete [] data;
    delete [] pDataConv;
    return 0;
    }
    jpeg_stdio_dest(&jcs, f);
      3、设置压缩参数,主要参数有图像宽、高、色彩通道数(1:索引图像,3:其他),色彩空间(JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像),压缩质量等,如下:
    jcs.image_width = nWidth; // 为图的宽和高,单位为像素
    jcs.image_height = nHeight;
    jcs.input_components = 1; // 在此为1,表示灰度图, 如果是彩色位图,则为3
    jcs.in_color_space = JCS_GRAYSCALE; //JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像
    jpeg_set_defaults(&jcs);
    jpeg_set_quality (&jcs, 80, true);
    需要注意的是,jpeg_set_defaults函数一定要等设置好图像宽、高、色彩通道数计色彩空间四个参数后 才能调用,因为这个函数要用到这 四个值,调用jpeg_set_defaults函数后,jpeglib库采用默认的设置对图像进行压缩,如果需要改变设置,如压缩质量,调用这个函数 后,可以调用其它设置函数,如jpeg_set_quality函数。其实图像压缩时有好多参数可以设置,但大部分我们都用不着设置,只需调用 jpeg_set_defaults函数值为默认值即可。
      4、上面的工作准备完成后,就可以压缩了,压缩过程非常简单,首先调用 jpeg_start_compress,然后可以对每一行进行压缩,也 可以对若干行进行压缩,甚至可以对整个的图像进行一次压缩,压缩完成后,记得要调用jpeg_finish_compress函数,如下:
    jpeg_start_compress(&jcs, TRUE);
    JSAMPROW row_pointer[1]; // 一行位图
    int row_stride; // 每一行的字节数
    row_stride = jcs.image_width; // 如果不是索引图,此处需要乘以3
    // 对每一行进行压缩
    while (jcs.next_scanline < jcs.image_height) {
    row_pointer[0] = & pDataConv[jcs.next_scanline * row_stride];
    jpeg_write_scanlines(&jcs, row_pointer, 1);
    }
    jpeg_finish_compress(&jcs);
      5、最后就是释放压缩工作过程中所申请的资源了,主要就是jpeg压缩对象,由于在本例中我是直接用的局部变量,所以只需调用jpeg_destroy_compress这个函数即可,如下:
    jpeg_destroy_compress(&jcs);
      三、解压缩步骤
    解压缩步骤与压缩步骤非常相似,只是解压缩对象为jpeg_decompress_struct类型,步骤如下:
    1、声明并初始化解压缩对象,同时制定错误信息管理器
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);
    2、打开jpg图像文件,并指定为解压缩对象的源文件
    FILE *f = fopen(strSourceFileName,"rb");
    if (f==NULL)
    {
    printf("Open file error!\n");
    return;
    }
    //
    jpeg_stdio_src(&cinfo, f);
    3、读取图像信息
    jpeg_read_header(&cinfo, TRUE);
    4、根据图像信息申请一个图像缓冲区
    data = new BYTE cinfo.image_width*cinfo.image_height*cinfo.num_components];
    5、开始解压缩
    jpeg_start_decompress(&cinfo);
    JSAMPROW row_pointer[1];
    while (cinfo.output_scanline < cinfo.output_height)
    {
    row_pointer[0] = &data[(cinfo.output_height - cinfo.output_scanline-1)*cinfo.image_width*cinfo.num_components];
    jpeg_read_scanlines(&cinfo,row_pointer ,
    1);
    }
    jpeg_finish_decompress(&cinfo);
    6、释放资源
    jpeg_destroy_decompress(&cinfo);
    fclose(f);
      好了,利用IJG JPEG Library进行图像压缩就介绍到这里,希望对大家有所帮助,实例代码已经实现了图像的压缩和解压缩的全部功能,命令格式 为:“TestLibjpeg.exe j|j24|b 源文件名 目标文件名”,其中,j选项将源文件压缩为jpg格式,不改变色彩模式,j24选项将源文件压缩为24为jpg格式,b选项将源文件解压缩为bmp格式, 该实例并没有提供文件有效性的验证,如果要引入自己的代码,请自己实现文件有效性的验证。
    linux下bmp转化为jpeg程序源代码

    /****************************************************************************

    名称: bmp-to-jpeg.c
    功能: linux下bmp转化为jpeg程序源代码
    注意: 编译时加“-ljpeg”(gcc -o mm bmp-to-jpeg.c -ljpeg)

    *****************************************************************************/

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define JPEG_QUALITY 80

    int main()
    {
    FILE fd;
    int ret;
    unsigned char *data;
    long sizeImage;
    sizeImage = 320*240*sizeof(unsigned char);
    data = (unsigned char
    )malloc(sizeImage);
    fd = fopen("palm.bmp", "rb");
    if(!fd)
    {
    printf("ERROR1: Can not open the image.\n");
    free(data);
    return -1;
    }

    // 跳过bmp文件头,直接读取掌纹图像数据
    fseek(fd, 1078, SEEK_SET);

    ret = fread(data, sizeof(unsigned char)*sizeImage, 1, fd);
    if(ret == 0)
    {
    if(ferror(fd))
    {
    printf("\nERROR2: Can not read the pixel data.\n");
    free(data);
    fclose(fd);
    return -1;
    }
    }

    char filename = "palm2.jpg";
    int width = 320;
    int height = 240;
    int depth = 1;
    //unsigned char *bits = "jsdgjksdjkgjks51sd536gsgjmskjgksjgss231h1b2s123z";
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *outfile; /
    target file /
    JSAMPROW row_pointer[1]; /
    pointer to JSAMPLE row[s] /
    int row_stride; /
    physical row width in image buffer */

    cinfo.err = jpeg_std_error(&jerr);
    /* Now we can initialize the JPEG compression object. */
    jpeg_create_compress(&cinfo);

    if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return -1;
    }
    jpeg_stdio_dest(&cinfo, outfile);

    cinfo.image_width = width; /* image width and height, in pixels /
    cinfo.image_height = height;
    cinfo.input_components = depth; /
    # of color components per pixel /
    cinfo.in_color_space = JCS_GRAYSCALE; /
    colorspace of input image */

    jpeg_set_defaults(&cinfo);
    /* Now you can set any non-default parameters you wish to.

    • Here we just illustrate the use of quality (quantization table) scaling: / jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE ); / limit to baseline-JPEG values */

    jpeg_start_compress(&cinfo, TRUE);

    row_stride = width; /* JSAMPLEs per row in image_buffer */

    while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = & data[cinfo.next_scanline * row_stride];
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    free(data);

    fclose(fd);
    fclose(outfile);

    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么