200分:将图片保存到xml文件 ( 积分: 200 )

  • 主题发起人 主题发起人 加油+加油
  • 开始时间 开始时间

加油+加油

Unregistered / Unconfirmed
GUEST, unregistred user!
最好说的具体一些,给个实例,不用adodataset控件,用msxml_tlb库
 
最近,不知道年轻人思想咋就这么前卫
 
<Element id=&quot;AG_AG_EXPOWER_ICON&quot;
format=&quot;AG_FORMAT_GRAPHIC&quot;>
<Data id=&quot;0&quot;>
<struct type=&quot;GddiGraphic&quot;>
<GddiUnit value=&quot;5&quot;
/>
<GddiUnit value=&quot;6&quot;
/>
<Int8 value=&quot;129&quot;
/>
<Int8 value=&quot;1&quot;
/>
<array type=&quot;Int8&quot;>
<Int8 value=&quot;48&quot;
/>
<Int8 value=&quot;232&quot;
/>
<Int8 value=&quot;12&quot;
/>
<Int8 value=&quot;232&quot;
/>
<Int8 value=&quot;48&quot;
/>
</array>
</struct>
</Data>
</Element>
<Element id=&quot;AG_DOWN_ARROW&quot;
format=&quot;AG_FORMAT_GRAPHIC&quot;>
<Data id=&quot;0&quot;>
<struct type=&quot;GddiGraphic&quot;>
<GddiUnit value=&quot;6&quot;
/>
<GddiUnit value=&quot;11&quot;
/>
<Int8 value=&quot;129&quot;
/>
<Int8 value=&quot;1&quot;
/>
<array type=&quot;Int8&quot;>
<Int8 value=&quot;255&quot;
/>
<Int8 value=&quot;127&quot;
/>
<Int8 value=&quot;63&quot;
/>
<Int8 value=&quot;31&quot;
/>
<Int8 value=&quot;14&quot;
/>
<Int8 value=&quot;4&quot;
/>
<Int8 value=&quot;224&quot;
/>
<Int8 value=&quot;192&quot;
/>
<Int8 value=&quot;128&quot;
/>
<Int8 value=&quot;0&quot;
/>
<Int8 value=&quot;0&quot;
/>
<Int8 value=&quot;0&quot;
/>
</array>
</struct>
</Data>
</Element>
以上是点阵方式保存的,只能黑白两色
 
icc 的话是什么意思啊?
To:abigfish不行啊,图片不是黑白的
 
那就比较难办了,或是数据量极大,可否弄成连接信息,还是BMP存储
 
mime一下就成text了,放xml/text都可以
 
XML内容
<?xml version=&quot;1.0&quot;
encoding=&quot;UTF-8&quot;?>
<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;
 
谢谢大家的回答,问题还没解决,解决后散分...
谢谢 风铃夜思雨 给出的代码,呵呵!
我只想实现一个简单的图片保存,我现在用的不是这种方法.
 
我问你一个比较实质性的问题
直接使用jpg文件(或其它格式),与转化到xml有何区别?
 
TO:icc 转化到xml是我必须要做的,而不是讨论哪种方法更好
 
问题就是,只要是XML就可以了,并且满足一个条件
能从XML中还原
要是这样,就十分的简单了
 
TO:icc
QQ:285209726 加我聊可以吗?
 
多人接受答案了。
 
后退
顶部