首页 > 解决方案 > 将base64字符串解密为jpeg图像时损坏的图像

问题描述

我的 android 应用程序正在将位图图像(临时捕获)作为 base64 编码字符串发送到远程服务器。代码如下。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
takenImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

现在我正在使用下面的代码解密图像

function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' );

     #spliting the string on commas
     #$data[ 0 ] == "data:image/png;base64"
     #$data[ 1 ] == <actual base64 string>
     $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ));   #

    // clean up the file resource
    fclose( $ifp );

    return $output_file;
    }

现在我收到一个错误

Undefined offset : 1 at base64_decode($data[1])

这意味着 data:image/png;base64 部分之后没有字符串。但我已经测试过该字符串在发布请求之前就在那里。如果我考虑到整个接收到的字符串,那么我会得到一个绿色阴影图像,无论拍摄的图像如何,它都是恒定的。

我错过了什么吗?

标签: phpandroidhttppostbase64

解决方案


推荐阅读