Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Encodage Base64
Pour fournir des données d'image pour la reconnaissance optique de caractères, vous pouvez spécifier le chemin d'accès de l'URI de l'image, ou envoyer les données d'image sous forme de texte encodé en base64.
La plupart des environnements de développement contiennent un utilitaire natif base64 permettant d'encoder une image binaire en données texte ASCII. Pour encoder un fichier image, procédez comme suit :
Chaque langage de programmation a sa propre manière d'encoder des fichiers image en base64 :
Python
En Python, vous pouvez encoder les fichiers image en base64 comme suit :
# Import the base64 encoding library.importbase64# Pass the image data to an encoding function.defencode_image(image):image_content=image.read()returnbase64.b64encode(image_content)
Node.js
En Node.js, vous pouvez encoder les fichiers image en base64 comme suit :
// Read the file into memory.varfs=require('fs');varimageFile=fs.readFileSync('/path/to/file');// Convert the image data to a Buffer and base64 encode it.varencoded=Buffer.from(imageFile).toString('base64');
Java
En Java, vous pouvez encoder les fichiers image en base64 comme suit :
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.byte[]imageData=Base64.encodeBase64(imageFile.getBytes());
Go
En Go, vous pouvez encoder les fichiers image en base64 comme suit :
import("bufio""encoding/base64""io""os")// Open image file.f,_:=os.Open("image.jpg")// Read entire image into byte slice.reader:=bufio.NewReader(f)content,_:=io.ReadAll(reader)// Encode image as base64.base64.StdEncoding.EncodeToString(content)
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eBase64 encoding allows you to send image data as text, which is useful for transmitting images in contexts where only text is supported.\u003c/p\u003e\n"],["\u003cp\u003eVarious operating systems like Linux, Mac OSX, and Windows, as well as their respective command lines, have native utilities or commands to perform base64 encoding on image files.\u003c/p\u003e\n"],["\u003cp\u003eMultiple programming languages including Python, Node.js, Java, and Go provide built-in libraries or functions for base64 encoding image data, each with its unique implementation.\u003c/p\u003e\n"],["\u003cp\u003eBase64-encoded image data can be directly embedded within a JSON request, specifically in the "content" field of an "image" object, for processing by applications requiring image input.\u003c/p\u003e\n"]]],[],null,["# Vertex AI services\n\nBase64 encoding\n---------------\n\nYou can provide image data for optical character recognition by specifying the URI path to the\nimage, or by sending the image data as base64-encoded text.\n\nMost development environments contain a native `base64` utility to\nencode a binary image into ASCII text data. To encode an image file: \n\n### Linux\n\n```\n base64 input.jpg \u003e output.txt\n```\n\n### Mac OSX\n\n```\n base64 -i input.jpg -o output.txt\n```\n\n### Windows\n\n```bash\n C:\u003e Base64.exe -e input.jpg \u003e output.txt\n```\n\n### PowerShell\n\n```bash\n [Convert]::ToBase64String([IO.File]::ReadAllBytes(\"./\u003cvar translate=\"no\"\u003einput.jpg\u003c/var\u003e\")) \u003e output.txt\n```\n\nYou can then use this output image data natively within the JSON request: \n\n```\n{\n \"requests\":[\n {\n \"image\":{\n \"content\": \"BASE64_ENCODED_DATA\"\n },\n \"features\": [\n {\n \"type\":\"TEXT_DETECTION\",\n \"maxResults\":1\n }\n ]\n }\n ]\n}\n```\n\nEach programming language has its own way of base64 encoding image files: \n\n### Python\n\nIn Python, you can base64-encode image files as follows: \n\n # Import the base64 encoding library.\n import base64\n\n # Pass the image data to an encoding function.\n def encode_image(image):\n image_content = image.read()\n return base64.b64encode(image_content)\n\n### Node.js\n\nIn Node.js, you can base64-encode image files as follows: \n\n // Read the file into memory.\n var fs = require('fs');\n var imageFile = fs.readFileSync('/path/to/file');\n\n // Convert the image data to a Buffer and base64 encode it.\n var encoded = Buffer.from(imageFile).toString('base64');\n\n### Java\n\nIn Java, you can base64-encode image files as follows: \n\n // Import the Base64 encoding library.\n import org.apache.commons.codec.binary.Base64;\n\n // Encode the image.\n byte[] imageData = Base64.encodeBase64(imageFile.getBytes());\n\n### Go\n\nIn Go, you can base64-encode image files as follows: \n\n```go\n import (\n \"bufio\"\n \"encoding/base64\"\n \"io\"\n \"os\"\n )\n\n // Open image file.\n f, _ := os.Open(\"image.jpg\")\n\n // Read entire image into byte slice.\n reader := bufio.NewReader(f)\n content, _ := io.ReadAll(reader)\n\n // Encode image as base64.\n base64.StdEncoding.EncodeToString(content)\n```"]]