Digital Persona U.R.U 4500 Issue with ZKSoftware - c#

I am using digital persona URU 4500 with C# SDK. evrything is fine, Image received function is working properly.
But image capture event is not working with ZKFinger SDK for C#
private void axZKFPEngX1_OnImageReceived(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnImageReceivedEvent e)
{
}
Image received function is working properly but the below image capture function is not working.
private void axZKFPEngX1_OnCapture(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnCaptureEvent e)
{
MessageBox.Show("ON Capture");
}
What i further need to do this, i heard i need Biokey.lic file. but after adding that to System32 its not working.
The same code works fine with URU4000 Scanner.
http://eu.zksoftware.com/product.do?id=156

I ended up using digital persona own SDK to enroll and to verify.
Here is the code to save template data to DB if anyone having issues.
switch (Enroller.TemplateStatus)
{
case DPFP.Processing.Enrollment.Status.Ready: // report success and stop capturing
OnTemplate(Enroller.Template);
MemoryStream fingerprintData = new MemoryStream();
Enroller.Template.Serialize(fingerprintData);
fingerprintData.Position = 0;
BinaryReader br = new BinaryReader(fingerprintData);
Byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);
Enroller.Template.Serialize(ref bytes);
string basestring = Convert.ToBase64String(bytes);
string fingerprint_ = basestring;
Stop();
break;
}

Related

Encode MemoryStream directly to MP3 using NAudio

I'm trying to use one of the methods below of the NAudio nuget package:
MediaFoundationEncoder.EncodeToMp3(new RawSourceWaveStream(_audioStream, _audioWriter.WaveFormat), "Test.mp3", 320000);
using (var reader = new WaveFileReader(_audioStream))
{
MediaFoundationEncoder.EncodeToMp3(reader, "Test.mp3", 320000);
}
This should encode the wav stream directly to mp3. Well it does, but the result only consists of noise.
When I save the same MemoryStream(WaveFileWriter) with
File.WriteAllBytes("Test.wav", _audioStream.ToArray());
MediaFoundationEncoder.EncodeToMp3(new WaveFileReader("Test.wav"), "Test.mp3", 320000);
the created wav file is fine and I can use this wav file to encode it to mp3. But I want to avoid saving the wav file first.
Is there a way to encode the stream directly to mp3 without only getting noise? I have no clue why the one method works and the other isn't. Google didn't help me as well. So maybe you guys have any idea.
Thanks for your effort.
Further infos:
.NET Core 3.0 Windows Application
NAudio Nuget Package (Version: 1.9.0)
Complete class:
using NAudio.MediaFoundation;
using NAudio.Wave;
using System;
using System.IO;
namespace Recorder.NAudio
{
public class Recorder
{
private WasapiLoopbackCapture _audioCapture;
private WaveFileWriter _audioWriter;
private MemoryStream _audioStream;
public bool IsRecording { get; private set; }
public Recorder()
{
MediaFoundationApi.Startup();
}
public void Init()
{
_audioCapture = new WasapiLoopbackCapture();
_audioStream = new MemoryStream(1024);
_audioWriter = new WaveFileWriter(_audioStream, new WaveFormat(44100, 24, 2));
_audioCapture.DataAvailable += DataReceived;
_audioCapture.RecordingStopped += RecordingStopped;
}
private void RecordingStopped(object sender, StoppedEventArgs e)
{
_audioCapture.DataAvailable -= DataReceived;
_audioCapture.RecordingStopped -= RecordingStopped;
_audioCapture.Dispose();
_audioCapture = null;
_audioWriter.Flush();
_audioStream.Flush();
_audioStream.Position = 0;
//This works the mp3 file is fine, but I want to avoid the workaround of first saving a wav file to my hard drive
File.WriteAllBytes("Test.wav", _audioStream.ToArray());
MediaFoundationEncoder.EncodeToMp3(new WaveFileReader("Test.wav"), "Test.mp3", 320000);
//Try 1: This doesn't work the mp3 file consists only of noise
MediaFoundationEncoder.EncodeToMp3(new RawSourceWaveStream(_audioStream, _audioWriter.WaveFormat), "Test.mp3", 320000);
//Try 2: This doesn't work the mp3 file consists only of noise
using (var reader = new WaveFileReader(_audioStream))
{
MediaFoundationEncoder.EncodeToMp3(reader, "Test.mp3", 320000);
}
_audioWriter.Close();
_audioWriter.Dispose();
_audioWriter = null;
_audioStream.Close();
_audioStream.Dispose();
_audioStream = null;
GC.Collect();
}
private void DataReceived(object sender, WaveInEventArgs e)
{
_audioWriter.Write(e.Buffer, 0, e.BytesRecorded);
}
public void Start()
{
Init();
_audioCapture.StartRecording();
IsRecording = true;
}
public void Stop()
{
_audioCapture.StopRecording();
IsRecording = false;
}
}
}
I found a solution. When I change constructor of the WaveFileWriter to this:
_audioWriter = new WaveFileWriter(_audioStream, _audioCapture.WaveFormat);
And then change the audio settings of my device in the windows sound settings dialog to:
2 Channels, 24 Bit, 44100Hz instead of 2 Channels, 24 Bit, 96000Hz
it works for some reason...

