How to load image using Tesseract LoadTiffFromMemory from WinAppDriver GetScreenshot method? - tesseract

I'm using the following code to capture a screenshot from a WinAppDriver session and then passing it to a Tesseract Pix class for OCR to navigate links in a table that the WinAppDriver session (Win32 app) doesn't recognize. Note session is defined using WinAppDriver's WindowsDriver<WindowsElement>
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium;
using System.Configuration;
using Tesseract;
[TestMethod]
public void ImageTest()
{
String ImagePath = #"c:\temp\Image.png";
var screen = session.GetScreenshot();
screen.SaveAsFile(ImagePath, ScreenshotImageFormat.Png);
Pix ImagefromFile = Pix.LoadFromFile(ImagePath); // Works
var screenmemory = session.GetScreenshot().AsByteArray;
Pix ImagefromMemory = Pix.LoadTiffFromMemory(screenmemory); // Fails with "IOExeception: Failed to load image from memory
}
As is stated in the comments, I can save the file locally and then access it. However, I was hoping to this via LoadTiffFromMemory instead. The test fails with exception System.IO.IOException: Failed to load image from memory

The screenshot is most likely not of TIFF format. Can you try?
Pix ImagefromMemory = Pix.LoadFromMemory(screenmemory);
or
MemoryStream memStream = new MemoryStream();
screen.Save(memStream, ImageFormat.Tiff);
Pix ImagefromMemory = Pix.LoadTiffFromMemory(memStream.ToArray());

Related

MediaRecorder No Metadata on Download

I'm using MediaRecorder (along with the Web Audio API) to record and process audio and download the blob that it generates. The recording and downloading work great, but there is no metadata when the file is downloaded (length, sample rate, channels, etc.)
I'm using this to create the blob, and I've also tried the mimetype with no luck:
const blob = new Blob(chunks, {
'type' : 'audio/wav'
});
chunks = [];
const audioURL = window.URL.createObjectURL(blob);
audio.src = audioURL;
console.log("recorder stopped");
var new_file = document.getElementById('downloadblob').src
var download_link = document.getElementById("download_link");
download_link.href = new_file;
var name = generateFileName();
download_link.download = name;
How could I ensure the length of the recording, sample rate, and other metadata are included in the download?
I don't know of any browser which allows you to record something as audio/wav. You can get the mimeType of your recording from the instance of the MediaRecorder.
const blob = new Blob(chunks, {
'type': mediaRecorder.mimeType
});
Please note that the length will only be correct if you omit the timeslice parameter when calling mediaRecorder.start(). Otherwise the browser doesn't know the final length of the file when generating the metadata.

How to upload image file in Xamarin forms using rest api . Have issues with large images

I tried and I face a problem when uploading images greater than 570 kb. This issue is in Xamarin Forms for android and PHP rest api. I tested the rest api separately and I have no issues there uploading 2mb files using postman.
Tried various ways also by giving some delay. I am capturing image using cross.media plugin. Then navigating to another page to upload. I wait for sometime and then click the button to upload. I am not able ascertain where the issue is.
System.IO.Stream fileStream = System.IO.File.Open(file, FileMode.Open);
byte[] data = ReadFully(fileStream);
fileStream.Close();
MultipartFormDataContent multi = new MultipartFormDataContent();
ByteArrayContent imageStream = new ByteArrayContent(data);
StringContent SequenceID = new StringContent(osequence);
imageStream.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
imageStream.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = osequence, // "screenshot.jpg", // generate this and send
Name = "avatar",
};
multi.Add(imageStream);
alertLabel.Text = "Uploading Now";
var response = await App.client.PostAsync(url, multi);
string responsestr = response.Content.ReadAsStringAsync().Result;
var retresponse = new retResponse();
bool uploadSuccess = false;
I have made the rest api to send response on error and showing the same in an alert box as below
if (responsestr != "") alertLabel.Text = responsestr.ToString();
else alertLabel.Text = alertLabel.Text + " After Upload Command ";
}
catch (Exception e)
{
}
} // private void upload(MediaFile mediaFile)
The error I get is no file sent
Please ignore the above question. The problem was solved by changing the PHP.ini max upload file size. The file size in the device was around 500 - 700 KB . don't know how it comes to 1.99 and 2 mb when uploaded. Increasing max file size solved it.

How create channel to read a BLOB and write file system file

I am trying to create a image by fetching blob from MySQL DB ...
Here goes the code....
var dbConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/dbname','username','');
var query_code = "Select blob from tblname where id =59"; //
var result = dbConn.executeCachedQuery(query_code);
dbConn.close();
if (result.next())
{
var image=FileUtil.encode(result.getBytes(1));
$gc('image1', image);
}
Could anyone help me out to achieve this...
Did You receive any error with this code if so post it.
Im not sure with Mirth connect tool But as far as I know, Retrieving the BLOB image from the DB directly with any IDE/Tools is always possible but difficult with 2 ways
By encoding to BASE-64 charcter
By specifying the entire location saving it locally.
And try removing the dbconn.close(); do not use it or use it after if() statement. Because it will throw an error like "no statements were executed after dbconn.close();

