0

我正在尝试将位码图像转换为 base64 格式。我试着像这样编码

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.URL_SAFE);
}

生成的响应低于它不会再次给我图像。

有人可以在这里提供一些指示吗?我可能做错了什么?

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAMgAlgDASIA AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3 uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+P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GB9z /sa/8E7vFHjGa0T4a/BHW7XTNV+x6r40xpunen/LT1PPQdSev+1z/FL4D8D3XiHxF4j8Z3Nr9stP8AkG+HPt+f7z9/ 8cckcjlj+rLeXpD/ANKqH0vDG0vWH/pVQ1NO0PG7n7ZpNkf7voX4+8T1/PoMnOPWPC3hs+Hrc3P2 Y2l3rWO3Xlz3PThf0zySTseHvAzAWlra2/237Fj+0eB2L9+fx9cgZOa7dZ2tg1rodve6xqv+6OmW 9+PuqT65HJ2jPxv1yXn90f8AI90xra30fw8tp9qP+i8HPP8AxNPmkB4z/u8dsn3J9Z8L/A/x9rdv b6oNKFppQ249vmOTzjrxn8OpGB9z /sa/8E7vFHjGa0Y2l3rWO3Xlz3PThf0zySTseHvAzAWlra2/237Fj+0eB2L9+fx9cgZOa7dZ2tg1rodve6xqv+6OmW 9+PuqT65HJ2jPxv1yXn90f8AI90xra30fw8tp9qP+i8HPP8AxNPmkB4z/u8dsn3J9Z8L/A/x9rdv b6oNKFppQ249vmOTzjrxn8OpGB9z/sa/8E7vFHjGa0Y2l3rWO3Xlz3PThf0zySTseHvAzAWlra2/237Fj+0eB2L9+fx9cgZOa7dZ2tg1rodve6xqv+6OmW 9+PuqT65HJ2jPxv1yXn90f8AI90xra30fw8tp9qP+i8HPP8AxNPmkB4z/u8dsn3J9Z8L/A/x9rdv b6oNKFppQ249vmOTzjrxn8OpGB9z/sa/8E7vFHjGa0

4

2 回答 2

1

试试这个功能:

public static String imageToString(Bitmap BitmapData) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BitmapData.compress(Bitmap.CompressFormat.PNG, 100, bos);
        byte[] byte_arr = bos.toByteArray();

        String file = Base64.encodeToString(byte_arr, Base64.DEFAULT);
        //appendLog(file);
        return file;
    }
于 2017-04-25T10:44:08.017 回答
0

这里有一些误解。在您的方法中,您通过参数接收位图(图像)。如果您想将它传递给base64format,那么您做对了,因为base64format 只是一个大字符串,正如您在 return 中看到的那样。

如果错误在您的方法中并且在反向过程中(将其解析为String),您可以尝试此功能:Base64Bitmap

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

base64byte array使用以下方法进行编码:

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

另外,尝试使用此方法来反转编码过程:

byte[] decodedString = android.util.Base64.decode(yourString, android.util.Base64.DEFAULT);
                            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
于 2017-04-25T10:49:46.930 回答