Menu
Sites
Language

Crop Jpeg file

Example how to crop jpeg image from file using 'Image Util'. Here ad->fileList[ad->curIndex] is some char string with path to file. http://tizen.org/privilege/mediastorage should be in manifest.
typedef struct appdata {
//...
    int croppedX1;
    int croppedY1;
    int croppedX2;
    int croppedY2;
} appdata_s;

static void completed_callback(media_packet_h *dst, int error_code, void *user_data)
{
    appdata_s *ad = (appdata_s *)user_data;
    void * ptr;
    media_packet_get_buffer_data_ptr(*dst, &ptr);
    image_util_encode_jpeg((unsigned char*)ptr, ad->croppedX2 - ad->croppedX1, ad->croppedY2 - ad->croppedY1
                                               , IMAGE_UTIL_COLORSPACE_RGB888, 100, "/opt/usr/media/cropped.jpg");
    media_packet_destroy(*dst);
}

int _finalize_callback(media_packet_h packet, int err, void* userdata)
{
   return MEDIA_PACKET_FINALIZE;
}

static void cropImage(appdata_s *ad)    
{    
    void* src_ptr;
    media_format_h fmt;
    media_packet_h src;
    transformation_h handle;

    image_util_transform_create(&handle);

    unsigned char *img_source = NULL;
    int width = 0, height = 0;
    unsigned int size_decode = 0;

    int ret=image_util_decode_jpeg( ad->fileList[ad->curIndex], (image_util_colorspace_e)IMAGE_UTIL_COLORSPACE_RGB888, &img_source, &width, &height, &size_decode);

    if (ret!=0) return;
 
    ad->croppedX1 =100;
    ad->croppedY1 =100;
    ad->croppedX2 =width-100;
    ad->croppedY2 =height-100;

    if(ad->croppedX1<0)  ad->croppedX1 = 0;
    if(ad->croppedY1<0)  ad->croppedX1 = 0;
    if(ad->croppedX2>width)  ad->croppedX2 = width;
    if(ad->croppedY2>height)  ad->croppedY2 = height;

    media_format_create(&fmt);
    media_format_set_video_mime(fmt, MEDIA_FORMAT_RGB888);
    media_format_set_video_width(fmt, width);
    media_format_set_video_height(fmt, height);

    media_packet_create_alloc(fmt, _finalize_callback, NULL, &src);
    image_util_transform_set_crop_area(handle, ad->croppedX1,ad->croppedY1,
                                               ad->croppedX2,ad->croppedY2);

    media_packet_get_buffer_data_ptr(src, &src_ptr);
    memcpy(src_ptr, img_source, size_decode);

    image_util_transform_run(handle, src , (image_util_transform_completed_cb)completed_callback, ad);
    free(img_source);

    media_packet_destroy(src);
    image_util_transform_destroy(handle);
}

Responses

1 Replies
Alex Dem

Also, fyi:
This code snippet applicable if Exif Orientation is ORIENTATION_NORMAL (equal 1)
How to get exif orientation 
https://developer.tizen.org/community/code-snippet/native-code-snippet/how-get-exif-orientation
In other cases you need to perform rotation transformation with ret = image_util_transform_set_rotation before.
Alexey.