Write non ASCII char with PhoneGap Javascript Image - javascript

Here is what I'm doing :
In javascript I ask for an image with AJAX.
The server php execute the following code :
readfile($pathfile);
Then the javascript receive the data, and with Phonegap I try to write it to a file :
var writer = new FileWriter();
writer.fileName = "./../Documents/" + nameFile;
writer.write(dataReceived);
In fact, it works with ASCII chars, but the function "write" fail with the content of the image received (png format). So I tried to encode the transferred data in Base64, in Hexadécimal and it works. But I don't want to store encoded data because I want to use it later in "img" tags (html tag). And make something like :
<img src='./../Documents/createdFile.png' />
Do you have any idea on how to write any characters in Phonegap to a file? Or what I'm doing wrong with the FileWriter of PhoneGap ? I know the way to do something like src='Image.php', but I have to store files on the device.
I've got the version 0.9x of Phonegap (included with Dreamweaver). Thanks in advance.

I finally found the explication. To begin I want to say that I just learned javascript ;) .So I asked the question to "macdonst" on github and he answered me :
"Sadly, you will be unable to write binary data with the FileWriter. JavaScript can't handle binary data very well and we can't pass binary data from the JavaScript side to the Native side so it is a limitation we have to live with.
However, once you've written you've gotten your base64 image data you can display it like this:
<img src="data:image/gif;base64,{this is where your base64 data goes}" />
"
Thanks to "macdonst", I posted his answer because I hope it will help beginners like me.

Related

Create PDF from html in react-js

I need to create a PDF from HTML inside a react-js app.
Many packages I have found prompt a download button in the browser ( like jsPDF ), but I actually need the PDF as a binary string. I need this string to be send to a private API that stores this PDF ( binary string ) in S3 as a PDF file. This private API call already exists, and I can not change anything from this code.
I am struggeling to understand why this is so hard. How would you go about converting HTML to PDF binary string? Thanks for any suggestions, packages, ... It can be javscript, if I can implement it inside my reactJS app.
Bonus points if the solution can accept HTML tags, since the input is done inside an WYSIWYG editor.
This server side solution works with any HTML framework.
https://github.com/PDFTron/web-to-pdf
This is from the company I work for, but is AGPL-3.0 so you should be able to use no problem.

Array of Bytes to an URL encoded in JavaScript for ImageJ

