Signature pad sending empty - forms

Signature pad sending empty
I'm using a Signature Pad, and Canvas by SkiaSharp, but when I sending after drawing by user and encode to base 64, on server only shows an empty canvas
async void OnSaveButtonClicked(object sender, EventArgs args)
{
using (SKImage image = SKImage.FromBitmap(saveBitmap))
{
try
{
SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
var bytesImg = data.ToArray();
string imageBase64 = Convert.ToBase64String(bytesImg);
var respuesta = await this.ApiService.PostSignature(
this.url,
this.Id,
imageBase64
);
Method to send on Services...
public async Task PostSignature(
string urlBase,
string folio,
string imageBase64)
{
try
{
var client = new HttpClient();
var response = await client.PostAsync(urlBase,
new StringContent(string.Format(
"idReporte={0}&imgFirma={1}",
folio, imageEncoded),
Encoding.UTF8, "application/x-www-form-urlencoded"));
if (!response.IsSuccessStatusCode)
{
return response.ToString();
}
else
{
var result = await response.Content.ReadAsStringAsync();
return result;
}
}
catch
{
return null;
}
}
END REQUEST...
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert(
"Error",
"Image Is not Send, error: " + ex.Message,
"OK"
);
}
finally
{
completedPaths.Clear();
inProgressPaths.Clear();
UpdateBitmap();
canvasView.InvalidateSurface();
}
The image is empty, is decoded OK and loaded in folder path.

According to your description, you want to get image from signature pad, and converting it into base 64, I do one simple that you can take a look,just cast your imagestream to memorystream
<StackLayout>
<forms:SignaturePadView
x:Name="signaturepad"
BackgroundColor="Black"
HeightRequest="350"
StrokeColor="White"
StrokeWidth="3"
WidthRequest="250" />
<Button
x:Name="save"
Clicked="Save_Clicked"
HeightRequest="50"
Text="save"
WidthRequest="200" />
</StackLayout>
private async void Save_Clicked(object sender, EventArgs e)
{
string base64String;
using (var memoryStream = new MemoryStream())
{
var signature = await signaturepad.GetImageStreamAsync(SignatureImageFormat.Png);
signature.CopyTo(memoryStream);
var byteArray = memoryStream.ToArray();
base64String = Convert.ToBase64String(byteArray);
}
}

Related

xamarin ListView den id alma olayı

After long efforts, I managed to pull data from the api;
When I compile and click on ListVeiw1 screen, I can't get ip or name,
What is the solution?
Thanks.
<ListView SelectionMode="Single" ItemSelected="ListView1_ItemSelected" x:Name="ListView1".....
private async void Button_Clicked(object sender, EventArgs e)
{
List<TodoItem> itemsNew = new List<TodoItem>();
using (var ic = new HttpClient())
{
using (var response = await ic.GetAsync("http://adress.com/api/items"))
{
var content = await response.Content.ReadAsStringAsync();
itemsNew = JsonConvert.DeserializeObject<List<TodoItem>>(content);
ListView1.ItemsSource = itemsNew;
}
}
}
private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
string myname = e.SelectedItem.ToString();
}
you need to cast the SelectedItem to the correct type before you can access its properties
private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (TodoItem)e.SelectedItem;
// now you can access any properties of item
}

Unity occurs a 400 Bad Request when calling a HTTP-Post on an SAP Rest API

