How to decode a Jpeg image from a web POST

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
LVL 2
Roger AlcindorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger AlcindorAuthor Commented:
The size of the raw data as given by  Request->RawContent.Length is  1579 kb.
0
Roger AlcindorAuthor Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roger AlcindorAuthor Commented:
Any further comments welcome in any case.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.