#include <libuvc/libuvc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
void save_jpeg(const char *filename, unsigned char *image_buffer, int width, int height) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *outfile;
JSAMPROW row_pointer[1];
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "Can't open %s\n", filename);
return;
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3; // تعداد کانالهای رنگ (RGB)
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image_buffer[cinfo.next_scanline * width * 3];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
}
void frame_callback(uvc_frame_t *frame, void *ptr) {
unsigned char *image_buffer = malloc(frame->width * frame->height * 3); // فرض بر این است که تصویر RGB است.
save_jpeg("captured_image.jpg", image_buffer, frame->width, frame->height);
free(image_buffer);
}
int main() {
uvc_context_t *ctx;
uvc_device_t *dev;
uvc_device_handle_t *devh;
uvc_stream_ctrl_t ctrl;
if (uvc_init(&ctx, NULL) < 0) {
fprintf(stderr, "Error initializing libuvc\n");
return -1;
}
if (uvc_find_device(ctx, &dev, 0, 0, NULL) < 0) {
fprintf(stderr, "Device not found\n");
return -1;
}
if (uvc_open(dev, &devh) < 0) {
fprintf(stderr, "Unable to open device\n");
return -1;
}
uvc_get_stream_ctrl_format_size(devh, &ctrl, UVC_FRAME_FORMAT_YUYV, 640, 480, 30);
uvc_start_streaming(devh, &ctrl, frame_callback, NULL, 0);
printf("Press Enter to stop streaming...\n");
getchar();
uvc_stop_streaming(devh);
uvc_close(devh);
uvc_unref_device(dev);
uvc_exit(ctx);
return 0;
}
gcc main.c -luvc -ljpeg
./web: error while loading shared libraries: libuvc.so.0: cannot open shared object file: No such file or directory