Skip to main content

Resizing image in Blackberry 10 Cascades

In this post we will learn how to re-size an image stored on file or in memory. Cascades provide Image class to perform loading of images but it do not have any support for manipulating the image especially re-sizing of image. But we have a class QImage from Qt that support re-sizing of images. So first lets see how we can load image through QImage class

Loading image

QImage has many overloaded constructors for loading images from different sources or just create an empty QImage of given height and width. We will cover two constructors here, one for loading image from file and other to load image from image data in memory.

Loading from file
We will use a constructor that takes file name (QString) as a parameter. We can optionally pass format as a char* parameter but its better to skip that so QImage will detect format from the filename
QImage img("asset:///testimage.jpg");

Loading from in memory data
QImage provides another constructor to load image data stored in memory .It takes pointer to data (as uchar*) , width ,height and format as parameter. We will load RGB32 image data
uchar* myImageData=getImageData(&width,&height);
QImage img(myImageData,width,height,QImage::Format_RGB32);

Re-sizing image

Now we have image data loaded in QImage instance. We can use scaled method of QImage to re-size the image.It has two overloads we will use the one that accepts width,height,aspect ratio (optional).
QImage resizedImage=img.scaled(250,250,Qt::KeepAspectRatio);
As we have used Qt::KeepAspectRatio the height is not guaranteed to be 250 (as we have passed) instead it will be calculated by QImage to not mess up the aspect ratio.

After re-sizing you can save the image like this
resizedImage.save("file:///path_to_your_file");

For the complete reference of QImage refer to the following link
http://qt-project.org/doc/qt-4.8/qimage.html

Comments

Popular posts from this blog

Decoding JPEG image file using libavcodec

I got a chance to work on a video encoding application that decodes series of jpeg files and convert them into ogg theora video file. I used the infamous libavcodec library that is used in FFMPEG . I decided to write blog posts explaining how I decode jpeg images and convert them into ogg video file. This is the first part and in this I will explain how to decode jpeg images using libavcodec. To learn how to write decoded images as a ogg video file please read http://random-stuff-mine.blogspot.com/2017/07/encoding-raw-images-to-ogg-theora-video.html Before reading this blog post you must be aware of using and setting up libavcodec. I highly recommend this tutorial to get basics of using libavcodec http://www.ffmpeg.org/doxygen/0.6/api-example_8c-source.html Allocating input format context We will first allocate input format for reading the file. We will use avformat_open_input function that will allocate AVFormatContext structure passed to it , the function detects input typ

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information

I created a new Blazor Server app in Visual Studio 2019 and tried to run it. But I was getting this error CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. I couldn't find any reason or solution to this problem. I tried creating the project multiple times but same error. I created a new .Net Core Web App and added a new razor component and included that component in a razor page (cshtml file) like this @(await Html.RenderComponentAsync<GeofenceWork>(RenderMode.ServerPrerendered)) and <component type="typeof(GeofenceWork)" render-mode="serverprerendered" /> As soon as I navigate to this page that has component added I got the same error: CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. This was very frustrating. After hours of trying and searching I figured out the solution. 

Encoding raw images to Ogg Theora video using libavcodec

In one of the blog posts we learned how to decode jpeg images using libavcodec. This is the second part of that post. In this we will learn how to encode decoded images (raw images) to theora and write them in ogg video file. In the end of the first part we saved our raw image in raw_data variable and its length in raw_data_size variable. Lets assume that we packaged all our decoding code in one function called "decode_jpeg_image" which has following signature int decode_jpeg_image(char *filename,int file_name_size,uint8_t *raw_data,int *raw_data_size) filename = name of jpeg file to decode file_name_size = length of jpeg file's name raw_data = contains decoded raw image on return raw_data_size = contains length of raw_data on return Now let's start working on how to encode this image in raw_data to theora and write that image to ogg video file. Finding Theora encoder We first have to find encoder for THEORA which is represented by AVCodec structure. He