extends TextureRect
Path to your JSON file
const JSON_FILE_PATH = “res://Image/img_241004_Update/ami_json/1_1_2.json”
func _ready():
# Load the JSON file
var file = FileAccess.open(JSON_FILE_PATH, FileAccess.READ)
if file:
var json_content = file.get_as_text()
var data = JSON.parse_string(json_content)
# Print the parsed data for debugging
print_debug("Parsed JSON data: ", data)
# Check if parsing was successful
if data == null or not data.has("assets"):
print("JSON parsing error or 'assets' key does not exist")
return
# Extract the base64 encoded image data
var base64_image = data["assets"][0]["p"].split(",")[1] # Extract base64 data
# Decode the base64 data
var decoded_image = decode_base64(base64_image)
# Create an Image and load the decoded data
var image = Image.new()
image.load_png_from_buffer(decoded_image) # Assuming the image is in PNG format
# Create a texture and convert the Image to a texture
var texture = ImageTexture.new()
texture.create_from_image(image)
# Create a Sprite to display the image
var sprite = Sprite2D.new()
sprite.texture = texture
add_child(sprite) # Add the Sprite to the scene
file.close()
Decode base64 string
func decode_base64(base64_string: String) → PackedByteArray:
# Convert the string to binary data and decode
var byte_array = PackedByteArray()
byte_array = byte_array.parse_utf8_string(base64_string)
return byte_array
Issue code: byte_array = byte_array.parse_utf8_string(base64_string)