I currently have a python socket server that captures from a webcam using opencv. I have converted the captured image using image.tostring();
I am trying to send this image to an android application and have it open in a web view. I can get the text string to open, but cannot make it back into an image.
This is the only way that I can get anything to display in the web view :
webv.loadData(html, "text/html", "utf-8");
The data is transferring to the android device, so I know that the socket is working fine, I just cannot figure out the translation.
Any suggestions?
EDIT based on comments below:
Ok. So I now converted the string to base64 in python, before sending it.
I tried using bitmapfactory to add the image to an ImageView using:
byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
but I get the error
SkImageDecoder:: Factory returned null
I also tried a webview with the following, which gives me a broken image icon
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "utf-8");
These give me a blank webview
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "iamge/jpeg", "base64");
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "base64");
webv.loadData(result, "image/jpeg","base64");
It seems that the problem is in my python code, as the base64 string exported to a text file cannot be reformed into an image, it "contains errors"
applicable code fragments:
img = cv.QueryFrame(cam)
imgstr = base64.b64encode(img.tostring())
conn = s.accept()
conn.send(imgstr)
Related
I am trying open a base64 string in WebView which is actually a doc/docx.
I have tried many ways, I don't wanna end up having paid library.
Here is my code. Please suggest...!
String lBase64Str =
Base64.encodeToString(lBytes, Base64.DEFAULT);
URL lUrl = new URL(lBase64Str);
lWebView.loadUrl(lUrl.toString());
lWebView.loadData(lBase64Str,"text/html","base64");
I want to display video in android app.
The link of video is blob link so that i think i need use webview to display it.
How to implement it?
WebView support loading HTML data from string. So following code is possible.
String videoBlobLinkUrl = "data:video/mp4;base64,[video base64-encoded data ..]";
String videoBlobMime = "video/mp4";
String videoHtml = "<html><body><video><source type=\""+videoBlobMime+"\" src=\""+ videoBlobLinkUrl +"\"></video></body></html>";
webview.loadData(videoHtml, "text/html", null);
I have a very simple WebView that needs to display text as HTML. I retrieve data from a server that is in a byte array. I need to display that byte array as readable text.
var wv = FindViewById<WebView>(Resource.Id.webview);
var container = AutoFac.Container;
_routinesService = container.Resolve<IRoutinesService>();
byte[] documentHtml = _routinesService.GetFragment(documentId);
string mimeType = "text/html";
string encoding = "utf-8";
var html = System.Text.Encoding.UTF8.GetString(documentHtml);
wv.LoadDataWithBaseURL("", html, mimeType, encoding, "");
I try to convert the byte array to a String, and the variable html becomes \"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg==\"
This looks like Base64 to me, and when I use an online converter (https://www.base64decode.org/), it does convert to the correct text. But the output on the screen in the WebView just looks like "U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg=="
Any ideas what I might be missing?
Just for more information, this is in Xamarin.Android so the code is C# instead of Java (but there is not much difference).
Have you tried javascript
atob()
method that decodes a string of data which has been encoded using base-64 encoding before posting it to webview.
I am trying to download image using an url like:
url --> http://www.example.com/path/to/image-ğüçöşı.jpg
InputStream input = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
However in the first line app crashes. Because it has a character like "ı" or "ç". If url doesn't has those character it doesn't crash and works fine.
I could almost say i tried most of the solutions like utf8 encoding and etc, including giving "UTF8" params to HttpClient.
It would be appreciated very much if you could help me. I am looking for any solution that doesn't slow down the code very much.
Thank you
Encode your url or only image name with this code
String query = URLEncoder.encode("strangeChars", "utf-8");
I want to add a image that i take from phones camera
and add it to a html that is build dynamically from user input.
so i cant get the html file from assets.
all the question/answers i found are for local images and html from assets.
Android - local image in webview
Android Development: Using Image From Assets In A WebView's HTML
any suggestions?
Thanks
One of the answers in the links you showed, suggests to use Base64 embedded image:
(https://stackoverflow.com/a/9000691/1271435)
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxx = base64 encoded string of images bytes
This sounds like a good idea, and one way to do it is something like this:
After taking the image with camera you should get back an imageUri of the photo.
then:
1.Get the input stream of that image:
InputStream stream = getContentResolver().openInputStream(imageUri);
2.Fill in the stream into a ByteArrayOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fillStream(stream, baos); // dummy method, provide implementation
3.Convert the byte array to Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
Then you can use the base64 string as the data for your image in HTML.