您好,我想解码一个Base64字符串,这是一个我用我编码的方式编码的图像,我将其转换为位图,然后对位图进行编码,现在我正在try 用Python对其进行解码,这就是我所做的.我有一个包含Base64数据的hash.txt文件,我复制该文件,将其放在变量定义中,它起作用了,然后我try f.read(),但失败了,这是代码
import base64
from PIL import Image
from io import BytesIO
def decode_base64_to_bitmap_and_save(base64_string, output_file_path):
try:
# Decode the base64 string to bytes
decoded_bytes = base64.b64decode(base64_string)
# Create a BytesIO stream from the decoded bytes
byte_stream = BytesIO(decoded_bytes)
# Open the image using Pillow (PIL)
decoded_bitmap = Image.open(byte_stream)
# Save the decoded bitmap as a PNG file
decoded_bitmap.save(output_file_path, "PNG")
return True # Successful save
except Exception as e:
print(f"Error: {e}")
return False # Saving failed
# Example usage:
file_path = "hash.txt"
with open(file_path, "r") as file:
base64_string = file.read()
output_file_path = "output.png"
if decode_base64_to_bitmap_and_save(base64_string, output_file_path):
print(f"Image saved as {output_file_path}")
else:
print("Failed to decode and save the image.")
我try 了f.read我try 了f.read的+"=="以添加填充我try 用""删除\n所以我从Base64中删除了所有\n
这是编码文件
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
Bitmap imageBitmap = (Bitmap) extras.get("data");
String encodedString = bitMapToBase64(imageBitmap);
AfterPictureBase64(encodedString);
this是Base64文件