How can I convert an image into Base64 string using JavaScript?
You can use the HTML5 <canvas>
for it:
Create a canvas, load your image into it and then use toDataURL()
to get the Base64 representation (actually, it's a data:
URL, but it contains the Base64-encoded image).
How can I convert an image into a Base64 string?
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap object
byte[] b = baos.toByteArray();
* Update *
If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 AKA version 2.2).
Check this article out for a workaround:
How to base64 encode decode Android
Convert image.jpg to Base64
This worked for me: https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
I can't believe C++ doesn't have base64 functionality in the standard library!
#include <fstream>
#include <string>
#include "base64.h"
using namespace std;
int main()
{
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input, line)) {
string encoded = base64_encode(reinterpret_cast<const unsigned char*>(line.c_str()), line.length());
output << encoded;
}
input.close();
}
}
How to convert an Image to base64 string in java?
The problem is that you are returning the toString()
of the call to Base64.encodeBase64(bytes)
which returns a byte array. So what you get in the end is the default string representation of a byte array, which corresponds to the output you get.
Instead, you should do:
encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8");
Convert Image to Base64 string
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile file1 = await storageFolder.GetFileAsync("Image.png");
string _b64 = Convert.ToBase64String(File.ReadAllBytes(file1.Path));
This worked for me.
Convert Image to Base64 String Android
I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so the broken string didn't decode the image for me.
So when i passed the encoded string directly to the decode function the image was visible.
Converting Image to BASE64 String in swift
Actually it will not take time to convert but for printing, it will take more time so don't print it...
Converting an image to base64 in angular 2
Working plunkr for base64 String
https://plnkr.co/edit/PFfebmnqH0eQR9I92v0G?p=preview
handleFileSelect(evt){
var files = evt.target.files;
var file = files[0];
if (files && file) {
var reader = new FileReader();
reader.onload =this._handleReaderLoaded.bind(this);
reader.readAsBinaryString(file);
}
}
_handleReaderLoaded(readerEvt) {
var binaryString = readerEvt.target.result;
this.base64textString= btoa(binaryString);
console.log(btoa(binaryString));
}
How can I convert an image into Base64 string from an image source?
I think you want to do this https://jsfiddle.net/samet19/yv9a4op8/
function encodeImageFileAsURL() {
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0) {
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
var newImage = document.createElement('img');
newImage.src = srcData;
document.getElementById("output").src = newImage.src;
}
fileReader.readAsDataURL(fileToLoad);
}
}
Related Topics
Horizontal Listview in Android
How to Send a "Multipart/Form-Data" Post in Android With Volley
What's the Difference Between the Various Methods to Get an Android Context
Android Fragment Handle Back Button Press
How to Get HTML Content from a Webview
Android Min Sdk Version Vs. Target Sdk Version
How to Make a Custom Keyboard in Android
Handle Textview Link Click in My Android App
Android Studio Suddenly Cannot Resolve Symbols
How to Display a Toast from a Background Thread on Android
Integrating the Zxing Library Directly into My Android Application
How to Put Google Maps V2 on a Fragment Using Viewpager
How to Completely Uninstall Android Studio from Windows(V10)
How to Store Image as Blob in Sqlite & How to Retrieve It