How to decode a Jpeg image from a web POST

Roger Alcindor
Roger Alcindor used Ask the Experts™
on
I am writing a windows Web application using embarcadero XE10.2 Berlin C++ builder;
The application uses a TWebModule and receives a POST web action from a web page being displayed on a remote mobile device.
the mobile device posts a jpeg image which Is received in the following function, st2 is a TMemoryStream.
I am copying the raw content into a TByteDyneArray b so that I can easily inspect it. I load the TMemoryStream with the raw content and then save the memory stream to a file with the extension .jpg.
This file is reported as corrupt or invalid or too long when I try to open it with windows Photo viewer or the like.
how do I extract the image data ?
The size of the raw data as given by  Request->RawContent.Length is
Inspection of the beginning of the raw content  up to encountering a NULL is shown in quotes below.
"------WebKitFormBoundary9lWvgPDQcTZoDcRf\r\nContent-Disposition: form-data; name=\"picture\"; filename=\"15123250544981385201258.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n�￘�£\x02tExif"

void TWebModule1::Action3Post(TWebRequest *Request,TWebResponse *Response)
{
	UnicodeString u,v,uu="";
	TByteDynArray b;
	char buff='a';

	int n,m,w;
	u = Request->ContentType;
	n = Request->RawContent.Length;
	m = Request->Files->Count;
	b = Request->RawContent;
	st2->Clear();
	b.set_length(n);
	st2->WriteData(Request->RawContent,Request->RawContent.Length);
    st2->Position = soBeginning;

//	Form1->Memo1->Lines =  Request->ContentFields;
	for(;;)
	{
		st2->Read(&buff,1);
		uu +=buff;
		if(buff==2)
		{
			for(;;)
			{
				st2->Read(&buff,1);
				uu +=buff;
				if(buff==0)
					break;
			}
			break;
		}
	}
	st2->SaveToFile(u"testpicture.jpg");

Open in new window


The html5 code in the web application that leads to the "post" action is shown below :
<div id="div1">
	<form name="scanner" id="scannerForm" action="" method="post" enctype="multipart/form-data"> 
	<label style ="margin-top:0px; font-size:50px;"for="barcodescan">Scan Barcode</label>
		<input name="picture" type="file" id="barcodescan" accept="image/*;capture=camera">
	</form>
</div>
<script>
    document.getElementById("barcodescan").onchange = function() {
          if( this.files.length != 0 ) {
                 document.getElementById("scannerForm").submit();
          }
          else {
                alert("no barcode picked, please try again!");
          }
    }
</script>

Open in new window

Thanks roger
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
The size of the raw data as given by  Request->RawContent.Length is  1579 kb.
I have managed to work it out.

I discovered that all jpeg files in my photos have the first two characters 0xFFD8
so searching for this sequence in the raw data gives me the start of the jpeg image data.
I then copy this data and all the data following, to a new memory stream. Then I load a TJPEGImage with
data from the new stream and from there on I can view or otherwise use the data for
an optical barcode recognition.

A relevant fragment of the code is shown below:
	st2->Clear();
	b.set_length(n);
	st2->WriteData(Request->RawContent,Request->RawContent.Length);
	st2->Position = soBeginning;

	for(i=0;i<1000;i++)     // limit the search to the 100 characters
	{
		st2->Read(buff+i,1);
		//uu +=buff;
		if(buff[i]==255)  // 0xFF
		{
			i++;
			for(;i<1000;i++)
			{
				st2->Read(buff+i,1);
				if(buff[i]==216)  // 0xD8
				{
					jpegfound = true;
					break;
				}
			}
			if(!jpegfound)
				continue;
			else
                break;
		}
	}
	if(jpegfound)
	{
		st2->Seek(jpegoffset,soCurrent); // back-up to start of jpeg image data
		mm->CopyFrom(st2,st2->Size-st2->Position);
		mm->SaveToFile(u"testpicture.jpg");   // this was for debugging
		mm->Position = soBeginning;
	jp->LoadFromStream(mm);
//	bm->Assign(jp);

	   Form1->Image1->Picture->Bitmap->Assign(jp);
	   //	Form1->Image1->Picture->LoadFromFile(u"testpicture.jpg");
		Form1->Image1->Stretch = true;
		Form1->Image1->Visible = true;

Open in new window

Author

Commented:
Any further comments welcome in any case.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial