android中bitmap图片与二进制,String间的转化

1,

public Bitmap stringtoBitmap(String string) {                 // 将字符串转换成Bitmap类型                 Bitmap bitmap = null;                 try {                         byte[] bitmapArray;                         bitmapArray = Base64.decode(string, Base64.DEFAULT);                         bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,                                         bitmapArray.length);                 } catch (Exception e) {                         e.printStackTrace();                 }

                return bitmap;         }

        public String bitmaptoString(Bitmap bitmap) {

                // 将Bitmap转换成字符串                 String string = null;                 ByteArrayOutputStream bStream = new ByteArrayOutputStream();                 bitmap.compress(CompressFormat.PNG, 100, bStream);                 byte[] bytes = bStream.toByteArray();                 string = Base64.encodeToString(bytes, Base64.DEFAULT);                 return string;         }

 

2,

Byte()转换成Bitmap

Dim ms As MemoryStream =  New MemoryStream(bytes)

ms.Position = 0

Dim img As Image =  Image.FromStream(ms)

ms.Close()

 Bitmap转化为Byte()

Public Function BitmapToByte() As Byte()

    Dim BitReturn As Bitmap =  New Bitmap()

    Dim bReturn() As Byte =  Nothing

    Dim ms As MemoryStream =  New MemoryStream()

    BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

    bReturn = ms.GetBuffer()

End Function

 

 

Byte()与String的转换代码

Dim converter As System.Text.UnicodeEncoding

converter = New System.Text.UnicodeEncoding()
Dim inputBytes() As Byte = converter.GetBytes(inputString)

Dim inputString As String = converter.GetString(inputBytes)  

Dim inputString As String = System.Convert.ToBase64String(inputBytes)

Dim inputBytes() As Byte =  System.Convert.FromBase64String(inputString)

 Stream和Byte()之间的转换

Public Function StreamToBytes(byval stream as Stream) As Byte

    Dim bytes() As Byte =  New Byte(stream.Length) {}

    stream.Read(bytes, 0, bytes.Length)

    ' 设置当前流的位置为流的开始

    stream.Seek(0, SeekOrigin.Begin)

    Return bytes

End Function

Public Function BytesToStream(ByVal bytes() As Byte) As Stream

    Dim stream As Stream =  New MemoryStream(bytes)

    Return stream

End Function

 Stream 和 文件之间的转换

Public Sub StreamToFile(ByVal stream As Stream, ByVal fileName As String)

    ' 把 Stream 转换成 byte()

    Dim bytes() As Byte =  New Byte(stream.Length) {}

    stream.Read(bytes, 0, bytes.Length)

    ' 设置当前流的位置为流的开始

    stream.Seek(0, SeekOrigin.Begin)

    ' 把 byte()写入文件

    Dim fs As FileStream =  New FileStream(fileName,FileMode.Create)

    Dim bw As BinaryWriter =  New BinaryWriter(fs)

    bw.Write(bytes)

    bw.Close()

    fs.Close()

End Sub

 

从文件读取Stream

Public Function FileToStream(ByVal fileName As String) As Stream           

    '打开文件

    Dim fileStream As FileStream

    fileStream = New FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read)

    '读取文件的 byte()

    Dim bytes() As Byte =  New Byte(fileStream.Length) {}

    fileStream.Read(bytes, 0, bytes.Length)

    fileStream.Close()

    '把 byte()转换成 Stream

    Dim stream As Stream =  New MemoryStream(bytes)

    Return stream

End Function

 

3, bitmap和base64之间的转换

  • /** 
  •  * bitmap转为base64 
  •  * @param bitmap 
  •  * @return 
  •  */  
  • public static String bitmapToBase64(Bitmap bitmap) {  
  •   
  •     String result = null;  
  •     ByteArrayOutputStream baos = null;  
  •     try {  
  •         if (bitmap != null) {  
  •             baos = new ByteArrayOutputStream();  
  •             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  •   
  •             baos.flush();  
  •             baos.close();  
  •   
  •             byte[] bitmapBytes = baos.toByteArray();  
  •             result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
  •         }  
  •     } catch (IOException e) {  
  •         e.printStackTrace();  
  •     } finally {  
  •         try {  
  •             if (baos != null) {  
  •                 baos.flush();  
  •                 baos.close();  
  •             }  
  •         } catch (IOException e) {  
  •             e.printStackTrace();  
  •         }  
  •     }  
  •     return result;  
  • }  
  •   
  • /** 
  •  * base64转为bitmap 
  •  * @param base64Data 
  •  * @return 
  •  */  
  • public static Bitmap base64ToBitmap(String base64Data) {  
  •     byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);  
  •     return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  

转载于:https://www.cnblogs.com/manmanlu/p/4054218.html

你可能感兴趣的:(移动开发)