0

I have captured a fingerprint image in imgBuffer.

BYTE *imgBuffer = new BYTE[m_ImageWidth*m_ImageHeight];
myObj->GetImage(imgBuffer);

Convert it into Base64.

int imgBufferSize = sizeof(imgBuffer) - 1;
QByteArray temp = QByteArray(*imgBuffer, imgBufferSize);
QByteArray base64Image = temp.toBase64();

But I am getting something like this:

 BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBw==

I followed this: How to convert an array into QByteArray?

Edit: Actually after implementing Jacob's solution I got another Base64 string but that was not also an image. Though Jacob's solution was correct. The problem was that the image didn't have any meta data. So, I solved it using following procedure:

QImage jpgImage((const unsigned char*)imgBuffer, m_ImageWidth, m_ImageHeight, QImage::Format_Grayscale8);

QByteArray mImage64ByteArray;
QBuffer buffer(&mImage64ByteArray);
buffer.open(QIODevice::WriteOnly);
jpgImage.save(&buffer, "JPG");
mImage64ByteArray = mImage64ByteArray.toBase64();
Community
  • 1
  • 1
Mahmudul Haque
  • 502
  • 2
  • 8
  • 18

2 Answers2

2

@Jacob is correct. Just to show you whats wrong, here is the corrected version:

int imgBufferSize = sizeof(imgBuffer);// - 1 propably wrong
QByteArray temp = QByteArray(imgBuffer, imgBufferSize); //use the pointer!
QByteArray base64Image = temp.toBase64();

As you can see, you need to pass the pointer. Also, Since you are working with a byte array, and not a c string, the -1 is propably wrong! But of course that depends on the GetImage method

Felix
  • 6,885
  • 1
  • 29
  • 54
1

You aren't calling the correct constructor for QByteArray. You probably don't mean to dereference that imgBuffer pointer when you pass it to the QByteArray constructor. What ends up happening in your case is that you end up calling the QByteArray(int size, char ch) constructor instead of the QByteArray(const char* data, int size) constructor.

In addition to this, the imgBufferSize is definitely going to be wrong, because you're using sizeof on a pointer type, which will just return the size of the pointer and not the size of the allocated memory. imgBufferSize should probably be m_ImageWidth*m_ImageHeight. This explains why your base64 encoded string is full of values that work out to 0x07, because that's the size of a pointer on your platform minus 1.

So the correct code would look something like this:

int imgBufferSize = m_ImageWidth*m_ImageHeight;
QByteArray temp = QByteArray(imgBuffer, imgBufferSize);
QByteArray base64Image = temp.toBase64();

In addition to this, you should listen to your compiler, because a reasonable compiler would give you a warning on the narrowing conversion from int to char. Likewise some of these problems should be fairly obvious if you stepped through the code with a debugger. It would also be worth considering getting rid of the manual allocation of the byte array, and just use the QByteArray all the way through. This would eliminate some copying, keep you from having to manually manage the memory, and simplify your program.

Jacob
  • 3,626
  • 2
  • 20
  • 26
  • Error: no instance of constructor "QByteArray::QByteArray" matches the argument list. argument types are: (BYTE *, int) – Mahmudul Haque Apr 09 '17 at 19:57
  • Have a look at what the type of BYTE is. I'm going to guess that BYTE is a typedef of an unsigned char, while QByteArray expects a signed char. – Jacob Apr 09 '17 at 20:04
  • Yes **typedef unsigned char BYTE;** – Mahmudul Haque Apr 09 '17 at 21:18
  • Alright, then you can look at this answer to figure out how to safely convert from between the two pointer types: http://stackoverflow.com/a/16261758/327071 – Jacob Apr 09 '17 at 21:29