XML内容
<?xml version="1.0"
encoding="UTF-8"?>
<OCR>
<item>
<DemoBmp>eNpz8r3HAAF2QKwBxDZALALEjEAIAgsYEIAJSv///x9Gg8CHBoiwADLN2ICgZyfwM8g22AFpyQbjhmQgbXAgseEAkGYA0v+h9GEgbQmUTxOYdsAcqN4GqN8Aah6Mtm9AtwcbDXMXADf7MqA=</DemoBmp>
</Item>
</OCR>
uses zlib;{Zlib我用的是新版本的,如果你用DELPHI自带的,用到的函数可能要改下}
Const
En64Table: Array[0..63] Of Char = (#65, #66, #67, #68, #69,
#70, #71, #72, #73, #74, #75, #76, #77, #78, #79,
#80, #81, #82, #83, #84, #85, #86, #87, #88, #89,
#90, #97, #98, #99, #100, #101, #102, #103, #104, #105,
#106, #107, #108, #109, #110, #111, #112, #113, #114, #115,
#116, #117, #118, #119, #120, #121, #122, #48, #49, #50,
#51, #52, #53, #54, #55, #56, #57, #43, #47);
De64Table: Array[43..122] Of Byte = ($3E, $7F, $7F, $7F, $3F, $34,
$35, $36, $37, $38, $39, $3A, $3B, $3C, $3D, $7F, $7F, $7F, $7F,
$7F, $7F, $7F, $00, $01, $02, $03, $04, $05, $06, $07, $08, $09,
$0A, $0B, $0C, $0D, $0E, $0F, $10, $11, $12, $13, $14, $15, $16,
$17, $18, $19, $7F, $7F, $7F, $7F, $7F, $7F, $1A, $1B, $1C, $1D,
$1E, $1F, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $2A,
$2B, $2C, $2D, $2E, $2F, $30, $31, $32, $33);
Function Base64EnCodeStream(InStream, OutStream: TStream): boolean;
Stdcall;
Var
i, O, Count: Integer;
InBuf: Array[1..45] Of Byte;
OutBuf: Array[0..62] Of Char;
Temp: Byte;
begin
Result := false;
If InStream.Size = 0 then
Exit;
FillChar(OutBuf, SizeOf(OutBuf), #0);
Repeat
i := 1;
O := 0;
Result := false;
Count := InStream.Read(InBuf, SizeOf(InBuf));
If Count = 0 then
Break;
While i <= (Count - 2)do
begin
{ Encode 1st byte }
Temp := (InBuf Shr 2);
OutBuf[O] := Char(En64Table[Temp And $3F]);
{ Encode 1st/2nd byte }
Temp := (InBuf Shl 4) Or (InBuf[i + 1] Shr 4);
OutBuf[O + 1] := Char(En64Table[Temp And $3F]);
{ Encode 2nd/3rd byte }
Temp := (InBuf[i + 1] Shl 2) Or (InBuf[i + 2] Shr 6);
OutBuf[O + 2] := Char(En64Table[Temp And $3F]);
{ Encode 3rd byte }
Temp := (InBuf[i + 2] And $3F);
OutBuf[O + 3] := Char(En64Table[Temp]);
Inc(i, 3);
Inc(O, 4);
end;
If (i <= Count) then
begin
Temp := (InBuf Shr 2);
OutBuf[O] := Char(En64Table[Temp And $3F]);
If i = Count then
begin
Temp := (InBuf Shl 4) And $30;
OutBuf[O + 1] := Char(En64Table[Temp And $3F]);
OutBuf[O + 2] := '=';
End else
begin
Temp := ((InBuf Shl 4) And $30) Or ((InBuf[i + 1] Shr 4) And $0F);
OutBuf[O + 1] := Char(En64Table[Temp And $3F]);
Temp := (InBuf[i + 1] Shl 2) And $3C;
OutBuf[O + 2] := Char(En64Table[Temp And $3F]);
end;
OutBuf[O + 3] := '=';
Inc(O, 4);
end;
OutStream.Write(OutBuf, O);
Result := true;
Until Count < SizeOf(InBuf);
OutStream.Position := 0;
end;
{-------------------------------------------------------------------}
Function Base64DeCodeStream(InStream, OutStream: TStream): boolean;
Stdcall;
Var
i, O, Count, c1, c2, c3: Byte;
InBuf: Array[0..87] Of Byte;
OutBuf: Array[0..65] Of Byte;
begin
Result := false;
If InStream.Size = 0 then
Exit;
Repeat
O := 0;
i := 0;
Result := false;
Count := InStream.Read(InBuf, SizeOf(InBuf));
If (Count = 0) then
Break;
While i < Countdo
begin
If (InBuf < 43) Or (InBuf > 122) Or
(InBuf[i + 1] < 43) Or (InBuf[i + 1] > 122) Or
(InBuf[i + 2] < 43) Or (InBuf[i + 2] > 122) Or
(InBuf[i + 3] < 43) Or (InBuf[i + 3] > 122) then
Break;
c1 := De64Table[InBuf];
c2 := De64Table[InBuf[i + 1]];
c3 := De64Table[InBuf[i + 2]];
OutBuf[O] := ((c1 Shl 2) Or (c2 Shr 4));
Inc(O);
If Char(InBuf[i + 2]) <> '=' then
begin
OutBuf[O] := ((c2 Shl 4) Or (c3 Shr 2));
Inc(O);
If Char(InBuf[i + 3]) <> '=' then
begin
OutBuf[O] := ((c3 Shl 6) Or De64Table[InBuf[i + 3]]);
Inc(O);
end;
end;
Inc(i, 4);
end;
OutStream.Write(OutBuf, O);
Result := true;
Until Count < SizeOf(InBuf);
OutStream.Position := 0;
end;
Procedure StrToBmpStream(v: OleVariant;
Var Stream: TMemoryStream);
Var
Str: String;
MS1: TStringStream;
MS2: TMemoryStream;
begin
If VarIsNull(v) then
Exit;
Str := VarToStr(v);
If Str = '' then
Exit;
MS1 := TStringStream.Create(Str);
MS2 := TMemoryStream.Create;
Base64DeCodeStream(MS1, MS2);
//BASE64解密流
MS2.Position := 0;
ZDecompressStream(MS2, Stream);//Zlib解压缩流
Stream.Position := 0;
FreeAndNil(MS2);
FreeAndNil(MS1);
end;
Function BmpStreamToStr(Stream: TMemoryStream): String;
Var
MS1: TMemoryStream;
MS2: TStringStream;
begin
MS1 := TMemoryStream.Create;
MS2 := TStringStream.Create('');
Stream.Position := 0;
ZCompressStream(Stream, MS1, zcMax);//ZLIB加密流
MS1.Position := 0;
Base64EnCodeStream(MS1, MS2);//Base64加密流
Result := MS2.DataString;
FreeAndNil(MS2);
FreeAndNil(MS1);
end;
var MS: TMemoryStream;
begin
取XML的内容我就不贴上来了
OCR_Item.DemoBmp等于从XML中读取DemoBmp段的文本内容
读取
StrToBmpStream(OCR_Item.DemoBmp, MS);
image1.Picture.Bitmap.LoadFromStream(MS);
保存
image1.Picture.Bitmap.SaveToStream(MS);
OCR_Item.DemoBmp := BmpStreamToStr(MS);
end;