NAudio WasapiLoopbackCapture intermittent sound

Part 1
I have some NAudio related code
private void InitAudioOut(DateTime dtNow)
{
_pathOut = string.Format(BaseDirectory + #"\({0:HH-mm-ss dd-MM-yyyy} OUT).wav", dtNow);
_waveOut = new WasapiLoopbackCapture();
_waveOut.DataAvailable += WaveOutDataAvailable;
_waveOut.RecordingStopped += WaveOutRecordStopped;
_waveOutFileStream = new WaveFileWriter(_pathOut, _waveOut.WaveFormat);
_waveOut.StartRecording();
}
With this initialization of the sound recording process I have the followind WaveOutDataAvailable method:
private void WaveOutDataAvailable(object sender, WaveInEventArgs e)
{
var buf= e.Buffer;
_waveOutFileStream.Write(buf, 0, buf.Length);
_waveOutFileStream.Flush();
}
The sound in the resulting file is intermittent and slow, like having "blank" sections between the sound chunks, any ideas are appreciated.
End of part 1
Part 2
There is another version of this code where i'm trying to convert the WAV stream to mp3 stream on the fly and then write it to file, it looks like this:
private void InitAudioIn(DateTime dtNow)
{
_pathIn = string.Format(BaseDirectory + #"\({0:HH-mm-ss dd-MM-yyyy} IN).mp3", dtNow);
_waveIn = new WaveInEvent();
_waveIn.WaveFormat = new WaveFormat(44100, 2);
_waveIn.DataAvailable += WaveInDataAvailable;
_waveIn.RecordingStopped += WaveInRecordStopped;
_waveInFileStream = File.Create(_pathIn);
_waveIn.StartRecording();
}
With the WaveInDataAvailable method as follows:
private void WaveInDataAvailable(object sender, WaveInEventArgs e)
{
var wavToMp3Buffer = ConvertWavToMp3(e.Buffer, _waveIn.WaveFormat);
_waveInFileStream.Write(wavToMp3Buffer, 0, wavToMp3Buffer.Length);
_waveInFileStream.Flush();
}
The ConvertWavToMp3 method:
public byte[] ConvertWavToMp3(byte[] wavContent, WaveFormat waveFormat)
{
using (var baseMemoryStream = new MemoryStream())
using (var wavToMp3Writer = new LameMP3FileWriter(baseMemoryStream, waveFormat, 64))
{
wavToMp3Writer.Write(wavContent, 0, wavContent.Length);
wavToMp3Writer.Flush();
return baseMemoryStream.ToArray();
}
}
If i don't try to convert it to MP3 and just write it as a WAV file that it's absolutely fine, but if i try the MP3 conversion through the ConvertWavToMp3 method then the sound gets slow and intermittent, what is wrong with this implementation?
First part, you are making an invalid assumption about the buffer length being the same as the number of valid bytes in the buffer. Try:
private void WaveOutDataAvailable(object sender, WaveInEventArgs e)
{
_waveOutFileStream.Write(e.Buffer, 0, e.BytesRecorded);
}
Let the output stream handle flushing automatically. Trying to force data to disk like that will either not work or in some cases can cause unexpected results like partial block writes that can interfere with your data. Flush at the end of the recording, not during.
As to the second part...
Your code is creating a file that is the concatenation of a series of MP3 files, one for each buffer passed to your WaveInDataAvailable method, and including all the blank space at the end of those buffers. Of course it's not going to play back properly.
If you want to write an MP3 then do it directly. Make your _waveInFileStream an instance of LameMP3FileWriter and let it handle the work itself. Not only is this going to produce a much more useful output but you save yourself a lot of inefficient messing around with setting up and tearing down the encoder for every data block you receive.

Loading image from bytes gives AG_E_NETWORK_ERROR

Im writing my own cache for for our Silverlight application to get around any size problems.
The cache works fine, however, it doesnt work for images.
Ive narrowed down the problem to a little test application that converts a BitmapImage to bytes (to simulate reading it our from the cache) and then convert those bytes back into an image.
Here is the code:
public MainPage()
{
InitializeComponent();
firstImage.ImageOpened += first_Loaded;
}
void first_Loaded(object sender, RoutedEventArgs e)
{
var bmp = firstImage.Source;
var bytes = ToByteArray(bmp as BitmapImage);
otherImage.Source = CreateImageFromBytes(bytes);
}
private byte[] ToByteArray(BitmapImage bi)
{
var enc = new BitmapEncoder(bi);
return enc.GetBitmapData();
}
private BitmapImage CreateImageFromBytes(byte[] data)
{
var bitmapImage = new BitmapImage();
bitmapImage.ImageFailed += bitmapImage_ImageFailed;
var ms = new MemoryStream(data);
bitmapImage.SetSource(ms);
return bitmapImage;
}
void bitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
Console.WriteLine(e.ErrorException.Message);
}
The ImageFailed event is always raised with the message "AG_E_NETWORK_ERROR"
Ive read a little about the problem and it seems to have to do with the origin of the image being different but surely I should be able to load an image from bytes in memory right?
Any ideas how to get around this?
I should add that the converting to bytes works fine since if I save those bytes down to disk I can open the image.
From the Remarks in the Silverlight BitmapImage page on MSDN:
The BitmapImage can be used to reference images in the JPEG and PNG
file formats.
Unfortunately the BitmapEncoder class that you are using seems to encode bitmaps in BMP format. Hence the encoded buffer can't be decoded by BitmapImage.
You would have to find another implementation of a BitmapEncoder, which is capable of encoding JPEG or PNG.

C# Console app - Bing Maps - how to save output

I can get a URI from Bing Maps but when I try to save the resulting file (eg, a .jpg) it is empty (eg, 1 x 1 size). I've got it working fine in a WPF app with event handling but how do I do it with a console app?
Here's the code that works with WPF but the same code fails in a console app - I think it must be because the file is saved in an event handler and (far as I know) you can't do that in a console app.
public void vSaveBitmapImage(string sURI, // created with Bing Maps GeocodeServices
string sLocFname)
{
// save file name so event handler knows it
this.sFname = sLocFname;
try
{
BitmapImage bmpImage = new BitmapImage(new Uri(sURI, UriKind.RelativeOrAbsolute));
// setup event handler - file is saved just fine here
bmpImage.DownloadCompleted += vImage_DownloadCompleted;
}
catch (SystemException sex)
{
string s = sex.Message;
}
}
/// <summary>
/// handle the event that an image download has completed
/// </summary>
private void vImage_DownloadCompleted(object sender,
EventArgs e)
{
try
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)sender));
using (var filestream = new FileStream(this.sFname, FileMode.Create))
encoder.Save(filestream);
}
catch (SystemException sex)
{
string s = sex.Message;
}
}
For a console application, it may be simpler to download and save the file synchronously, e.g. with WebClient.DownloadFile().
var client = new WebClient();
client.DownloadFile( sURI, sLocFname );
I'm not sure what is going wrong with your event-based code.
My first guess is that the application drops out of Main() and exits before the background download completes. In that case, you may need to block the main thread until your vImage_DownloadCompleted event is completed.

