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 with java
Using Apache IOUtils
and Base64
:
byte[] imageBytes = IOUtils.toByteArray(new URL("...")));
String base64 = Base64.getEncoder().encodeToString(imageBytes);
Converting javafx image to/from base64
Path path = Paths.get("...");
byte[] bytes = Files.readAllBytes(path);
And then
byte[] img = Base64.getDecoder().decode(bytes);
Or with better memory usage:
InputStream in = Base64.getDecoder().wrap(Files.newInputStream(path));
Image image = new Image(in);
One problem is that there are several Base64 classes historically, you need java.util.Base64
.
Convert selected image into base64 string in Android (Java)
Please follow this to get actual image.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
String imgString = toBase64(photo);
}
}
public String toBase64(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.NO_WRAP);
}
Converting specific image to base64 string is incorrect
Looks like I was using Resttemplate to make the initial call to get the image URL in the response. I included the .pfx certificates in this resttemplate call. However, when I make the second call using new URL() I did not include the certificates and I was retrieving a failed html response that I encoded.
How to convert a image into Base64 string without compressing the image?
First of all using PNG compression will not lose image quality.
If you want to get the bytes without compression anyway, you can try the following:
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
To get Bitmap
back from byte array:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data));
return bitmap;
Related Topics
Raising a Number to a Power in Java
Java: How to Indent Xml Generated by Transformer
Exception Thrown Inside Catch Block - Will It Be Caught Again
What Is the Most Efficient Java Collections Library
Are There Best Practices for (Java) Package Organization
Convert Seconds Value to Hours Minutes Seconds
How to Get the Session Object If I Have the Entity-Manager
How to Enumerate Ip Addresses of All Enabled Nic Cards from Java
How to Create Change Listener for Variable
Persistent Httpurlconnection in Java
Javafx Using Objects from Maincontroller or Other Controllers in Proper Controller Class
Java, List Only Subdirectories from a Directory, Not Files
How Is This Private Variable Accessible
Javamail API to Imail -- Java.Net.Socketexception: Permission Denied: Connect
Java Singleton and Synchronization
Get Yesterday's Date Using Date