Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Encoding base64
Anda dapat menyediakan data gambar untuk pengenalan karakter optik dengan menentukan jalur URI ke
gambar, atau dengan mengirimkan data gambar sebagai teks berenkode base64.
Sebagian besar lingkungan pengembangan berisi utilitas base64 native untuk
mengenkode gambar biner ke dalam data teks ASCII. Untuk mengenkode file gambar:
Setiap bahasa pemrograman memiliki cara sendiri untuk mengenkode file gambar base64:
Python
Di Python, Anda dapat mengenkode file gambar base64 sebagai berikut:
# 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
Di Node.js, Anda dapat mengenkode file gambar base64 sebagai berikut:
// 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
Di Java, Anda dapat mengenkode file gambar ke base64 sebagai berikut:
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.byte[]imageData=Base64.encodeBase64(imageFile.getBytes());
Go
Di Go, Anda dapat mengenkode file gambar base64 sebagai berikut:
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)
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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```"]]