Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不压缩图像的情况下将图像转换为Base64字符串?_Java_Android_Image - Fatal编程技术网

Java 如何在不压缩图像的情况下将图像转换为Base64字符串?

Java 如何在不压缩图像的情况下将图像转换为Base64字符串?,java,android,image,Java,Android,Image,每次我更新同一张图片时,图像压缩方法会降低图像质量 public String BitMapToString(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); base64Image = Bas

每次我更新同一张图片时,图像压缩方法会降低图像质量

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

我不想一次又一次地压缩图像。请提出建议。

首先,使用PNG压缩不会降低图像质量

如果仍要在不压缩的情况下获取字节,可以尝试以下操作:

ByteBuffer buffer = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
要从字节数组获取位图,请执行以下操作:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data));
return bitmap;

“更新同一张图片”是什么意思?
删除这行位图.compress(bitmap.CompressFormat.PNG,100,baos);没有。PNG压缩是无损的。@RakshitNawani如果删除该行,
ByteArrayOutputStream
中将没有任何内容。“更新相同的图片”是否意味着您在循环中进行此操作?请澄清您的问题@Sam这条线的宽度和高度是多少<代码>位图位图=Bitmap.createBitmap(宽度、高度、Bitmap.Config.ARGB_8888)