Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
NareshPalle
Posts: 27
Registered: ‎04-20-2012
My Device: non-BlackBerry user
My Carrier: My company

image cropping in bb10 native app development

I am developing an image cropping application using libimg library. i am able to load the image file from application  memory when i tried to crop the image file using img_crop() function it is returning IMG_ERR_PARM error.  please let me know why this error is coming and is there any sample code available for image cropping using libimg library.

 

 

Please use plain text.
BlackBerry Development Advisor
ChrisCameron
Posts: 7
Registered: ‎01-21-2013
My Device: Z10
My Carrier: Rogers

Re: image cropping in bb10 native app development

[ Edited ]

Hi, I work on this image library at QNX. Could you please share a short example of what you are trying to do?

 

The most common causes for IMG_ERR_PARM are:

 

1. Your source image does not have valid width, height, and format (it checks to make sure the flags IMG_W, IMG_H, and IMG_FORMAT are set)

2. Your destination image does not have valid width and height (it checks to make sure the IMG_W and IMG_H flags are set)

3. Your crop co-ordinates are not compatible with the source image, e.g., (x + dst->w > src->w) or (y + dst->h > src->h)

4. Your destination image is bigger than your source image

 

There are other possibilities but they are much less likely. What kind of image are you trying to crop?

 

Here is a brief code snippet that I wrote that loads an image and saves a cropped version:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <img/img.h>

int main(int argc, char ** argv)
{
	if (argc != 3) {
		printf("Usage: %s input filename output_filename \n", argv[0]);
		printf(" e.g. %s /path/to/input.jpg /path/to/result.jpg \n", argv[0]);
		return 1;
	}

	// attach image library (loads codecs)
	img_lib_t ilib;
	int rc = img_lib_attach(&ilib);
	if (rc != IMG_ERR_OK) {
		printf("img_lib_attach() failed: %d\n", rc);
		return 1;
	}

	// load image
	img_t img;
	memset(&img, 0, sizeof(img_t));
	rc = img_load_file(ilib, argv[1], NULL, &img);
	if (rc != IMG_ERR_OK) {
		printf("img_load_file() failed: %d\n", rc);
		return 1;
	}

	img_t cropped;
	memset(&cropped, 0, sizeof(img_t));
	// the following configuration tells libimg: give me a 500x500 crop of the source image. The
	// The crop rectangle's left edge (x) will be the left edge of the source image, and the
	// crop rectangle's top edge (y) will be 500 lines from the bottom of the source image. I.e.,
	// I'm asking for the bottom right corner of the source image. Note that this works because
	// my source image is 1836x3264
	cropped.w = 500;
	cropped.h = 500;
	cropped.flags |= (IMG_W | IMG_H);
	rc = img_crop(&img, &cropped, 0, img.h-500);
	if (rc != IMG_ERR_OK) {
		printf("img_crop() failed: %d\n", rc);
		return 1;
	}

	// do whatever you planned to do with the cropped image here. I saved it to disk to verify
	// the crop
	rc = img_write_file(ilib, argv[2], NULL, &cropped);
	if (rc != IMG_ERR_OK) {
		printf("img_write_file() failed: %d\n", rc);
		return 1;
	}

	//Note: When you are done with the data, make sure you free() the image data (in the
	//      .access.direct.data pointer) when flags & IMG_DIRECT is true. Also at the end,
	//      please call img_lib_detach().

	return 0;
}

 

Note it is not a completely proper example. You should read the documentation to make sure you free the image data and detach from the image library at the appropriate time.

Please use plain text.