I'm developing an Android App that should send an image to my ASP.NET Web Service where the image will be saved as a file. I've seen a couple of ways to do this and I went for this one: convert the image to a byte array -> convert the byte array to a string -> send the string to the web service using KSOAP2 -> receive the String at the Web Service -> convert it to a byte array ->
Save it as an image:
IVtest = (ImageView)findViewById(R.id.carticon);
BitmapDrawable drawable = (BitmapDrawable) IVtest.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
ImageView image=new ImageView(this);
image.setImageBitmap(bmp);
String strBase64 = Base64.encode(data);
Then I send strBase64 to the web service.
In the Web Service I have this:
public Image ConvertToImage(byte[] image)
{
MemoryStream ms = new MemoryStream(image);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
[WebMethod]
public String UploadImage(String image, String name)
{
byte[] image_byte = Encoding.Unicode.GetBytes(image);
Image convertedImage = ConvertToImage(image_byte);
try {
convertedImage.Save(Server.MapPath("generated_image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
} catch (Exception e) {
return e.Message;
}
return "Success";
}
I get an error at this line: Image returnImage = Image.FromStream(ms);
This is the error I get:
SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
at Service.ConvertToImage(Byte[] image) in e:\FTP\nvm\Service.asmx:line 1366
at Service.UploadImage(String image, String name) in e:\FTP\nvm\Service.asmx:line 1374
--- End of inner exception stack trace ---' faultactor: 'null' detail: org.kxml2.kdom.Node#437bf7b0
Thanks
Seems like there is something iffy with your conversion from string to image. Also you are not disposing your stream which will leak memory.
Try this instead:
private Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0,
imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
Related
I have used this function for encoding the image.
private String encodeImage(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String encImage = Base64.encodeToString(b, Base64.DEFAULT);
Log.d(TAG, "encodeImage: " + encImage);
return encImage;
}
This is how I created the bitmap image
setContentView(R.layout.camera);
cameraView = (CameraView) findViewById(cameras);
cameraView.setJpegQuality(2);
cameraView.setCameraListener(new CameraListener() {
#Override
public void onPictureTaken(byte[] jpeg) {
super.onPictureTaken(jpeg);
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
encodeImage(bitmap);
}
});
While decoding the byte64 string generated by this using online tools i am getting a completely black image.
this is my byte64 string
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCBIACiADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBg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FFMfLk7D36E/wCJOM55xkdaScZlLyH96f8Alv8A
QsfX6/j3xursw8pNy0Vr07+nNPz/ALq+97kqb626dHteV+vZRfzfVMmkSRmMsUsp87Hri5wz8j9T
jryepAYyRIY4448enn9P9GvOQe/r15I5GcAZqJXiDGKPnHE/v8xx3PqR69cZwxq+H8sRyeb/AMsB
5A/4E+e59vfr68lWUot6K15W9FKXn5/l2dzneu
Your encode code is OK, just checked it. There is a big possibility you made mistake trying to copy your encoded string from logcat. Just place breakpoint on Log.d(), copy encoded string value and check it out again via online tools. I used this to test the result.
I don't code in Android but I think you're mixing your data up. For example the manual says decodeByteArray will "Decode an immutable bitmap from the specified byte array." so this line :
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
Would only work if jpeg array contains raw RGB colour values not actual JPEG values (since JPEG is compression math not a colour array).
Try this and see if String encImage can be displayed by some online tool. No usage of that other function private String encodeImage(Bitmap bm) since I suspect it's part of the problem...
setContentView(R.layout.camera);
cameraView = (CameraView) findViewById(cameras);
cameraView.setPictureFormat(ImageFormat.JPEG);
cameraView.setJpegQuality(2);
cameraView.setCameraListener(new CameraListener() {
#Override
public void onPictureTaken(byte[] jpeg)
{
super.onPictureTaken(jpeg);
String encImage = Base64.encodeToString(jpeg, Base64.DEFAULT); //print and test this string in some B64-to-JPEG decoder/viewer
//Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
//encodeImage(bitmap);
}
});
I am reading a PDF file (as bytes) using this code
byte[] bytes=FileUtils.readFileToByteArray(pdf);
i want to convert this byte[] to base64 string and send it to a client.
I am getting String value as [B#42109d30.
And I want to fill byte[] with the same value [B#42109d30.
I tried to fetch each character of string into a Byte and then assign this Byte value to byte[] , but even then the final value of byte[] changes.
String getbase64 = "[B#42109d30";
String a = String.valueOf((getbase64.charAt(i)));
if (a.equals("#") || a.equals("d") ) {
bytes[i]=0;
} else {
b = Byte.valueOf(""+a);
bt = b;
bytes[i] = bt;
Log.d("bytes", bytes.toString());
}
Please help,thank you
You can convert string to byte[] and vice versa this way :
byte[] bytes = text.getBytes("UTF-8");
String text = new String(bytes, "UTF-8");
I am trying to decode base64 encoded string into bitmap in android.
The encoded string will start with "image/jpeg;base64,"
I use the following method:
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
// also tried using Base64.URL_SAFE , Base64.NO_PADDING
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
Assume that I am calling the above method as
ImageView get_img=(ImageView)fundViewByID(R.id.getImg);
get_img.setImageBitmap(ImageCache.decodeBase64(url))
When I run the code it either throws
--- SkImageDecoder::Factory returned null
or
bad base-64 with IllegalArgumentException
The below is base64 string
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUUEhQUFRQVFBQUFBQXFxQWGBQWFxUWFhQXFRUYHSggGBwlHBQUITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywmHyQsLCwsLC4sLywsLCwsLCwsLCwsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIAJcBTgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EAEUQAAEDAgIGBwQHBwMDBQAAAAEAAhEDIRIxBEFRcYGRBSJhobHB0RMykvAGFBVCUuHxU1RicoKT0jOiskNE4hYjNGNz/8QAGwEAAwEBAQEBAAAAAAAAAAAAAAECAwQFBgf/xAAwEQACAgECBAIKAwEBAQAAAAAAAQIRAxIhBDFBURNhBRQiQoGRobHh8FJx0TLxFf/aAAwDAQACEQMRAD8A5+FSEzArwL64+A3FwpCZgVhiA3FwpCbgUwJC3FwpCaGK8CLDcVCmFO9mrDEWFMThUhPwK/ZosNLM8K8Kf7NTAixaWIwqQnezU9mixUxMKQm4FMKLEKhSE3AqLEBYuFITMKmFAWLhVCZhUhAWLhSEyFITDULhSEcKYUBYuFITIUhA7FQpCZCkICxcKoTYV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ejs/QK6iTclyFOJE533n/kFALe6eEfkmGodojtEeMFBjPPWDHj4p7cg3KdXOsHLY+O6QkEgn3eLZy4LQGkbd8h/eJVNJ1kHhHmhRHqMzwDrbGwkE9/qrZMQCYnVJ8E2uwawN+tZNI0BhjqjfICmUPJMuEk+rQVPo91T3jjjVkCdUrTVY5v3GxtsZ1XuI+bKKJSdM3W8bZna/EbtiNcyZ7k5+jEQSMIdfMTAOwW7goonzqyG6uhwYANhjM3sTfIWyCjpLRhLtc5QIyjWooopWVboChTEAmZdOvXrIAsq0mm5omSQMyDHFRRTNbjg7RVKqTmesMna9x+SoylItn88FFE9EdNiU5OVA0asOLX5tG+PmFppFjx94do1bxN1FFCd8zSq5F1NGc28iM5vw7U2iXRMjfH5BRRPkhtWxrtKc3XLdZMEciJ8UTaw2luuBrv2BRRDlSsWncA6SJPXJ7CDvPZlKaykHZkZTlGedwJ2KlFljyOU9LLnjUY2hYols4nHLqkajkMQvbdtWY6Q7FDyPGRE9iiic8sovYUcMJLcZ9otgADxg8DKSdIbJJbBGy3gYUUXRBKa3RzSuL2YWCm6Ze5picp8BbmhGhBvWa8kDO0W4lUoo0xtqkW5SpOw6DpdYujDYSL75hGKjri1pizYEbZ17lFEpwUY2hQm5yplMc8yScvvdu5NNT8WWTjDe6ZKiiyeWVpdzTwY02W4tuW3i8QPGAVkr1YiYzibWOcSROsc1FEeJJNrsLw4tJ9x7W4RiIBBiD6/oipgOHVE9hy3QVFFnHPKUU3XOi5YYxk0m+QP1IOyJYRkAQQdsCAAgcI90hx2EEH0/VRRdsYI4XNidI0gs6tSROrqlU2hOQvnqHmFaizvematVG0TA7WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k=
The string "data:image/jpeg;base64," is not a valid base64 Encoded string. So it must be removed before decoding.
here you go.
String encodedDataString = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA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LdZMEciJ8UTaw2luuBrv2BRRDlSsWncA6SJPXJ7CDvPZlKaykHZkZTlGedwJ2KlFljyOU9LLnjUY2hYols4nHLqkajkMQvbdtWY6Q7FDyPGRE9iiic8sovYUcMJLcZ9otgADxg8DKSdIbJJbBGy3gYUUXRBKa3RzSuL2YWCm6Ze5picp8BbmhGhBvWa8kDO0W4lUoo0xtqkW5SpOw6DpdYujDYSL75hGKjri1pizYEbZ17lFEpwUY2hQm5yplMc8yScvvdu5NNT8WWTjDe6ZKiiyeWVpdzTwY02W4tuW3i8QPGAVkr1YiYzibWOcSROsc1FEeJJNrsLw4tJ9x7W4RiIBBiD6/oipgOHVE9hy3QVFFnHPKUU3XOi5YYxk0m+QP1IOyJYRkAQQdsCAAgcI90hx2EEH0/VRRdsYI4XNidI0gs6tSROrqlU2hOQvnqHmFaizvematVG0TA7WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k="
encdoedDataString = encodedDataString.replace("data:image/jpeg;base64,","");
byte[] imageAsBytes = Base64.decode(encodedDataString.getBytes(), 0);
imgView.setImageBitmap(BitmapFactory.decodeByteArray(
imageAsBytes, 0, imageAsBytes.length));
You don't need the data:image/jpeg;base64, at the start of the string, that is actually not part of Base64 encoding per se but a Base64 Data URI for a web browser. Just pass the string from /9j/4AAQSkZJRgABAQAAAQABA... onwards and it will decode properly.
I am trying convert an 64-bit encoded string of an image(BLOB) into a Bitmap so that I may display it on an image view. The code for 64-bit decoding works fine and returns the byte-stream , but converting the returned byte-stream to a Bitmap returns null .
Please help.
//This statement works
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE).toString().getBytes(),
Base64.DEFAULT);
//This statement returns null
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
The complete code is as below :-
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
products = jsonObj.getJSONArray(TAG_PRODUCTS);
// looping through All Contacts
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
Double prod_price = Double.valueOf(c.getString(TAG_PRICE));
//The code below works as it returns the byte-array
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE), Base64.DEFAULT);
//The code below returns null even though the byte-stream is known
//from above
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
myProductsList.add(new PRODUCT(c.getString(TAG_PRODUCT_NAME),c.getString(TAG_STORE_NAME), c.getString(TAG_OWNER_NAME), c.getString(TAG_DESCRIPTION), prod_price,bitmap));
}
} catch (JSONException e) {
e.printStackTrace();
}
I would appreciate a solution on this as I have spent a long time searching and am now stuck on this.
Thanks in advance.
Change this code :
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE).toString().getBytes(), Base64.DEFAULT);
To :
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE), Base64.DEFAULT);
There is no need to call toString because you already use getString, and dont call getBytes, just pass the encoded String to decode 1st param.
Thanks to everyone who made suggestions. I found out the fundamental issue was not in the Java programming side but on the database. I had been storing images as blob data-type which tend to small for the images. Changing the datatype to mediumblob solved the issue.
See all 3 edits below
Update: Changed 'read()' to 'readFully()' and got some things working. 'decodeByteArray()' no longer returns null. The jpeg written to the card is actually a full image. The image is still not being drawn to my view. I will keep working and post another question if need be.
I am writing a video streaming client program that takes in images from a DataInputStream.
Because the server calls compressToJpeg() on these images, I figured I could simply take the byte array from my DataInputStream, decode it, and draw the Bitmap from decodeByteArray() to my SurfaceView. Unfortunately, that method only leaves me with decodeByteArray() returning null.
So I added an intermediate step. I created a compressFrame() method which simply takes the byte array from the stream, creates a new YuvImage instance, and uses that to call compressToJpeg(). When run, however, this method hangs on compressToJpeg and closes my client activity. There is no FC popup, and I get no errors in my log.
I have provided the run() of the thread calling the problem method, the method itself, and the log printed before the app closes.
EDIT: I've also added the ReceiverUtils.getFrame() just in case.
#Override
public void run() {
String fromServer = null;
int jpgSize;
byte[] buffer;
byte[] jpgData;
Bitmap image;
ByteArrayOutputStream jpgBaos = null;
while(isItOK && (receiver.textIn != null)){
if(!holder.getSurface().isValid()){
continue;
}
ReceiverUtils.init(holder);
//Receive 'ready' flag from camcorder
fromServer = ReceiverUtils.receiveText(receiver);
while((fromServer != null)){
Log.e("From server: ", fromServer); //logging here because of null pointers when done in Utils
if(fromServer.startsWith("ANDROID_JPG_READY")){
//Obtain size for byte array
jpgSize = Integer.parseInt(fromServer.substring(18));
Log.e("JPG SIZE", String.valueOf(jpgSize));
//Create byte array of size jpgSize
buffer = new byte[jpgSize];
Log.e("BUFFER SIZE", String.valueOf(buffer.length));
//Send flag and receive image data from camcorder
ReceiverUtils.sendText(receiver, "ANDROID_JPG_SEND");
jpgData = ReceiverUtils.receiveData(receiver, buffer);
//Compress jpgData and write result to jpgBaos
jpgBaos = ReceiverUtils.compressFrame(imgRect, jpgData);
//Decode jpgData into Bitmap
image = ReceiverUtils.getFrame(jpgBaos.toByteArray(), jpgSize);
//image = ReceiverUtils.getFrame(jpgData, jpgSize);
//Set buffer and jpgData to null since we aren't using them
buffer = null;
jpgData = null;
//Draw Bitmap to canvas
ReceiverUtils.draw(image, holder, imgRect);
//break; (testing one run-through)
}
//Receive 'ready' flag from camcorder
fromServer = ReceiverUtils.receiveText(receiver);
if(isItOK == false){
break;
}
}
//break; (testing one run-through)
}
}
This is the ReceiverUtils.compressFrame() called in the previous snippet.
/*----------------------------------------
* compressFrame(Rect imgRect, byte[] input): Creates new instance of YuvImage used to compress ARGB
* byte array data to jpeg. Returns ByteArrayOutputStream with data.
*/
public static ByteArrayOutputStream compressFrame(Rect imgRect, byte[] input){
boolean success = false;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(input, ImageFormat.NV21,
imgRect.width(), imgRect.height(), null);
Log.e("HEY", "HEY");
success = yuvImage.compressToJpeg(imgRect, 80, baos);
Log.e("Compression Success:", String.valueOf(success));
return baos;
}
Finally, this is the output I'm getting from my LogCat. (Beware some obviously hastily written debug logs.)
07-03 15:01:19.754: E/From server:(1634): ANDROID_JPG_READY_26907
07-03 15:01:19.754: E/JPG SIZE(1634): 26907
07-03 15:01:19.764: E/BUFFER SIZE(1634): 26907
07-03 15:01:19.764: E/To server:(1634): ANDROID_JPG_SEND
07-03 15:01:19.834: E/jpgIn(1634): Data received successfully.
07-03 15:01:19.834: E/HEY(1634): HEY
07-03 15:01:19.844: D/skia(1634): onFlyCompress
EDIT: ReceiverUtils.getFrame()
/*---------------------------------------
* getFrame(byte[] jpgData, int jpgSize): Decodes a byte array into a Bitmap and
* returns the Bitmap.
*/
public static Bitmap getFrame(byte[] jpgData, int jpgSize){
Bitmap res = null;
res = BitmapFactory.decodeByteArray(jpgData, 0, jpgSize);
Log.e("Decode success", String.valueOf(!(res == null)));
return res;
}
EDIT 2: Code before adding compression
//Compress jpgData and write result to jpgBaos
//jpgBaos = ReceiverUtils.compressFrame(imgRect, jpgData);
//Decode jpgData into Bitmap
//image = ReceiverUtils.getFrame(jpgBaos.toByteArray(), jpgSize);
image = ReceiverUtils.getFrame(jpgData, jpgSize);
EDIT 3: Image after saved to SD Card
The image that Android holds in 'jpgData' in the first code snippet is not the entire image. I can't post it due to being a new user. I added a 'writeToSD' method to test. I assume that the reason it cannot be decoded is because the majority of the image is just blank space, and only a portion of the image is there.