在Android中对base64字符串中的位图对象进行编码和解码

时间:2022-12-03 00:07:38

I want to encode and decode Bitmap object in string base64. I use the Android API10,

我想在字符串base64中编码和解码Bitmap对象。我使用的是Android API10,

I have tried, with no success, to use a method in this form to encode a Bitmap.

我尝试使用此表单中的方法对Bitmap进行编码,但没有成功。

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}

3 个解决方案

#1


206  

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

Example usage:

用法示例:

String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);

#2


9  

Hope this will help you

希望对你有帮助

 Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));

(if you are referencing URI to construct bitmap) OR

(如果您引用URI来构造位图)或者

Resources resources = this.getResources();
Bitmap bitmap= BitmapFactory.decodeResource(resources , R.drawable.logo);

(if you are referencing drawable to construct bitmap)

(如果您引用drawable来构造位图)

Then Encode it

然后编码

 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 byte[] image = stream.toByteArray();
 String encodedImage = Base64.encode(image, Base64.DEFAULT);

For Decoding Logic will be exactly reverse of encoding

对于解码逻辑将完全颠倒编码

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

#3


0  

To encode the bimap to image:

要将bimap编码为图像:

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
   byte[] imageBytes = byteArrayOutputStream.toByteArray();
   String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d("bytearray", String.valueOf(byteArrayOutputStream.toByteArray()));
    Log.d("encodedimage",encodedImage);

#1


206  

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

Example usage:

用法示例:

String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);

#2


9  

Hope this will help you

希望对你有帮助

 Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));

(if you are referencing URI to construct bitmap) OR

(如果您引用URI来构造位图)或者

Resources resources = this.getResources();
Bitmap bitmap= BitmapFactory.decodeResource(resources , R.drawable.logo);

(if you are referencing drawable to construct bitmap)

(如果您引用drawable来构造位图)

Then Encode it

然后编码

 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 byte[] image = stream.toByteArray();
 String encodedImage = Base64.encode(image, Base64.DEFAULT);

For Decoding Logic will be exactly reverse of encoding

对于解码逻辑将完全颠倒编码

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

#3


0  

To encode the bimap to image:

要将bimap编码为图像:

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
   byte[] imageBytes = byteArrayOutputStream.toByteArray();
   String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d("bytearray", String.valueOf(byteArrayOutputStream.toByteArray()));
    Log.d("encodedimage",encodedImage);