Uploading Image to Imgur on Windows Phone

I'm currently trying to build an app that involves the user selecting a photo from their library (or taking a photo) and uploading it to Imgur. I have already built a fairly robust C# Imgur client for Windows Forms applications, but unfortunately porting it to the Windows Phone has been a disaster.
Here is the code that I am using:
public void UploadImageAsync(Stream PhotoStream)
{
try
{
WebClient w = new WebClient();
w.Headers["Content-type"] = "application/x-www-form-urlencoded";
string data = "key="+PublicKey+
"&_fake_status=200"+
"&type=base64"+
"&image="+PhotoStreamToBase64(PhotoStream);
w.UploadStringAsync(new Uri("http://api.imgur.com/2/upload", UriKind.Absolute), "POST", data);
}
catch (Exception ex)
{
}
}
string PhotoStreamToBase64(Stream PhotoStream)
{
MemoryStream memoryStream = new MemoryStream();
PhotoStream.CopyTo(memoryStream);
byte[] result = memoryStream.ToArray();
return System.Convert.ToBase64String(result);
}
What is interesting (and frustrating) is that it appears as though everything is working fine, and I receive a successful response after the upload has completed. However, when trying to view the image after being uploaded, the result looks like this: http://i.imgur.com/NWY0R.jpg.
This leads me to believe that somehow the image stream is being converted into the byte array incorrectly, or converted into a base 64 string incorrectly. In any case, I cannot get it to work and I am at a total loss. Does anybody have any idea? Any help would be greatly appreciated.
SpikeX pushed me toward Imgur's C# API example for image uploading. Borrowing the Base64 encoding logic from their example fixed the issue. Here is the now functional PhotoStreamToBase64 method:
string PhotoStreamToBase64(Stream PhotoStream)
{
MemoryStream memoryStream = new MemoryStream();
PhotoStream.CopyTo(memoryStream);
byte[] result = memoryStream.ToArray();
string base64img = System.Convert.ToBase64String(result);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < base64img.Length; i += 32766)
{
sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(32766, base64img.Length - i))));
}
return sb.ToString();
}

Categories

Resources