Qt桌面截图并使用libquant 库压缩图片发送

核心代码:
  int start = QDateTime::currentDateTime().currentMSecsSinceEpoch();
    QPixmap image =  screen->grabWindow(0);
    QBuffer buffer;
//    qDebug()<<_pixWidth<<_pixHeight;
    image = image.scaled(_pixWidth,_pixHeight,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    image.save(&buffer,"JPEG");
    qDebug()<<image.save("C:\\Users\\9DVR\\Desktop\\four\\quantized_b.jpg","jpg");
    int end = QDateTime::currentDateTime().currentMSecsSinceEpoch();
    qDebug()<<start<<end<<end-start;
    time->stop();
    return;

    unsigned int width, height;
    unsigned char *raw_rgba_pixels;
    unsigned int status = lodepng_decode32(&raw_rgba_pixels, &width, &height, (unsigned char*)buffer.data().data(),buffer.size());
    if (status) {
        qDebug()<<"load image error:"<<lodepng_error_text(status);
        return;
    }
    liq_attr *handle = liq_attr_create();
    liq_image *input_image = liq_image_create_rgba(handle, raw_rgba_pixels, width, height, 0);
    liq_set_quality(handle,50,60);
    liq_result *quantization_result = NULL;
    quantization_result = liq_quantize_image(handle, input_image) ;
    if (quantization_result  == NULL) {
        qDebug()<< "Quantization failed\n";
        return ;
    }

    size_t pixels_size = width * height;

    unsigned char *raw_8bit_pixels = (unsigned char *)malloc(pixels_size);
    liq_set_dithering_level(quantization_result, 1.0);

    liq_write_remapped_image(quantization_result, input_image, raw_8bit_pixels, pixels_size);
    const liq_palette *palette = liq_get_palette(quantization_result);


    LodePNGState state;
    lodepng_state_init(&state);
    state.info_raw.colortype = LCT_PALETTE;
    state.info_raw.bitdepth = 8;
    state.info_png.color.colortype = LCT_PALETTE;
    state.info_png.color.bitdepth = 8;

    for(int i=0; i < palette->count; i++) {
       lodepng_palette_add(&state.info_png.color, palette->entries[i].r, palette->entries[i].g, palette->entries[i].b, palette->entries[i].a);
       lodepng_palette_add(&state.info_raw, palette->entries[i].r, palette->entries[i].g, palette->entries[i].b, palette->entries[i].a);
    }

    unsigned char *output_file_data;
    size_t output_file_size;
    unsigned int out_status = lodepng_encode(&output_file_data, &output_file_size, raw_8bit_pixels, width, height, &state);
    if (out_status) {
        qDebug()<<"Can't encode image:"<<lodepng_error_text(out_status);
        return ;
    }

    liq_result_destroy(quantization_result);
    liq_image_destroy(input_image);
    liq_attr_destroy(handle);
    free(raw_8bit_pixels);
    lodepng_state_cleanup(&state);
    buffer.data().data();

     ImageData img ;
     img.size = output_file_size;
     memset(img.imgData,0,sizeof(img.imgData));
     memcpy(img.imgData,output_file_data,output_file_size);
完整代码下载地址:http://download.csdn.net/download/xzpblog/10215632

猜你喜欢

转载自blog.csdn.net/xzpblog/article/details/79114837