Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Codificação Base64
É possível fornecer dados de imagem para reconhecimento óptico de caracteres especificando o caminho do URI para a imagem ou enviando os dados da imagem como texto codificado em base64.
A maioria dos ambientes de desenvolvimento contém um utilitário base64 nativo para
codificar uma imagem binária em dados de texto ASCII. Para codificar um arquivo de imagem:
Cada linguagem de programação tem sua própria maneira de codificar arquivos de imagem em base64:
Python
Em Python, é possível codificar arquivos de imagem em base64 da seguinte maneira:
# 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
Em Node.js, é possível codificar arquivos de imagem em base64 da seguinte maneira:
// 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
Em Java, é possível codificar arquivos de imagem em base64 da seguinte maneira:
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.byte[]imageData=Base64.encodeBase64(imageFile.getBytes());
Go
Em Go, é possível codificar arquivos de imagem em base64 da seguinte maneira:
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)
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 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```"]]