Delphi Database Programming Tutorial (6)

zhaozj2021-02-16  51

Section IV Looking for the beginning of JPEG in BLOB

OLE Object Type Format - ILE Object Type Format - Take Three! Now all we need to do what stores pictures to disk (saving ordinary binary) and understands what it is in the door.

All image files (formats) have file headers that are uniquely identified images. The JPG image file starts with a so-called SOI tag, and the sixteen-entered value of the tag is $ FFD8.

The following line code stores the image field value to the relevant file of the working directory (BlobImage.dat). Place this code in the oncreate event of the form, and then remove the code after starting the project.

Adotable1Picture.Savetofile ('blobimage.dat');

Once we have this file. We can use HEX Editor to see its content.

Do you believe? MS Access stores the path of the connected OLE object as part of the object definition in the OLE object field. Because the storage definition of the OLE object is not documentized (! This is directly from MS), so there is no way to know what the real image data is written before.

Take this problem in two parts. First: We need to find 'ffd8' and start reading images from there. Second: 'FFD8' is impossible to always in the same location of the file. Conclusion: We need a function that returns the location of the SOI tag of the JPG file stored as an OLE object in the Access database.

Correct method - Idea 4 (The Correct Way - Take Four!)

After providing the BLOB type field, our function should return the location of the 'FFD8' string in AdoblobStream. Readbuffer reads data from a byte by one byte in the stream. Each call to Readput is a bit of one byte of the mobile stream. When the two bytes lead to the SOI tag together, the function returns to the position of the stream. This is this function:

Function JPEGSTARTSINBLOB (Picfield: TBLOBFIELD): Integer;

VAR

BS: tadoblobstream;

Buffer: Word;

HX: String;

Begin

RESULT: = -1;

BS: = Tadoblobstream.create (picfield, bmread);

Try

While (result = -1) and (bs.position 1

Begin

BS.Readbuffer (buffer, 1);

HX: = INTTOHEX (Buffer, 2);

IF HX = 'FF' THEN BEGIN

BS.Readbuffer (buffer, 1);

HX: = INTTOHEX (Buffer, 2);

IF hx = 'd8' Then Result: = bs.position - 2

Else if hx = 'ff' Then

BS.Position: = bs.position-1;

End; // IF

End; // while

Finally

BS.Free

End; // Try

END;

Once we have the location of the SOI tag, we can use it to find the location of the image in the AdoBlob stream.

Uses JPEG;

...

Procedure TFORM1.BTNSHOWIMAGECLICK (Sender: TOBJECT);

VAR

BS: tadoblobstream;

Pic: tjpegimage;

Begin

BS: = TadoblobStream.create (adotable1Picture, Bmread);

Try

BS.Seek (JPEGSTARTSINBLOB (ADOTABLE1PICTURE), SOFROMBEGINNING

Pic: = tjpegimage.create;

Try

Pic.LoadFromstream (BS);

ADOIMAGE.PICTURE.GRAPHIC: = PIC;

Finally

Pic.free;

END;

Finally

BS.Free

END;

END;

Running engineering, OK!

Who will say that the programming doesn't have fun?

Note: In the real code program, we will add code in the TDataSet's Afterscroll event to read and display images from the current row (it is in the ADOTABLE1AFTERSCROLL event). When the application rolls from one record to another, the AfTerscroll event occurs.

Thoughts five!

This is the main content of this chapter. Now you can store and display all JPG images you are interested in. At the last page of this article, I will provide a complete code (Form1 unit); all data arrangements are placed in the oncreate event of the form. This ensures that all three components are properly connected - you don't need to use the Object Inspector when design.

I admit that this chapter is not suitable for beginners, but the world is cruel! Another thing: You noticed that you don't know how to change (or add some new) tables! Yes, that is another complete story!

转载请注明原文地址:https://www.9cbs.com/read-24478.html

New Post(0)