bmp 图片如何保存成Base64位编码,就是保持成文本形式,有人会吗? 不至于吧。 (200分)

  • 主题发起人 主题发起人 Puma Wang
  • 开始时间 开始时间
P

Puma Wang

Unregistered / Unconfirmed
GUEST, unregistred user!
也知道Base64 的大概原理,就是不知道如何写 读bmp 然后传的 程序。
请哪位知道的大虾,给段程序让我参考参考,直接写出来更好。
不要技术保守了,同志们,我搞定了会感谢大家的。
 
bmp先savetostream,然后encode

你搜索一下base64,应该有好多这种东西的编码和解码的资料的
 
请先参考:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=525820
 
来如风, 这个我还不太会,有空我去试试。 或者你有代码让我参考参考吗? 拜托了。
 
左轻侯的网站上有源码下
 
http://www.wushuang.net/

哦,记错了没有源码
但是左大虾是很慷慨的
你去封信他不会不给你的吧:)
 
base64编码 delphi自己就带有. 在用delphi做web方面的程序的时候必不可少。
首先在uses 加上 encddecd ;
在encddecd里面有四个 开放函数。
procedure EncodeStream(Input, Output: TStream); //base64 对stream编码
procedure DecodeStream(Input, Output: TStream); //base64 对stream解码
function EncodeString(const Input: string): string; //base64 对字符串编码
function DecodeString(const Input: string): string; //base64 对字符串解码

















下面就是 encddecd.pas (delphi6就只带有encddecd.pas文件 ,如果你delphi5 那么就把下面的拷贝下去)

{********************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 2000, 2001 Borland Software Corporation }
{ }
{********************************************************}
unit EncdDecd;

{ Stream encoding properly wraps }

//

interface

uses Classes;

procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);
function EncodeString(const Input: string): string;
function DecodeString(const Input: string): string;

implementation

const
EncodeTable: array[0..63] of Char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/';

DecodeTable: array[#0..#127] of Integer = (
Byte('='), 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64);

type
PPacket = ^TPacket;
TPacket = packed record
case Integer of
0: (b0, b1, b2, b3: Byte);
1: (i: Integer);
2: (a: array[0..3] of Byte);
3: (c: array[0..3] of Char);
end;

procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar);
begin
OutBuf[0] := EnCodeTable[Packet.a[0] shr 2];
OutBuf[1] := EnCodeTable[((Packet.a[0] shl 4) or (Packet.a[1] shr 4)) and $0000003f];
if NumChars < 2 then
OutBuf[2] := '='
else OutBuf[2] := EnCodeTable[((Packet.a[1] shl 2) or (Packet.a[2] shr 6)) and $0000003f];
if NumChars < 3 then
OutBuf[3] := '='
else OutBuf[3] := EnCodeTable[Packet.a[2] and $0000003f];
end;

function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket;
begin
Result.a[0] := (DecodeTable[InBuf[0]] shl 2) or
(DecodeTable[InBuf[1]] shr 4);
NChars := 1;
if InBuf[2] <> '=' then
begin
Inc(NChars);
Result.a[1] := Byte((DecodeTable[InBuf[1]] shl 4) or (DecodeTable[InBuf[2]] shr 2));
end;
if InBuf[3] <> '=' then
begin
Inc(NChars);
Result.a[2] := Byte((DecodeTable[InBuf[2]] shl 6) or DecodeTable[InBuf[3]]);
end;
end;

procedure EncodeStream(Input, Output: TStream);
type
PInteger = ^Integer;
var
InBuf: array[0..509] of Byte;
OutBuf: array[0..1023] of Char;
BufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket;
begin
K := 0;
repeat
BytesRead := Input.Read(InBuf, SizeOf(InBuf));
I := 0;
BufPtr := OutBuf;
while I < BytesRead do
begin
if BytesRead - I < 3 then
J := BytesRead - I
else J := 3;
Packet.i := 0;
Packet.b0 := InBuf;
if J > 1 then
Packet.b1 := InBuf[I + 1];
if J > 2 then
Packet.b2 := InBuf[I + 2];
EncodePacket(Packet, J, BufPtr);
Inc(I, 3);
Inc(BufPtr, 4);
Inc(K, 4);
if K > 75 then
begin
BufPtr[0] := #$0D;
BufPtr[1] := #$0A;
Inc(BufPtr, 2);
K := 0;
end;
end;
Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
until BytesRead = 0;
end;

