我想要的:
將現有的jpeg調整爲新的較小的jpeg。閱讀JPEG,調整大小並將其保存到磁盤
例子:before_3000x2000.jpeg→→→after_1024x768.jpeg
我想我需要僞功能,如這些:
富(before_file_path,after_file_path,new_width,new_height)
其利用
decompress_jpeg的(before_f ile_path)
調整大小(decompressed_data,new_width,new_height)
compress_jpeg(resized_data)
save_jpeg(compressed_data,after_file_path)
請糾正我的方法,如果我誤解的東西。
我想什麼:
- 由於分辨率會有所不同,首先你要解壓縮現有的JPEG這樣你就可以正確地與像素的數據。
- 其次,你用一些函數foo()將pixeldata大小調整到一個較小的緩衝區。
- 第三,你壓縮所述緩衝區並將其保存到磁盤。
我想什麼(失敗):
對於初學者來說,我試圖與
write_JPEG_file(字符*文件名,INT質量)
功能提供玩弄in example.c of jpeglib-9b。我只是想以我用單一顏色指定的任何分辨率寫出測試圖片。但我遇到了一個問題。我認爲我正在寫隨機記憶,我不知道爲什麼。 (這裏是一張圖片:img_100x100)請注意,頂部有白色到某一點。這部分相應地改變了我在第一個循環中分配的內容。但是在一定量之後,它會變成隨機噪聲。 爲什麼?
我改變了:
我定義image_width, image_height, image_buffer
我更換了所有fopen(...)
與fopen_s(...)
。
我硬編碼在jpeg_set_quality(...)
質量,100碼:
#include <iostream>
#include "jpeg-9b\jpeglib.h"
#include "jpeg-9b\jerror.h"
int TEST_WRITE_JPEG(char* file_name)
{
// DEFINE A PIC HERE
const int image_width = 100; /* Number of columns in image */
const int image_height = 100; /* Number of rows in image */
JSAMPLE* image_buffer = new JSAMPLE[image_width * image_height](); /* Points to large array of R,G,B-order data */
for (int i = 0; i < image_width*image_height; i++)
{
image_buffer[i] = 255;
}
/* Step 1: allocate and initialize JPEG compression object */
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
/* Step 2: specify data destination (eg, a file) */
if (fopen_s(&outfile, file_name, "wb") != NULL)
{
fprintf(stderr, "can't open %s\n", file_name);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
/* Step 3: set parameters for compression */
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = 3; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE);
/* Step 4: Start compressor */
jpeg_start_compress(&cinfo, TRUE);
/* Step 5: while (scan lines remain to be written) */
row_stride = image_width * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (JSAMPROW)&image_buffer[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile);
/* Step 7: release JPEG compression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_compress(&cinfo);
return 0;
}
清晰的問題:
爲什麼我寫出來this圖片,而不是一個純白色的?
如果image_buffer
包含多個128x128無符號字符,爲什麼程序崩潰?
你不想這樣做。你想用較低的Q值來保存它。這種方法會使數據產生像素化。 – EJP
@EJP我不想做什麼具體?我假設你在談論調整大小的部分?請你詳細說明一下嗎? –