2013-02-08 27 views
0

我已經將一個圖像轉換爲base64字符串,並且輸出與在線網站相同。爲什麼Base64字符串在C#和Android中不同

但是,當我將它從Android轉換成相同的圖像是不同的。

你能解釋爲什麼C#和Android base64字符串對於同一圖像是不同的。

C#.NET代碼

string cImagePath = @"G:\bg-listing.png"; 
byte[] imagebyte = StreamFile(cImagePath); 
String result = System.Convert.ToBase64String(imagebyte); 
System.IO.StreamWriter outFile; 
try 
{ 
    outFile = new System.IO.StreamWriter(Application.StartupPath + "//image2base641.txt", 
            false, 
            System.Text.Encoding.Default); 
       outFile.Write(result.ToString()); 
       outFile.Close(); 
      } 
      catch (System.Exception exp) 
      { 
       // Error creating stream or writing to it. 
       System.Console.WriteLine("{0}", exp.Message); 
      } 

的Android代碼

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao); 
byte [] ba = bao.toByteArray(); 
String ba1=Base64.encodeToString(ba,Base64.DEFAULT); 

兩個圖像的base64是不同的。

請幫幫我。

回答

3

有很多基地64的變種,涉及行長度,填充,校驗和等。Wikipedia article on Base64有一個很好的變種表。

我的猜測是C#和Android只是使用不同的變體。

編輯根據您的更新後,有一對夫婦的其他可能性:

  • ,Android可以修改.jpg文件時它打包作爲資源(但是,而資源打包在壓縮方面非常積極,這可能不是這種情況);
  • 的Android可被重新編碼不同的圖像比原始(二.jpg文件可以表示相同的像素值和不字節對字節相同的)

一個更好的測試將是跳過(在Android代碼中)從資源轉換爲Bitmap並返回到.jpg編碼。只需以流的形式打開資源,將其直接讀入一個字節數組,並將其編碼爲64位。

相關問題