procedure DecodeStream(Input, Output: TStream);
var
InBuf: array[0..75] of Char;
OutBuf: array[0..60] of Byte;
InBufPtr, OutBufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket;

procedure SkipWhite;
var
C: Char;
NumRead: Integer;
begin
while True do
begin
NumRead := Input.Read(C, 1);
if NumRead = 1 then
begin
if C in ['0'..'9','A'..'Z','a'..'z','+','/','='] then
begin
Input.Position := Input.Position - 1;
Break;
end;
end else Break;
end;
end;

function ReadInput: Integer;
var
WhiteFound, EndReached : Boolean;
CntRead, Idx, IdxEnd: Integer;
begin
IdxEnd:= 0;
repeat
WhiteFound := False;
CntRead := Input.Read(InBuf[IdxEnd], (SizeOf(InBuf)-IdxEnd));
EndReached := CntRead < (SizeOf(InBuf)-IdxEnd);
Idx := IdxEnd;
IdxEnd := CntRead + IdxEnd;
while (Idx < IdxEnd) do
begin
if not (InBuf[Idx] in ['0'..'9','A'..'Z','a'..'z','+','/','=']) then
begin
Dec(IdxEnd);
if Idx < IdxEnd then
Move(InBuf[Idx+1], InBuf[Idx], IdxEnd-Idx);
WhiteFound := True;
end
else
Inc(Idx);
end;
until (not WhiteFound) or (EndReached);
Result := IdxEnd;
end;

begin
repeat
SkipWhite;
{
BytesRead := Input.Read(InBuf, SizeOf(InBuf));
}
BytesRead := ReadInput;
InBufPtr := InBuf;
OutBufPtr := @OutBuf;
I := 0;
while I < BytesRead do
begin
Packet := DecodePacket(InBufPtr, J);
K := 0;
while J > 0 do
begin
OutBufPtr^ := Char(Packet.a[K]);
Inc(OutBufPtr);
Dec(J);
Inc(K);
end;
Inc(InBufPtr, 4);
Inc(I, 4);
end;
Output.Write(OutBuf, OutBufPtr - PChar(@OutBuf));
until BytesRead = 0;
end;

function EncodeString(const Input: string): string;

var
InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
EncodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;































function DecodeString(const Input: string): string;

var
InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
DecodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;




















end.
 
贴出来吧,自己以前写的。不太好,不过可以用。要给分哦。
unit Pub;

interface

uses
Math,SysUtils;

function Base64CodeTab(AIndex: Byte): string;
function Base64Decode(AInputStr: string): string;
function Base64DecodeTab(AIndex: Char): Byte;
function Base64Encode(AInputStr: string): string;
function BinToInt(ABinStr: string): Integer;
function HexToBinary(AHexStr: string): string;

implementation

function Base64CodeTab(AIndex: Byte): string;
begin
Result := '';
case AIndex of
0: Result := 'A';
1: Result := 'B';
2: Result := 'C';
3: Result := 'D';
4: Result := 'E';
5: Result := 'F';
6: Result := 'G';
7: Result := 'H';
8: Result := 'I';
9: Result := 'J';
10: Result := 'K';
11: Result := 'L';
12: Result := 'M';
13: Result := 'N';
14: Result := 'O';
15: Result := 'P';
16: Result := 'Q';
17: Result := 'R';
18: Result := 'S';
19: Result := 'T';
20: Result := 'U';
21: Result := 'V';
22: Result := 'W';
23: Result := 'X';
24: Result := 'Y';
25: Result := 'Z';
26: Result := 'a';
27: Result := 'b';
28: Result := 'c';
29: Result := 'd';
30: Result := 'e';
31: Result := 'f';
32: Result := 'g';
33: Result := 'h';
34: Result := 'i';
35: Result := 'j';
36: Result := 'k';
37: Result := 'l';
38: Result := 'm';
39: Result := 'n';
40: Result := 'o';
41: Result := 'p';
42: Result := 'q';
43: Result := 'r';
44: Result := 's';
45: Result := 't';
46: Result := 'u';
47: Result := 'v';
48: Result := 'w';
49: Result := 'x';
50: Result := 'y';
51: Result := 'z';
52: Result := '0';
53: Result := '1';
54: Result := '2';
55: Result := '3';
56: Result := '4';
57: Result := '5';
58: Result := '6';
59: Result := '7';
60: Result := '8';
61: Result := '9';
62: Result := '+';
63: Result := '/';
end;
end;

