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
New Contributor
Posts: 2
Registered: ‎11-27-2012
My Device: 9000
My Carrier: XL Axiata

Crop image into certain shape

hi there is there any way to crop image into certain image such as ellipse or triangle ? or can image.copy() handle copying image pixel by pixel. 

if anyone has already made a method to crop a circular shape on an image please let me know 

 

best regards

 

Idham 

Contributor
Posts: 18
Registered: ‎03-30-2012
My Device: Playbook
My Carrier: Ropdgers

Re: Crop image into certain shape

Highlighted
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Crop image into certain shape

yes there is, and it's pretty straightforward.

 

take a look at this

 

bb::cascades::Image Triangle::fromQImage(const QImage &origQImage, Color color)
{
	bb::ImageData imageData(bb::PixelFormat::RGBA_Premultiplied, origQImage.width(), origQImage.height());
	int y = 0;

	unsigned char *dstLine = imageData.pixels();

	float red = color.red();
	float green = color.green();
	float blue = color.blue();

	for (y = 0; y < origQImage.height(); y++)
	{
		unsigned char * dst = dstLine;
		for (int x = 0; x < imageData.width(); x++)
		{
			QRgb srcPixel = origQImage.pixel(x, y);

//			*dst++ = qRed(srcPixel) * qAlpha(srcPixel) / 255;
//			*dst++ = qGreen(srcPixel) * qAlpha(srcPixel) / 255;
//			*dst++ = qBlue(srcPixel) * qAlpha(srcPixel) / 255;
//			*dst++ = qAlpha(srcPixel);

			*dst++ = red * qAlpha(srcPixel);
			*dst++ = green * qAlpha(srcPixel);
			*dst++ = blue * qAlpha(srcPixel);
			*dst++ = qAlpha(srcPixel);

		}
		dstLine += imageData.bytesPerLine();
	}

	return Image(imageData);
}

 

This is my code to create a one-colored Image which keeps the alpha values of my mask image. the inner loop is the key. What I am doing is using the mask image pixel by pixel, reading it's alpha value and multiply it with my color. That way I can use any image with alpha values between 0.0 and 1.0 (so, my 'Shape') to create e.g. a red version WITHOUT any structures

 

Since you want to create an image with a specific shape, just apply my function, but instead of multiplying it with a certain color, just give the function a second image, and put something like

 

*dst++ = qRed(imagePixel) * qAlpha(shapePixel) / 255;

 

in the inner loop. That should give you a bb::cascades::Image with your shape

 

Keep in mind that the function requires a QImage, not a bb::cascades::Image, but returns a bb::cascades::Image

 

If you aren't afraid of a bit of source code, take a look at the Carousel-example, which creates a mirrorred image by merging two images together (one of them flipped)

https://github.com/blackberry/Cascades-Community-Samples/blob/master/stumpers/bbjamcarousel/src/Caro...

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o