I need too convert my array of bytes (an image) to an URL encoded so it can be interpreted by my distant server then after treatment, will re-encode it and send back to the user.
This is for setting up a server (cloud) on which the hosted model of deep Learning will get the image treated by the user (using FiJi (imageJ) at laboratories). it will remove noises (Poisson and Gaussian) and user will receive his stack directly from server, avoiding them to install heavy stuff on their computer.
importClass(Packages.ij.IJ);
importClass(java.awt.image.BufferedImage);
importClass(java.io.ByteArrayOutputStream);
importClass(java.io.File);
importClass(javax.imageio.ImageIO);
importClass(java.util.Base64);
importClass(java.lang.StringBuffer);
imp = IJ.getImage();
var buffered = imp.getBufferedImage();
imp.draw();
imp.getCurrentSlice();
baos = new ByteArrayOutputStream();
ImageIO.write( buffered, "jpg", baos );
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
IJ.log(imageInByte[18751]);
So at the moment (i'm kind of new with JS So sorry about the "IJ.log(imageInByte[])" line because it was the only way to see if my bytes encoding was working correcly.
I'm now struggling to find a way to have this "imageInByte" converted to an URL so my next step will be to setup the server so he can get this URL, decode it, having the 8-bit image, do his job to remove noises on the picture (microscopes stacks) , recode it into bytes then URL and resend it to the Customer via ImageJ Plugin and enjoy the clean image without installing the whole model on localhost ...
I have no idea how to proceed since everything I found are pure Java or Web programming and i'm writing a FiJi script
(excuse my lvl I'm not enough familiar for passing from a language to another since i've found solutions but for unsimilar problems and for PLSQL or C++)
I'd usually go with "btoa" then "atob" but my fiji console return me "not defined" i've imported many buffering class but it wont be "defined" for it...
Edit : alternate solution would be to get the same code (i don't know how to import these java libraries as Jython) so I can have same result (bytes array of the image) and making it an encoded URL seems much more easy in Python so i'll be open to any help about translating this into python too !
So after realizing things are getting harder with JS, I swapped my code to python (was easy thanks to Jython env)
I went further in my problem so I'll post in python
answer : it's impossible to convert a byte of array to an URL encoded or with a toString in JS.
thanks again for your precious help.

Convert binary / unicode string to image

I am using a library to recieve attachments (image) from salesforce using OAuth and a proxy. Without the library (and the proxy) I am able to the same using XHR, but I have to use the library because I need the proxy.
In chrome debugger I can see image is downloaded fine, but I can't get it to work in my code. Data looks like:
So far methods I have tried:
btoa(unescape(encodeURIComponent(file.body)));- produces a base64 that does not work. decoding it using online tools gives me back the same string.
escape(file.body) - using this as base64 also does not work.
Converting to a blob.
var blob = new Blob([file.body], {type : "image/png"});
urlCreator.createObjectURL(blob);
The url it points to displays nothing, and if I remove {type : "image/png"} the url points to a page displaying same binary string.
Long story short this looks like someone read the image with readAsText() which mangles ("interprets") binary into utf8, instead of utf16. You need to get the server to return the data in arraybuffer or blob format ideally, or even base64, these preserve binary.
Long version with snippet. (those question marks symbols in the snippet show the lost information, compare it to the binary where they are not question marks in utf16):
https://stackoverflow.com/a/56523118/2962797
I would create an <img /> element, equal its src attribute to the image data you received. Now you have the image.
If you want to extract it or convert it to PNG, I would apply this another answer process.

how to get data from a base64 encoding of a .pptx file in javascript

I get data from a server of the .pptx file in base64 encoding now i would like to get the text that is present inside the base64 data.
Is there any third party java script library to do this especially scanning in base64 code rather than taking the file path and i would like insert these strings into a power point using office js.
Client side would be preferred.
thanks
Seems that what you need is a JavaScript decoder for base64 files, there are many projects in Github Doing this, for instance here is one https://github.com/mathiasbynens/base64.
That said, I am not sure about your scenario, and what types of files are been base64-encoded. Base64 at the end of the day is a text "representation" of usually a binary file, like an image or a compressed zip file. I wonder if once you decode it you will get what you expect. And if you are expecting text, i wonder why your service is even encoding it like this.
Anyways... once you have whatever text you want to insert, you can use the setSelectedDataAsync method of our Office.js in PPT to write it in your presentation's active selection. https://dev.office.com/reference/add-ins/shared/document.setselecteddataasync

Encode/Decode a picture in Hex

A client (a job board) has asked me to do the following :
Create a form, gather informations and create an xml file containing all those informations everytime a user fills out the form, easy enough.
So the client sent me an xml model and within this xml file there is an encoded picture and an encoded CV, both are encoded (it seems in HEX), and i cannot understand how to decode (or even encode under the same format for that matter) the piece of HEX.
Here are pieces of the xml fiel, I cannot post it entirely, you'll surely understand why.
<photo>
FFD8FFE10A1845786966000049492A000800000008000F010200040000004854430010010200150000006E0000001201030001000000010[.............]EF6A57F5A8E41EE594D62075FF8F77CFF00B1FF00D7A7C17D13B7FA99157FE0269C60E22E4D4DAB38A09E24788F5FF80D5B5B5FEE9ACE32E518AB6DFEDAD1F653FC2D57700FB23FFB1F9D5DB64289B4
</photo>
<cv>
255044462D312E340D0A25C8C9CACB0D0A372030206F626A0D0A3C3C2F54797065202F506167652F506172656E742033203020522F436F6E74656E74.......
</cv>
<extensionCv>.pdf</extensionCv>
And just to make it harder here are several points to take into consideration :
This file is to be used to import informations into a software which has been developed especially for this company, I do not have access to it, and cannot get in touch with the company that designed it. The xml file has been created by this software as an export of a candidat file.
I cannot encode it in base64 (it'd be too easy), it needs to be the same encoding.
I need to be able to encode it in either js, or php (once im sure the software can decode if, i'll only need to encode, I won't need to decode anything).
Thank you.
You can use bin2hex PHP function to convert binary data into HEX string. Please check PHP documentation where you can find an example of bin2hex with reading a binary file

Categories

Resources