I want to call a REST API with an Unity-Script but it occurs me the Error 400 Bad Request. It is maybe because of the http-header. May you can help me. SAP offers a Code Snippet in JAVA which I want to show you first:
DataOutputStream dataOut = null;
BufferedReader in =null;
try {
//API endpoint for API sandbox
String url = "https://sandbox.api.sap.com/mlfs/api/v2/image/scene-text-
recognition";
//Available API Endpoints
//https://mlfproduction-scene-text-
recognition.cfapps.eu10.hana.ondemand.com/api/v2/image
//https://mlfproduction-scene-text-
recognition.cfapps.us10.hana.ondemand.com/api/v2/image
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection)
urlObj.openConnection();
//setting request method
connection.setRequestMethod("POST");
//adding headers
connection.setRequestProperty("content-type","multipart/form-data;
boundary=---011000010111000001101001");
//API Key for API Sandbox
connection.setRequestProperty("APIKey","----api-Key---");
//Available Security Schemes for productive API Endpoints
//OAuth 2.0
connection.setDoInput(true);
//sending POST request
connection.setDoOutput(true);
dataOut = new DataOutputStream(connection.getOutputStream());
dataOut.writeBytes("-----011000010111000001101001\r\nContent-
Disposition: form-data; name=\"files\"; filename=\"<file_name>\"\r\nContent-Type: <file_type>\r\n\r\n<file_contents>\r\n-----011000010111000001101001--");
dataOut.flush();
int responseCode = connection.getResponseCode();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
//printing response
System.out.println(response.toString());
} catch (Exception e) {
//do something with exception
e.printStackTrace();
} finally {
try {
if(dataOut != null) {
dataOut.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
//do something with exception
e.printStackTrace();
}
}
My UnityCode looks something like this:
public void ExecutePost()
{
Debug.Log("execute started");
byte[] img =
File.ReadAllBytes(#"C:\Users\InnovationLab\Documents\ECENTA\ECENTA
FSE\Bild1.jpg");
string url = "https://sandbox.api.sap.com/mlfs/api/v2/image/scene-
text-recognition";
coroutine = Post(url, img);
StartCoroutine(coroutine);
}
public IEnumerator Post(string url,byte[] image)
{
WWWForm form = new WWWForm();
form.AddBinaryData("imageField", image, "HoloImg");
var headers = new Dictionary<string, string> {
{"content-type", "multipart/form-data; boundary=---011000010111000001101001" },
{"APIKey", "---here I implemented the key---" }
};
WWW www = new WWW(url, image, headers);
yield return www;
if (www.error != null && www.error != "")
{ // on error, show information and return
Debug.Log("Network Error occured: " + www.error);
yield break;
}
while (!www.isDone)
{
Debug.Log(www.text);
}
}
}
So my question is, how to change the unity code so that it works?
I fixed it by using MultipartFormSections. The problem was that the api expected form-data not a binary Array.
public IEnumerator Upload(string url, byte[] img)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
MultipartFormFileSection myFormFile = new MultipartFormFileSection("files", img,
"Bild1.jpg", "multipart/form-data");
formData.Add(myFormFile);
Debug.Log(formData.ToString());
UnityWebRequest www = UnityWebRequest.Post(url, formData);
www.SetRequestHeader("APIKey", "<api-key>");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
throw new Exception(www.downloadHandler.text ?? www.error);
}
else
{
Debug.Log("Done!!!!!");
}
Debug.Log(www.downloadHandler.text);
var ResultObject = JsonUtility.FromJson<TextPrediction>(www.downloadHandler.text);
foreach (var result in ResultObject.texts)
{
}
}

How to upload empty file with Retrofit 2?

