对不起,如果我做的事情很傻。我试图做的是从android中的camera2 api捕获图像,之后将该图像转换为base64编码,下面是我的代码
private String getBase64Image(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 15, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
String base64Image = Base64.encodeToString(bytes, Base64.NO_WRAP);
return base64Image;
}
private void decodeBase64Image(String base64Image) {
byte[] bytes = Base64.decode(base64Image, Base64.NO_WRAP);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(bitmap);
saveFile(bytes);
}
以及我如何捕捉图像
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = imageReader.acquireLatestImage();
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
byte jpegBytes[] = new byte[byteBuffer.remaining()];
byteBuffer.get(jpegBytes);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(jpegBytes, 0, jpegBytes.length);
String base64Image = getBase64Image(imageBitmap);
Log.d("mi", "widthpx:" + image.getWidth() + " heightpx:" + image.getHeight());
Log.d("mi", "Base64Image:" + base64Image);
decodeBase64Image(base64Image);
image.close();
}
}, null);
上面的代码正常工作将其转换为base64编码,然后解码并在imageview上设置,我可以看到。它工作正常,直到这里。但问题是,当我使用base64编码并尝试使用一些在线base64字符串到图像转换器时,它不会给我任何结果。下面是我如何获得base64编码的字符串
/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDADUlKC8oITUvKy88OTU / UIVXUElJUKN1e2GFwarLyL6qurfV8P // 1EL / 5re6 //////////// ZV ////////////// 2wBDATk8PFBGUJ1XV53 / 3Lrc //// ////////////////////////////////////////////////// ////////////// wAARCA2YEiADASIAAhEBAxEB / 8QAGQABAQEBAQEAAAAAAAAAAAAAAAECAwQF / 8QALBABAQACAgICAgIDAAIDAQEBAAECESExA0ESEzJRImEEcYEzQhQ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bxzoPZtXDHyt45y + 0HQSZQBRAVQAABAABUUARQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAQAAAAAAAAAAAAAAFAQAAFBBUBUVAUEBUABUAURQAARRAAAAAAAAAAAAAAAAAAAAARFAQABNiZXUoOflz1NR5cq15cvlkxVEAEUNHoC1ILBWsZt2xmmMY6yAsjcMY0gigAAACgAAAAAAAAAAAAKigAAAAAACKAAAAAAAAAAAAAAAAAKgAqAAoIqKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACggAAKCCoCoAAKCAoAACKAAAigAigCKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIoAigAAIqKAAAAAAAAAAAAAAAACUAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
解
从@toble回答我没有在Log.d()中获得完整的base64Image字符串,所以我记录完整的base64Image的方法如下所示。
private void logLargeString(String content) {
if (content.length() > 3000) {
Log.d("mi", content.substring(0, 3000));
logLargeString(content.substring(3000));
} else {
Log.d("mi", content);
}
}
希望这对其他人有所帮助。
答案 0 :(得分:3)
您的base64字符串仅包含jpeg图像的开头。只有图像的前60行包含实际的彩色像素。
确保使用完整的base64字符串,您应该能够看到完整的图像。您可能使用记录的字符串:
Log.d("mi", "Base64Image:" + base64Image);
输出可能在logcat中被裁剪。