有多少的方式来位图转换为字符串,反之亦然?反之亦然、位图、有多少、转换为

由网友(Sadness″  逝言)分享简介:在我的应用程序,我想发送的位图图像到服务器字符串的形式,我想知道有多少种方法可用于一个位图转换为字符串。现在我使用Base64格式进行编码和解码,它需要多一点的内存。是否有任何其他的可能性,做同样的事情用不同的方式这需要较少的内存cosumptions。现在,我用这code。资源R = ShowFullImage.t...

在我的应用程序,我想发送的位图图像到服务器字符串的形式,我想知道有多少种方法可用于一个位图转换为字符串。现在我使用Base64格式进行编码和解码,它需要多一点的内存。是否有任何其他的可能性,做同样的事情用不同的方式这需要较少的内存cosumptions。 现在,我用这code。

 资源R = ShowFullImage.this.getResources();
位图BM = BitmapFactory.de codeResource(R,R.drawable.col);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
bm.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS); // BM是位图对象
byte []的B = baos.toByteArray();

字符串连接codeDIMAGE = Base64.en codeToString(B,Base64.DEFAULT);
 

解决方案

 公共字符串BitMapToString(位图位图){
     ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
     bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
     byte []的B = baos.toByteArray();
     字符串临时= Base64.en codeToString(B,Base64.DEFAULT);
     返回温度;
}
 
怎么把pdf转换成word,文件格式的快速转换方法

下面是相反的过程将字符串转换为位图,但字符串应Base64编码

  / **
 * @参数EN codedString
 * @返回位图(从给定的字符串)
 * /
公共位图StringToBitMap(字符串连接codedString){
   尝试 {
      byte []的EN codeByte = Base64.de code(EN codedString,Base64.DEFAULT);
      点阵位图= BitmapFactory.de codeByteArray(EN codeByte,0,EN codeByte.length);
      返回的位图;
   }赶上(例外五){
      e.getMessage();
      返回null;
   }
}
 

In my application i want to send bitmap image to the server in the form of string, i want to know how many ways are available to convert a bitmap to string. now i am using Base64 format for encoding and decoding, it takes little bit more memory. is there any other possibilities to do the same thing in different ways which takes less memory cosumptions. Now i am using this code.

Resources r = ShowFullImage.this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.col);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

解决方案

public String BitMapToString(Bitmap bitmap){
     ByteArrayOutputStream baos=new  ByteArrayOutputStream();
     bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
     byte [] b=baos.toByteArray();
     String temp=Base64.encodeToString(b, Base64.DEFAULT);
     return temp;
}

Here is the reverse procedure for converting string to bitmap but string should Base64 encoding

/**
 * @param encodedString
 * @return bitmap (from given string)
 */
public Bitmap StringToBitMap(String encodedString){
   try {
      byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
      Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
      return bitmap;
   } catch(Exception e) {
      e.getMessage();
      return null;
   }
}

阅读全文

相关推荐

最新文章