i cannot resolve encodeToString() for base64.
i tried import android.util.Base64 but still it doesnt work
it also show error in Base64.DEFAULT and still i tried Base64.NO_WRAP and is also did not work
public String convertBmToStr(Bitmap bmp) {
ByteArrayOutputStream strm = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, strm); //compress to which format you want.
byte[] byte_arr = strm.toByteArray(); //byte array of image
String imagetoStr = Base64.encodeToString(byte_arr,Base64.DEFAULT);
return imagetoStr;
}
A data URI follows a certain scheme:
data:[<media type>][;base64],<data>
You need to manually add the prefix:
String imagetoStr = "data:image/png;base64," + Base64.encodeToString(
byte_arr, Base64.NO_WRAP);
Related
I'm working on eye project detection using Tensorflow lite on Android.
I have got false values for x and y coordinates of landmarks that should be drawn on the eye region.
So I have tried to follow the original source of the error and I figured out that the problem is coming from the image ( bitmap ).
So I have tried to compare the base64 encoded string of the same image using two functions on Android ( java code ) and on python.
Unfortunately, I have got different results of base64 String encoded.
The image is from 300VW ibug dataset: (112*112)
Python code:
import os
import base64
image = 'irisdata-300VW_Dataset_2015_12_14-017-000880.jpg'
encoded_string = ""
with open(image, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = encoded_string
print(encoded_string)
Java code ( Android ):
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
public static String convert(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP);
}
myOptions2.inDither = true;
myOptions2.inScaled = true;
myOptions2.inPreferredConfig = Bitmap.Config.RGB_565;// important
myOptions2.inPurgeable = true;
bitt1 = BitmapFactory.decodeResource(getResources(), R.drawable.image000880);
Bitmap bitt2 = Bitmap.createBitmap(bitt1);
Bitmap bitt3 = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
image2.buildDrawingCache();
Bitmap bmap = image2.getDrawingCache();
String bs64=getEncoded64ImageStringFromBitmap(bmap);
Log.i("Bitmap chaine"," new chaine "+bs64);
String ch=convert(bitt1);
String ch1=convert(bitt2);
String ch2=convert(bitt3);
Log.i("chaine 1",""+ch);
Log.i("chaine 2",""+ch1);
Log.i("chaine 3",""+ch2);
Logs
Android logcat:
2019-08-23 10:46:47.298 30203-30203/com.example.irisdetection I/chaine 1: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCADgAOADASIAAhEBAxEB/8QAHgAAAgMBAQEBAQEAAAAAAAAABwgFBgkECgMCAQD/xABAEAABAwIEBAMGBgAFAwUAAwABAgMRBCEABQYxBxJBURNhcQgigZGh8BQyscHR4QkVI0LxFjNiCiQlQ1JUssL/xAAcAQACAwEBAQEAAAAAAAAAAAAEBQMGBwIBAAj/xAA2EQABAgQEBAUDBQACAwEBAAABAhEAAyExBEFRYQUScYGRobHB8AYT0QciMuHxI0IUFVIkYv/aAAwDAQACEQMRAD8A8XdBXOVmVIfCyoOqAXCTYBYB2HmZ2N8NS3wS09UcFX+IKHfDrUoSTIV+bxS31MT8I+WFp0XRpGjauqcUFOsFqAU3HM7Ha0gzfuBh41ZswPZYfoXHkodcbskKHMYqpHui4naT8MEf+pllYKTndxUeVmyzgdM4S5pDihDGm35LVze1iX7ItI7nHBfiLl7VSC7lzVJA92XApxw2Fp5QJttgAZglSMxqqR5wPhDzwSI5S2UrWVSRvMdfhOGH/wAOLK6PP6biB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ReAvPNc1dWFsrMrYP410YXiOpypVInj4jh6jq6bscXczBVJkY9OtcfPKS0hiJBSeZicemP8ACvVLrQ9DuG3ypt9CDiud1H4QS3xkbRdVjZ3DlY5F28keuTmvVp59hK0bSdjyquSYuk/d1R//2Q=='
How can I fix this error?
So i have a bitmap and now i want to convert it into an imageUri (or string),
i am using this code here but its just doesn't work instead of returning the imageUri its returning a long random text.
Here is my code :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
saveBitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos);
String path = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT);
And this is what i am getting :
Instead of Base64.DEFAULT, use Base64.NO_WRAP
String path = Base64.encodeToString(baos.toByteArray(),Base64.NO_WRAP);
try below way, should be work
byte[] data = convert image in byte.
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
Base64.encodeToString() encodes the byte array in a string. This isn't your uri. Rather this is your image/bitmap in Base64. You can use suitable Base64.decode to get back the byte array.
To get uri, you can use some of the other options including
Uri.fromFile(new File("your_file_path));
try {
val imageStream: InputStream? = requireActivity().getContentResolver().openInputStream(mProfileUri)
val selectedImage = BitmapFactory.decodeStream(imageStream)
val baos = ByteArrayOutputStream()
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b = baos.toByteArray()
val encodedString: String = Base64.encodeToString(b,Base64.DEFAULT)
Log.d("check string" ,encodedString.toString())
} catch (e: IOException) {
e.printStackTrace()
}
For kotlin use this code and this is running successfully image to base64 when upload image to server . just put image uri "imageStream" here thats it.
Sorry guys, i thought Base64.encodeToString() will return me the imagePath, but i was wrong. Anyways i got the solution,
Here is the code that i have used,
ByteArrayOutputStream baos = new ByteArrayOutputStream();
saveBitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),saveBitmap,"Title",null);
In Java server I fetch image from external service URL like:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
Then on Android I parse it like:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Result: Image not displayed, ain't errors/exceptions neither on server nor on client.
What is the problem here?
EDIT: On android I use class android.util.Base64
Thanks,
Use Picasso library to load image:
You just need to add 1 line of code to show the image on ImageView
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
You can learn more from here
use this to convert to base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
As commented, let's assume base64Content is the base64 string responsed from your web service/server-side app, you can refer to the following sample code:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Moreover, if your server compressed reponse data either by gzip or deflate, your client app must decompress the data first.
Hope this helps!
how to convert gif to base64?
here's what i try so far
public static String convertImageToBase64(String urlString, String type ){
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
URL url = new URL(urlString);
BufferedImage img = ImageIO.read( url );
ImageIO.write(img, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
code above is working, but it lost the animation
Most likely the class BufferedImage does not support images with animation. So the code opens and parses the image only to the first frame.
Instead try directly getting the bytes with URL.openStream and then just convert the downloaded bytes to base64.
Notice that this way you can't be sure that the downloaded file is actually an image since you are not opening it at all. This may or may not be needed.
You have to use
public String encodeToString(byte[] src)
of class BASE64.Encoder (from java 8)
I'm assuming what you want is a Base64 representation of the GIF file itself, not the ImageIO representation. If you have an older version of Java without built-in Base64 support, Apache Commons Codec will do the job. Read the file into a byte array using URL.openStream(), not with ImageIO, then call Base64.encodeBase64String. If you want the result URL encoded, call Base64.encodeBase64URLSafe instead.
If you actually want the ImageIO representation, then you're stuck with losing the animation.
I am trying to update photo using Google apps engine.I have imageurl i convert it into byte array and then encode
it using base64.i got encoded string,now i m trying to update photodata using directory API Reference
https://developers.google.com/admin-sdk/directory/v1/reference/users/photos#resource
after update i got error invalid byteString.I face this problem from yesterday.So Let me know where i did wrong? Below is my code.
import com.google.appengine.repackaged.org.apache.commons.codec.binary.Base64;
above class used for Base64.
URL url = new URL(myImageUrl);
ByteArrayOutputStream bais = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
bais.write(byteChunk, 0, n);
}
System.out.println(byteChunk);
byte[] encoded = Base64.encodeBase64(byteChunk);
String ecodedString = new String(encoded, "UTF-8");
ecodedString = ecodedString.replace("/", "_");
ecodedString = ecodedString.replace("+", "-");
ecodedString = ecodedString.replace("=", "*");
System.out.println(ecodedString);
Padding could be the problem, try not replacing "=" with "*". See also:
Converting string to web-safe Base64 format
p.s. repackaged libs are discouraged in app engine; you may use DatatypeConverter.printBase64Binary() instead.