How to connect asp.net web server to iPhone

I realize that there are other posts asking similar questions, but do not quite answer the question I have. I apologize if this is not enough info, formatting is off, or whatnot. Its my first time posting.
Basically my group is doing an iPhone app that needs to interface with our sql server database. But because they can't talk directly with eachother we are implementing an asp.net medium that will accept XML and apparently will need to output JSOD. Currently I have a .asmx written with "working" code (test string inside code). Right now I am just trying to accept the XML from the iPhone. I feel like we are missing something either on his end (iPhone) or my end (.asmx). Hopefully you bright minds out there can fill me in because with our combined research....i feel theres something missing. Maybe im wrong and Ive we've got our stuff together which would be awesome.
apparently this is all he needs (according to his research) on his end in order to connect and transfer the information. obviously its not .asmx but it shouldn't matter the extension right?
NSURL *url = [NSURL URLWithString:#"http://myurl.com/RequestHandler.ashx"];
Here is the code that I have with my .asmx (note the function is where the meat is at)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;
namespace Outbreak_Mobile
{
[WebService(Namespace = "http://OutbreakMobile.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod (Description="To intake an xml package from iPhone and store it")]
public string iPhoneLocation(string xml)
{
//test string to output to screen
string location = null;
//test string of xml to be parsed
//will be converting next line to parse input xml
//string xml = #"<Player>
// <Latitude>42.13245465</Latitude>
// <Longitude>11.11111111</Longitude>
// </Player>";
//line to parse document
//xdocument doc = xdocument.parse(incoming xml)
XDocument doc = XDocument.Parse(xml);
//grabs the information inside <player> tags
XElement loc = doc.Elements("Player").SingleOrDefault();
if (loc != null)
{
var lat = (double)loc.Element("Latitude");
var lng = (double)loc.Element("Longitude");
location = "Lat: " + lat + '\n' + "Long: " + lng;
}
//this is what puts out to the screen
return location;
}
}
}
currently the output of the string 'location' was just for functionality testing and will change when I know that I am receiving the packages correctly. What are we missing in order to connect with one another?
Thank you all in advance!
You created a NSURL object. Now you need to create a NSURLConnection to download the data from that URL.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
And to transfer the NSData object you got from the connection just use NSString -initWithData:encoding: method.

ADO.NET Data Services - Uploading files

I am trying to write REST web service through which our clients can upload a file on our file server. IS there an example or any useful links which I can refer for any guidance?
I haven't seen many examples of POST operation using ADO.NET data services available.
I've uploaded a file to ADO.NET dataservices using POST although I'm not sure whether it's the recommended approach. The way I went about it is:
On the dataservice I've implemented a service operation called UploadFile (using the WebInvoke attribute so that it caters for POST calls):
[WebInvoke]
public void UploadFile()
{
var request = HttpContext.Current.Request;
for (int i = 0; i < request.Files.Count; i++)
{
var file = request.Files[i];
var inputValues = new byte[file.ContentLength];
using (var requestStream = file.InputStream)
{
requestStream.Read(inputValues, 0, file.ContentLength);
}
File.WriteAllBytes(#"c:\temp\" + file.FileName, inputValues);
}
}
Then on the client side I call the data service using:
var urlString = "http://localhost/TestDataServicePost/CustomDataService.svc/UploadFile";
var webClient = new WebClient();
webClient.UploadFile(urlString, "POST", #"C:\temp\test.txt");
This uses a WebClient to upload the file which places the file data in the HttpRequest.Files collection and sets the content type. If you would prefer to send the contents of the file yourself (eg from an Asp FileUpload control) rather than the webClient reading a file using a path to the file, you can use a WebRequest similar to the way that it's done in this post. Although instead of using
FileStream fileStream = new FileStream(uploadfile,
FileMode.Open, FileAccess.Read);
you could use a byte array that you pass in.
I hope this helps.
I'm not 100% sure how to do this directly to a file server per se, but ADO.Net Data Services definitely support something similar to a database. The code below is how a similar goal of putting a file into a database has been accomplished. Not sure how much that will help, but
var myDocumentRepositoryUri = new Uri("uri here");
var dataContext = new FileRepositoryEntities(myDocumentRepositoryUri);
var myFile = new FileItem();
myfile.Filename = "upload.dat";
myFile.Data = new byte[1000]; // or put whatever file data you want to here
dataContext.AddToFileItem(myFile);
dataContext.SaveChanges();
Note: this code is also using Entity Framework to create a FileItem (representation of a database table as an object) and to save that data.