将位图转换为字节数组,转换为字符串,然后一直返回

时间:2021-06-06 20:14:00

I have tried several different methods found in the java documentation, as well as several solutions from other SO questions and have successfully gotten a Bitmap to convert to a byte[] and back again.

我已经尝试了在java文档中找到的几种不同的方法,以及来自其他SO问题的几种解决方案,并且已经成功地将Bitmap转换为byte []并再次返回。

The problem is that I now need to convert this byte[] to a String, then back to a byte[], then back to a Bitmap again. To recap what I need:

问题是我现在需要将此byte []转换为String,然后再转换为byte [],然后再转换回Bitmap。回顾一下我需要的东西:

Bitmap -> byte[] -> String -> byte[] -> Bitmap

位图 - > byte [] - > String - > byte [] - > Bitmap

I know this sounds strange but what I'm trying to accomplish must be done this way. Below is what I have tried, if anyone could point out what I'm doing wrong I'd greatly appreciate it!

我知道这听起来很奇怪,但我想要完成的事情必须以这种方式完成。以下是我的尝试,如果有人能指出我做错了什么我会非常感激!

Bitmap bitmap = mv.getDrawingCache();

// Convert bitmap to byte[]
ByteArrayOutputStream output = new ByteArrayOutputStream(bitmap.getByteCount());
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
byte[] imageBytes = output.toByteArray();

// Convert byte[] to string
// I have also tried using Base64.encodeToString(imageBytes, 0);

String encodedString = new String(imageBytes);

// Convert string to byte[]
byte[] newImageBytes = encodedString.getBytes();

if (imageBytes == newImageBytes) {
    Toast.makeText(SignatureActivity.this, "SUCCESS!!", Toast.LENGTH_SHORT).show();
} else { // Sadly, we always get to this point :(
    Toast.makeText(SignatureActivity.this, "BOOO", Toast.LENGTH_SHORT).show();
}

// Convert byte[] back to bitmap
bitmap = BitmapFactory.decodeByteArray(newImageBytes, 0, newImageBytes.length);

Again, going Bitmap -> byte[] -> Bitmap was successful, but adding in the conversion to a String and back is causing the final Bitmap to write an image of 0kb.

再次,去Bitmap - > byte [] - > Bitmap是成功的,但是将转换添加到String并返回导致最终的Bitmap写入0kb的图像。

1 个解决方案

#1


2  

The problem is not in the conversion, but how you verify the result. using == to compare two arrays only returns true if they are the same array reference. Since you create a new array with byte[] newImageBytes = encodedString.getBytes(); This will always be false. See this question for reference.

问题不在于转换,而是如何验证结果。使用==比较两个数组只有它们是相同的数组引用才返回true。由于您使用byte [] newImageBytes = encodedString.getBytes()创建一个新数组;这总是错误的。请参阅此问题以供参考。

On another note, if you are going to transfer or use the string in some way, it is probably better to use Base64.encodeToString(imageBytes, Base64.NO_WRAP); to get a string, and get it back with Base64.decode(encodedString, Base64.NO_WRAP).
You can also get the byte array without compressing it, with the copyPixelsToBuffer() method (see this question for an example).

另外请注意,如果要以某种方式传输或使用字符串,最好使用Base64.encodeToString(imageBytes,Base64.NO_WRAP);获取一个字符串,并使用Base64.decode(encodedString,Base64.NO_WRAP)将其取回。您还可以使用copyPixelsToBuffer()方法获取字节数组而不压缩它(请参阅此问题的示例)。

#1


2  

The problem is not in the conversion, but how you verify the result. using == to compare two arrays only returns true if they are the same array reference. Since you create a new array with byte[] newImageBytes = encodedString.getBytes(); This will always be false. See this question for reference.

问题不在于转换,而是如何验证结果。使用==比较两个数组只有它们是相同的数组引用才返回true。由于您使用byte [] newImageBytes = encodedString.getBytes()创建一个新数组;这总是错误的。请参阅此问题以供参考。

On another note, if you are going to transfer or use the string in some way, it is probably better to use Base64.encodeToString(imageBytes, Base64.NO_WRAP); to get a string, and get it back with Base64.decode(encodedString, Base64.NO_WRAP).
You can also get the byte array without compressing it, with the copyPixelsToBuffer() method (see this question for an example).

另外请注意,如果要以某种方式传输或使用字符串,最好使用Base64.encodeToString(imageBytes,Base64.NO_WRAP);获取一个字符串,并使用Base64.decode(encodedString,Base64.NO_WRAP)将其取回。您还可以使用copyPixelsToBuffer()方法获取字节数组而不压缩它(请参阅此问题的示例)。