Currently, I'm working in an Android application project at my University, that post image to a web-service and using OpenCV to process this image in some way. In android, I convert a bitmap to byte array like this:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteData = byteArrayOutputStream.toByteArray();
Then, how can I convert that byteData into bitmap again in C++ in web-service in order to use it in image processing?
I'm not so good in C++, please help.
Related
I need to give the user possibility to share his high score. I took an image (PNG) and wrote the high score on it on it. Can I convert BitMap to PNG and share it without saving the file?
You can use the boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) of the Bitmap class.
ByteArrayOutputStram baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] png = baos.toByteArray();
You will have the byte[] contained the image in png format. You can then use any API you want to share that image.
I was wondering if it's possible to upload an image on the "search by image" page using JSON or JSOUP in Android without using any Google API. Thanks in advance
Yes it is possible to convert an image in Base64 string and send to server using Json or any other way.
e.g.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.facebook);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 70, stream); byte[] byteArray = stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArray,Base64.DEFAULT);
I'm currently using the following method to convert a bitmap into a Base64 encoded string, however for very big images I'm getting an OutOfMemory error:
public static String convertBitmapToBase64String(Bitmap bmp) throws OutOfMemoryError
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String base64EncodedImg = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
return base64EncodedImg;
}
I saw this answer, however it uses IOUtils: https://stackoverflow.com/a/24877734/720175
Is it possible to do convert a large bitmap into a base64 encoded string using only standard libraries normally included?
bmp.compress(Bitmap.CompressFormat.JPEG, (AS LOWER), baos);
bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos);
You can reduce the quality level then its working fine
It is most likely because your final string is too big as well as the temporary space it needs to do the conversion. If you want very large string like this you will need to use a stream in and stream out process.
That is, convert the strings from an input stream and stream the results back out to a file.
You dont have to load the large file at once, load it in chunks which will not lead to outofmemoryexception.
Refer this link to convert large file in chunks of data to base64 string
Is there any way to store image files in firebase using Java api to be used for android application?
I have read this thread Can I store image files in firebase using Java API and still there is no answer. I know that there is an official api for it, called firepano here is the link https://github.com/firebase/firepano but it is for Javascript library.
is there any solution for Java library??
I used this code and now it's working:
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.chicken);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
I want to upload an image to my website via my android app so for that i want to convert my image to Byte[].
i have used the following code but not work for me..
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG,0, bos);
byte[] data=bos.toByteArray();
So please share with me any other way to convert an Image to Byte[]..
Use ByteBuffer:
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);
and use array the way you want...