I successfully set up an uploading images to my REST API via retrofit with this code:
File imageFile = ImagePicker.getFileFromResult(this, resultCode, data);
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
MultipartBody.Part body =
MultipartBody.Part.createFormData("userpic", imageFile.getName(), requestFile);
RetrofitClient.compositeSubscription.add(RetrofitClient.getService().updateProfileUserpic("Token " + RevolutionApp.getInstance().getUserToken(), body).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Profile>() {
#Override
public void onCompleted() { }
#Override
public void onError(Throwable e) { }
#Override
public void onNext(Profile profile) {
mProfileFragment.fetchProfileData();
}
}));
Now I need to send empty file (null) to my my API. How to implement it?
MultipartBody.Part.createFormData("userpic", imageFile.getName(), requestFile)
does not work
i don't Know this is the right way to do this,i tried like this.its working
MultipartBody.Part fileToUpload = null;
if (realPath != null) {
try {
File file = new File(realPath);
if (file.exists()) {
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
fileToUpload = MultipartBody.Part.createFormData("attachment", file.getName(), requestBody);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}else{
RequestBody attachmentEmpty = RequestBody.create(MediaType.parse("text/plain"), "");
fileToUpload = MultipartBody.Part.createFormData("attachment", "", attachmentEmpty);
}
Note: File Name should not be null ,in place of null use empty("").
Here is final solution:
API interface method to update model:
public void updateProfileData(ProfilePost profile, final OnProfileUpdatedListener listener) {
Observable<Profile> observable;
if (profile.getUserpicPart() != null) {
observable = RetrofitClient.getService().updateProfileData(profile.asHashMap(), profile.getUserpicPart());
} else {
observable = RetrofitClient.getService().updateProfileData(profile.asHashMap());
}
RetrofitClient.compositeSubscription.add(observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Profile>() {
...
}));
}
Retrofit method signature:
#PUT("/api/v1/profile/")
#Multipart
Observable<Profile> updateProfileData(#Header("Authorization") String authorization, #PartMap() Map<String, RequestBody> partMap, #Part MultipartBody.Part image);
#PUT("/api/v1/profile/")
#Multipart
Observable<Profile> updateProfileData(#Header("Authorization") String authorization, #PartMap() Map<String, RequestBody> partMap);
and model to post:
public class ProfilePost {
MultipartBody.Part userpicPart;
public HashMap<String, RequestBody> asHashMap() {
HashMap<String, RequestBody> result = new HashMap<>();
result.put("first_name", RequestBody.create(MediaType.parse("text/plain"), this.firstName));
result.put("last_name", RequestBody.create(MediaType.parse("text/plain"), this.lastName));
result.put("email", RequestBody.create(MediaType.parse("text/plain"), this.email));
}
public void getUserpicPart() {
if (this.userpicFile == null) {
return null;
}
return MultipartBody.Part.createFormData("userpic", this.userpicFile.getName(), requestFile);
}
}

Bluemix: Response of Text-to-speech is not playing audio using below code

Can you help met solve this issue, why the response of Text-to-speech is not playing audio using below code?
index.jsp
$.fn.PlayBtnClicked = function() {
var txt=$(this).data("text");
$.ajax({
type: "GET",
url: 'demo',async: false,
data: { text: txt} ,
success: function(response) {
$(".result").show();
var audio = document.getElementById("myAudio");
audio.src = response;
audio.type = "type/ogg";
audio.play();
}
});
};
<audio id="myAudio" autoplay preload="auto" autobuffer controls class="audio"></audio>
DemoServlet.java
protected void doGet(final HttpServletRequest req,
final HttpServletResponse resp) throws ServletException,
IOException {
String serviceName = "text_to_speech";
// If running locally complete the variables below with the information
// in VCAP_SERVICES
String baseURL = "https://stream.watsonplatform.net/text-to-speech/api";
String username = "USERNAME";
String password = "PASSWORD";
if (req.getParameter("text") == null) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
} else {
boolean download = false;
if (req.getParameter("download") != null
&& req.getParameter("download").equalsIgnoreCase("true")) {
download = true;
}
req.setCharacterEncoding("UTF-8");
try {
String text=req.getParameter("text");
text=URLEncoder.encode(text, "UTF-8");
String voice="&voice=en-US_AllisonVoice";
String queryStr=text+voice;
String url = baseURL + "/v1/synthesize";
if (queryStr != null) {
url += "?text=" + queryStr;
}
URI uri = new URI(url).normalize();
Request newReq = Request.Get(uri);
newReq.addHeader("Accept", "audio/ogg; codecs=opus");
Executor executor = Executor.newInstance().auth(username,
password);
Response response = executor.execute(newReq);
if (download) {
resp.setHeader("content-disposition",
"attachment; filename=transcript.ogg");
}
ServletOutputStream servletOutputStream = resp
.getOutputStream();
response.returnResponse().getEntity()
.writeTo(servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();
} catch (Exception e) {
// Log something and return an error message
logger.log(Level.SEVERE, "got error: " + e.getMessage(), e);
resp.setStatus(HttpStatus.SC_BAD_GATEWAY);
}
}
}
Here both the java moethod runs successfully in response it returns some binary kind of data to jsp , ajax response.
But still i could not play the audio. Could you please help me to solve out this issue?
The problem is that you are setting an html response to the src property of an <audio> tag.
You need to do something like:
$.fn.PlayBtnClicked = function() {
var text = $(this).data("text");
var audio = document.getElementById("myAudio");
audio.setAttribute('src','/synthesize?text=' + encodeURIComponent(text));
});
More info
audio tag
IBM Watson Text to Speech

wicket response trimmed at the end

I have a trouble with output data in response.
#Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
ResourceResponse response = new ResourceResponse();
response.setContentDisposition(ContentDisposition.INLINE);
response.disableCaching();
StringBuilder stringBuilder = new StringBuilder(DEFAULT_CONTENT_TYPE);
stringBuilder.append(";").append(charset == null ? DEFAULT_CHARSET : charset);
response.setContentType(stringBuilder.toString());
response.setLastModified(Time.now());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
fillOutputStream(outputStream);
} catch (IOException e) {
logger.error("Error when try to fill data for html report", e);
}
String message = null;
try {
message = outputStream.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn("Unknown encoding");
message = outputStream.toString();
}
final CharSequence data = message;
response.setContentLength(data.length());
response.setWriteCallback(new WriteCallback() {
#Override
public void writeData(Attributes attributes) {
attributes.getResponse().write(data);
}
});
configureResponse(response, attributes);
return response;
}
Here data is html page which has been generated in fillOutputStream() method and transform to CharSequence.
I've logged the data and it have correct content which I expect but in the result I have trimmed at the end page.
The length of String is not necessarily equal to its byte count.
Use a CountingOutputStream instead (e.g. from guava).