How Many Ways to Convert Bitmap to String and Vice-Versa

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

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

Convert Bitmap image to String format to send over network(LAN) and vice-versa

Try to convert it to a byte array:

public static byte[] ImageToByteArray(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();

byteArray = stream.ToArray();
}
return byteArray;
}

I believe you can simply cast a Bitmap object to an Image object. So Image img = (Image)myBitmap; - then pass that into the method above.

Converting bitmap to bytearray to string then convert back to bitmap is always null in Android

To properly convert a byte[] to a String, you should use Base64.encodeToString().

Documentation

Android Bitmap to Base64 String

use following method to convert bitmap to byte array:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();

to encode base64 from byte array use following method

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

How to convert Bitmap to a Base64 string?

The characters you get by doing System.Text.Encoding.UTF8.GetString(imageBytes) will (almost certainly) contain unprintable characters. This could cause you to only see those few characters. If you first convert it to a base64-string, then it will contain only printable characters and can be shown in a text box:

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);

// Write the bytes (as a Base64 string) to the textbox
richTextBox1.Text = base64String;

convert string into bitmap got null

render a string as a Bitmap

    public void drawText(String text, int textSize) {

// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);

StaticLayout mTextLayout = new StaticLayout(text, textPaint,
370, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(370, mTextLayout.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);

// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawPaint(paint);

// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();

printBitmap(b);
}

i found that, the code that im using previously is for changing the bitmap - stringbase 64 - bitmap

thanks to selbie, he realise me about i explain about the byte array of a bitmap file encoded in a base64 string not the render text.



Related Topics



Leave a reply



Submit