how to convert imagebytes to byte[] in android using ksoap? - android

friends,
i am using ksoap library to call dotnet webservice.
i am getting following webservice reponse
<imageByte>R0lGODlhlgBQAPcAAKPBqfP69Ja0m32hgxdVJdrb28zMzPr+/QRKEdPi1IGliL3PwCFXLP38+YSJhPr7+</imageByte>
<imageByte>R0lGODlhlgBQAPcAAKPBqfP69Ja0m32hgxdVJdrb28zMzPr+/QRKEdPi1IGliL3PwCFXLP38+YSJhPr7+</imageByte>
<imageByte>R0lGODlhlgBQAPcAAKPBqfP69Ja0m32hgxdVJdrb28zMzPr+/QRKEdPi1IGliL3PwCFXLP38+YSJhPr7+</imageByte>
removed some extra bytes from string because it was very lengthy.
1) i dont know which format it is.
2) how to convert it to byte[] to display it in android imageview.
any help would be appreciated.

This may be base64 encoding; at least it looks like the right mix of characters. The specific webservice docs should tell you more about the message format.
Assuming it is, and that one image is broken into multiple <imageByte /> tags, combine the contents into one long string. You can then use Base64.decode to get a byte[], and BitmapFactory.decodeByteArray to get a Bitmap handle, which you pass to ImageView.setImageBitmap
Hope this helps,
Phil Lello

Related

how to convert an image into qrcode in android

Actually, I'am developping an application wich should allow users to get photo from the camera and then convert it into qrcode.
I am trying to use xzing library to generate the qrcode and the Base64 java class to convert the image to String.But when I run this application, the class qrcodewriter handles an exception indicating that the data is too big to be converted.I have tried to get some solutions to this problem and the only way that I found is to subdivise the String generated using the Base64 class into substrings and then convert each of these substrings to a subqrcode and finally concatenate these subqrcodes to get the desired result.Can any one help me to find other method that is easier to implement.
Try to resize and compress the image and then convert the compressed image to base64 encoding. This base64 string will be comparatively small. Now generate QR code based on this string. You have to find the trade-off between quality of image and it's size.

ways and usefull libraries for sending image from client (android) to server (Spring)

I find the next possible ways
1) convert Image into Base64 string and put this string into the JSON object.
Send image as json entry android
2) create an array of bytes out of the bitmap, and then create a new string with that array of bytes, and then send it to the server
http://blog.3dmick.com/2012/06/android-app-for-uploading-images-on-server/
Which else libraries or ways do u know ??
Which ways are popular on the enteptise projects?

Decode JSON base64 data (from PHP's base64_encode) in Android?

This is my JSON code. Note the title field is base64_encoded. I want to get this title field value.
{"item":{"id":"1","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}},
{"item":{"id":"4","title":"ZGVtbyBldmVudCBmb3IgZGVtbyBkZXNjcmlwdGlvbg==","status":"1"}}
{"item":{"id":"6","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}}
The title text is in base64 format (ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==) but I would like to get it as normal text -- how can I do this easily?
Thanks.
unfortunately the entire point of encryption is to stop people from doing this...
If you are legally obtaining the encrypted data, try getting in touch with the server team or checking their documentation to find out how they encrypt and how you should decrypt.
There isn't a one size fits all solution for this problem. The answer is 100% dependent on how the String is encrypted in the first place.
good luck!

getting image from byte array in JSON object to android app

Heres my situation:
I have a RESTful WCF service running on my server, the service is meant to get various types of data about people from a database and makes that data available as a single JSON object. It works great!
[EDIT]There is another service that maintains an image cache in the file system on the server. When a request is sent to the RESTful service, that service then requests an image from the image service. If the image is already in the cache (same width, height and person as the request), it returns that (as a byte array). We MUST use this service to retrieve images.
Now what I want is the image of that person. In our database, the image is a long raw (ew). However, I have dealt with that issue already (previous paragraph). The image is now a Byte array. I am pretty new to android and I am not sure what the best way to retrieve this image is. What I thought I could do was add the byte array to the JSON object and then use the base64 decoder to convert it into a drawable image. However, everytime I try, it times out and tells me it expected ',' or ']' at some arbitrary index of the char buffer for the JSON object.
I have been able to pull the small bits of data out of the JSON object without an issue, but now that there is a huge byte array in it, the JSONObject hates me. What would be a better way to get this image from my service?
Base64 encode the byte array to get a string.
Add the string to JSON object and send it.
When JSON is received, get out the string.
Base64 decode it to get back the byte array.
Use byte array to create Image.
See this question on storing images, it's always better to store this sort of data on file system. If possible deprecate that field, and create a script to move existing images to file system.
You should then store the images on a file system (or some sort of content management system) which can be retrieved by a URL.
Then store the URL in the database. you can then send this in your json object.
{
...
image_url:<url from database>
...
}
When the client receives this it will make a call to that URL and download the image.
Your client will have to make a separate call to retrieve the image but it's generally better than filling your database with binary data. This can also work to your advantage if you want to display data fast while allowing the image to be downloaded in the background.
Better than using Base64 encoding is this way of returning Stream (from WCF RAW programming)
[OperationContract, WebGet]
public Stream GetTestImage(Image image)
{
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream;
}

Include Small Image in JSON for Android App

My app currently requests a JSON file with some text and other data from my server. I want to add functionality so that it also downloads a very small image (like an icon) through the same file [without creating an additional request]. Is it possible to do so, and how would I go about it (base64?)
Should be eminently reasonable: look at http://developer.android.com/reference/android/util/Base64.html. All you'd need to do is:
Read your icon into a byte[] array on the server.
(Assuming your server is in java) Use something like http://iharder.sourceforge.net/current/java/base64/ to write the byte[] array into a StringOutputStream through http://iharder.sourceforge.net/current/java/base64/api/index.html?Base64.OutputStream.html.
Add the contents of the String to the JSON file.
On the android device call http://developer.android.com/reference/android/util/Base64.html#decode%28java.lang.String,%20int%29 to convert the JSON attribute into a byte[] array.
You can then pass the byte array to http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29 or one of its brethren functions (you may have to play with image formats/encodings to get it to swallow your byte array correctly).
Voila! You have a Bitmap you can use.
Let me know how that works out.

Categories

Resources