有多少的方式来位图转换为字符串,反之亦然? [英] How many ways to convert bitmap to string and vice-versa?

查看:218
本文介绍了有多少的方式来位图转换为字符串,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想发送的位图图像到服务器字符串的形式,我想知道有多少种方法可用于一个位图转换为字符串。现在我使用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);
     返回温度;
}
 

下面是相反的过程将字符串转换为位图,但字符串应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;
   }
}

这篇关于有多少的方式来位图转换为字符串,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