function Base64Decode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 4 do
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
AInputStr := Copy(AInputStr,5,Length(AInputStr));
end;
case Pos('=',AInputStr) of
3:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6);
Result := Result + Chr(BinToInt(Copy(ABuffer,1,8)));
end;
4:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6);
for i := 0 to 1 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
0:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
end;
end;

function Base64DecodeTab(AIndex: Char): Byte;
begin
Result := 0;
case AIndex of
'A': Result := 0;
'B': Result := 1;
'C': Result := 2;
'D': Result := 3;
'E': Result := 4;
'F': Result := 5;
'G': Result := 6;
'H': Result := 7;
'I': Result := 8;
'J': Result := 9;
'K': Result := 10;
'L': Result := 11;
'M': Result := 12;
'N': Result := 13;
'O': Result := 14;
'P': Result := 15;
'Q': Result := 16;
'R': Result := 17;
'S': Result := 18;
'T': Result := 19;
'U': Result := 20;
'V': Result := 21;
'W': Result := 22;
'X': Result := 23;
'Y': Result := 24;
'Z': Result := 25;
'a': Result := 26;
'b': Result := 27;
'c': Result := 28;
'd': Result := 29;
'e': Result := 30;
'f': Result := 31;
'g': Result := 32;
'h': Result := 33;
'i': Result := 34;
'j': Result := 35;
'k': Result := 36;
'l': Result := 37;
'm': Result := 38;
'n': Result := 39;
'o': Result := 40;
'p': Result := 41;
'q': Result := 42;
'r': Result := 43;
's': Result := 44;
't': Result := 45;
'u': Result := 46;
'v': Result := 47;
'w': Result := 48;
'x': Result := 49;
'y': Result := 50;
'z': Result := 51;
'0': Result := 52;
'1': Result := 53;
'2': Result := 54;
'3': Result := 55;
'4': Result := 56;
'5': Result := 57;
'6': Result := 58;
'7': Result := 59;
'8': Result := 60;
'9': Result := 61;
'+': Result := 62;
'/': Result := 63;
end;
end;

function Base64Encode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 3 do
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
begin
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
AInputStr := Copy(AInputStr,4,Length(AInputStr));
end;
case Length(AInputStr) of
1:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2)) + '0000';
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,1,6))) +
Base64CodeTab(BinToInt(Copy(ABuffer,7,6))) + '==';
end;
2:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2)) + '00';
for i := 0 to 2 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)));
Result := Result + '=';
end;
3:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
end;
end;

function BinToInt(ABinStr: string): Integer;
var
i: Integer;
begin
Result := 0;
for i:=0 to Length(ABinStr)-1 do
begin
Result := Result + StrToInt(ABinStr[i+1]) * Round(IntPower(2,Length(ABinStr)-i-1));
end;
end;

function HexToBinary(AHexStr: string): string;
var
i: Integer;
begin
Result := '';
for i:=1 to Length(AHexStr) do
begin
case AHexStr of
'0':
begin
Result := Result + '0000';
end;
'1':
begin
Result := Result + '0001';
end;
'2':
begin
Result := Result + '0010';
end;
'3':
begin
Result := Result + '0011';
end;
'4':
begin
Result := Result + '0100';
end;
'5':
begin
Result := Result + '0101';
end;
'6':
begin
Result := Result + '0110';
end;
'7':
begin
Result := Result + '0111';
end;
'8':
begin
Result := Result + '1000';
end;
'9':
begin
Result := Result + '1001';
end;
'A':
begin
Result := Result + '1010';
end;
'B':
begin
Result := Result + '1011';
end;
'C':
begin
Result := Result + '1100';
end;
'D':
begin
Result := Result + '1101';
end;
'E':
begin
Result := Result + '1110';
end;
'F':
begin
Result := Result + '1111';
end;
end;
end;
end;

end.
 
为什么不用Delphi自带的?
Delphi6的Indy面板上有两个Indy的控件,好像叫Base64Encode和Base64Decode吧?(具体你找找)直接用它就可以加密和解密Base64啦!
 
多人接受答案了。
 
后退
顶部