Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many ways to convert bitmap to string and vice-versa?

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); 
like image 349
RajaReddy PolamReddy Avatar asked Nov 26 '12 10:11

RajaReddy PolamReddy


People also ask

What can a bitmap be converted to?

Software such as Adobe Illustrator may be able to convert bitmap images to vector images. Vector images can be converted to bitmap images by opening them with Adobe Photoshop.

Can object be converted into bitmap?

Converting a vector graphic or object to a bitmap lets you apply special effects to the object with CorelDRAW. The process of converting a vector graphic to a bitmap is also known as “rasterizing.” When you convert the vector graphic, you can select the color mode of the bitmap.

Is bitmap a base64?

This tool base64-encodes BMP (bitmap) images. As base64 is text-based encoding and can be viewed and read on different screens, you can split the base64 output into individual lines with a certain length.


2 Answers

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;    } } 
like image 69
sachin10 Avatar answered Oct 05 '22 22:10

sachin10


Yes, You can do it by implenment this code :

String to Bitmap :

 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;     } } 

Bitmap to String :

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; } 
like image 37
Mitul Goti Avatar answered Oct 06 '22 00:10

Mitul Goti