我正在开发一个应用程序,我需要从相机中捕获图像.捕获后,我必须将位图转换为Base64.转换为Base64后,我必须将该字符串发送到SERVER.我正在使用以下代码执行此任务:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
base64Image = Base64.encodeToString(b,Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
问题:当我将Base64转换为图像时,我得到了不完整的图像.同样的结果发生在服务器上,我的图像没有完全从Base64 String重构.
请建议我的解决方案.我已经搜索了很多并获得了我正在使用的相同代码.
编辑:请看下面不完整的图片

代码用于捕获图像:
intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PHOTO);
Run Code Online (Sandbox Code Playgroud)
当我将该 Base64 转换为图像时,我得到的图像不完整
尝试对您的图像执行此操作Bitmap,并检查是否有不符合预期的情况:
Bitmap originalBitmap = (Bitmap) data.getExtras().get("data"); //or whatever image you want
Log.d(TAG, "original bitmap byte count: " + originalBitmap.getByteCount());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
originalBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
Log.d(TAG, "byte array output stream size: " + baos.size());
byte[] outputByteArray = baos.toByteArray();
Log.d(TAG, "output byte array length: " + outputByteArray.length);
String base64EncodedString = Base64.encodeToString(outputByteArray, Base64.DEFAULT);
Log.d(TAG, "base64 encoded string length: " + base64EncodedString.length());
byte[] inputByteArray = Base64.decode(base64EncodedString, Base64.DEFAULT);
Log.d(TAG, "input byte array length: " + inputByteArray.length);
ByteArrayInputStream bais = new ByteArrayInputStream(inputByteArray);
Log.d(TAG, "byte array input stream size: " + bais.available());
Bitmap decodedBitmap = BitmapFactory.decodeStream(bais);
Log.d(TAG, "decoded bitmap byte count: " + decodedBitmap.getByteCount());
Log.d(TAG, "decoded bitmap same as original bitmap? " + decodedBitmap.sameAs(originalBitmap));
Run Code Online (Sandbox Code Playgroud)
如果一切正常,那么问题不在于 Base64 编码。让我知道!
| 归档时间: |
|
| 查看次数: |
7466 次 |
| 最近记录: |