URL에서 JPEG 이미지를 다운로드하고이를 PNG로 디스크에 저장하는 알고리즘을 구축하려고합니다. 는 다운로드 내가 libcurl에 사용했습니다이를 달성하고, 여기에libCurl 및 GdkPixBuff를 사용하여 JPEG에서 PNG로 이미지 저장
데이터를 달성하기 위해 코드 (나는 GDK 라이브러리 잘 접착하고있어 프로젝트 제한에 대한) 다른 거즈에 대한 GdkPixbuff 라이브러리 :
CURL *curl;
GError *error = NULL;
struct context ctx;
memset(&ctx, 0, sizeof(struct context));
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, *file_to_download*);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDownloadedPic);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ctx);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
을 이러한 방식 writeDownloadedPic
struct context
{
unsigned char *data;
int allocation_size;
int length;
};
및 을 : 컨텍스트 그런 정의
,617size_t writeDownloadedPic (void *buffer, size_t size, size_t nmemb, void *userp)
{
struct context *ctx = (struct context *) userp;
if(ctx->data == NULL)
{
ctx->allocation_size = 31014;
if((ctx->data = (unsigned char *) malloc(ctx->allocation_size)) == NULL)
{
fprintf(stderr, "malloc(%d) failed\n", ctx->allocation_size);
return -1;
}
}
if(ctx->length + nmemb > ctx->allocation_size)
{
fprintf(stderr, "full\n");
return -1;
}
memcpy(ctx->data + ctx->length, buffer, nmemb);
ctx->length += nmemb;
return nmemb;
}
내가 그런 식으로 이미지를 저장하려고 결국: 내가 가진 것을
GdkPixbuf *pixbuf;
pixbuf = gdk_pixbuf_new_from_data(ctx.data,
GDK_COLORSPACE_RGB,
FALSE, 8,
222, 310,
222 * 3,
NULL, NULL);
gdk_pixbuf_save(pixbuf, "src/pics/image.png", "png", &error, NULL);
하지만, 임의 픽셀의 무리와 함께 이미지를 PNG로 전혀 형성하지 않는 아가씨입니다. 지금, 나는 이미지, 폭과 높이의 확인 차원에 대해 알아,하지만 난 내가 내가 잘못 폭 * 3
로 계산 한 RowStride 일부 혼란을 할 것 같아요?
나는 GInputStream 메서드를 좋아한다. CURL의 쓰기 콜백에 * g_input_stream_read * 함수를 사용하려고 시도했지만 처음 읽은 attemp 이후에 전체 응용 프로그램이 중단됩니다! – Archedius
가장 쉬운 방법은'gdk_pixbuf_new_from_stream'을 사용하고 ['g_memory_input_stream_new_from_data()'] (http://developer.gnome.org/gio/unstable/GMemoryInputStream.html#g-memory- 입력 - 스트림 - 새로운 - 데이터에서). 이런 방식으로 컬을 다운로드하는 로직은 단순하게 유지되며, gdk pixbuf는'gdk_pixbuf_new_from_stream'에서 지원되는 모든 종류의 리소스를로드 할 수 있습니다. – user4815162342