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
PJDavis1970
Posts: 44
Registered: ‎01-22-2013
My Device: Z10, Q10, Z10 Alpha
Accepted Solution

Photo orientation

Hi,

 

I am using FilePicker in qml code to select an image and then loading that image in c++ and converting to base64Encoded to send to my javascript running in a WebView.  My question is how do I get the orientation of the image to rotate that image correctly before base64Encoding it.

 

This is how I load the image.  name is returned from the qml FilePicker.

 

QFile file(name);
file.open(QIODevice::ReadOnly);
QByteArray loadedArray(file.readAll());
QByteArray encodedFile(loadedArray.toBase64());
QString newString = QString(encodedFile);

 

Basically I need the image rotated to the correct orientation before encoding and sending.  Any ideas how I get the orientation of teh image.

 

Thanks

Developer
jalue
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10

Re: Photo orientation

You can check this thread concerning the EXIF information for an image/photo...

 

http://supportforums.blackberry.com/t5/Cascades-Development/Read-image-EXIF-info/td-p/2044495

Contributor
PJDavis1970
Posts: 44
Registered: ‎01-22-2013
My Device: Z10, Q10, Z10 Alpha

Re: Photo orientation

Thanks for the response.  This lead me to finding out how to obtain the EXIF data for the images.  For anyone else looking into this then this is how I obtained the orientation value.  

 

QString BBOmnisWrapper::EncodeImage( QString name )
{
   ExifData *data;
   ExifEntry *entry;
   data = exif_data_new_from_file( name.toLatin1().data() );

   for (int i=0; i<EXIF_IFD_COUNT; i++)
   {
      if ((entry=exif_content_get_entry(data->ifd[i], EXIF_TAG_ORIENTATION)))
         printf("EXIF_TAG_ORIENTATION found L %i\n", *entry->data);
   }

   QFile file(name);
   file.open(QIODevice::ReadOnly);
   QByteArray loadedArray(file.readAll());
   

// rotate image here

 

   QByteArray encodedFile(loadedArray.toBase64());

 

It comes from entry->data and these are the different image orientations.

 

0x0112Orientationint16uIFD0
1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW
Developer
tommy20
Posts: 262
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Photo orientation

Hi PJDavis1970,

Could you please share here how can you rotate an image in your case above?

thanks,
--tom
Contributor
PJDavis1970
Posts: 44
Registered: ‎01-22-2013
My Device: Z10, Q10, Z10 Alpha

Re: Photo orientation

[ Edited ]

Hi Tommy,

 

Sorry its taken me a while to get back I have been so bogged down with deadlines I have not had time until now.

 

OK basically this is the code I use to rotate an image.  the only param you will need in the function params is the name.  The other 2 params ( command and retID ) are just params used by our system for other stuff.  I will try document the code to make it clear whats going on,

 

You will notice I have not handled all orientations as these were not needed for our system but if you look at how I handled the rotations I needed you should easily be able to add the code for other orientations.

 

Hope this helps

 

Paul

 

// QString name is the file name on the BB of the image

void BBOmnisWrapper::EncodeImage( QString command, QString name, QString retID  )
{
	ExifData *data;
	ExifEntry *entry;
	int		orien = 1;

// get the EXIF data for the image.  This contains image orientation etc.

	data = exif_data_new_from_file( name.toLatin1().data() );

// find the orientation entry in the exif data block

	if( data != NULL )
		for (int i=0; i<EXIF_IFD_COUNT; i++)
			if ((entry=exif_content_get_entry(data->ifd[i], EXIF_TAG_ORIENTATION)))
				orien = *entry->data;

// load image and and perform a rotation on it depending what exif returned.

	QImage image( name );
	QTransform transform;
	switch( orien )
	{
		case	1:	// Horizontal normal
				break;
		case	2:	// Mirror horizontal
				break;
		case	3:	// Rotate 180
				transform.rotate(180);
				image = image.transformed(transform);
				break;
		case	4:	// Mirror vertical
				break;
		case	5:	// Mirror horizontal and rotate 270 CW
				break;
		case	6:	// Rotate 90 CW
				transform.rotate(90);
				image = image.transformed(transform);
				break;
		case	7:	// Mirror horizontal and rotate 90 CW
				break;
		case	8:	// Rotate 270 CW
				transform.rotate(270);
				image = image.transformed(transform);
				break;
	}

// save rotated image and base64 encode our newely transformed image to be sent to our server.

	QByteArray byteArray;
	QBuffer buffer(&byteArray);
	image.save(&buffer, "JPG");
	QString encodedString = QString::fromLatin1(byteArray.toBase64().data());

 

Developer
tommy20
Posts: 262
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Photo orientation

It's perfect, Paul. Thanks so much for sharing this.
--tom
Contributor
PJDavis1970
Posts: 44
Registered: ‎01-22-2013
My Device: Z10, Q10, Z10 Alpha

Re: Photo orientation

Glad i could help

Developer
dbigham
Posts: 552
Registered: ‎04-01-2009
My Device: Z10, PlayBook

Re: Photo orientation

Hey folks, thanks so much for the help here. Do you know of a way to take the QImage once it's rotated and put it into an ImageView without having to write it out to the sdcard first?

 

Thanks,
Daniel

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: Photo orientation

dbigham, the solution to that is QPainter

Developer
ETitus
Posts: 54
Registered: ‎05-14-2013
My Device: LE Z10

Re: Photo orientation

Hi all,

 

I've been having issues the last coupe of days with images that are not orientated propperly. Though the native picture viewer and the file manager seem to rotate the images to their proper orientation, the imageview in my app doesn't seem to know how.

 

I've used the photobomber app as an example, similar to your code:

 

ExifData *exifData = 0;
	ExifEntry *exifEntry = 0;
	int exifOrientation = 1;

	exifData = exif_data_new_from_file(imageFilePath.toLatin1().data());
	qDebug() << exifData;
	// Locate the orientation exif information.
	if (exifData != NULL) {
		qDebug() << "Exif data bestaat";
		for (int i = 0; i < EXIF_IFD_COUNT; i++) {
			qDebug() << "Exif int" << i;
			exifEntry = exif_content_get_entry(exifData->ifd[i], EXIF_TAG_ORIENTATION);

			// If the entry corresponds to the orientation it will be a non zero pointer.
			if (exifEntry) {
				qDebug() << "Exif updaten";
				exifOrientation = *exifEntry->data;
				break;
			}
		}
	}
	qDebug() << "Exif data" << exifOrientation;

 The exifData is NULL however, thus the exif_data_new_from_file function returns an error: "\return allocated #ExifData, or NULL on error".


Anyone see something wrong or knows a better way to do this?

Thanks,


Titus

Don't forget to mark as solved and like my post if it helped out!