I am trying to read multiple barcodes in a jpg image using the ZXing library. But it's returning only one barcode. I am not getting any source on how I can modify my code so that it read multiple barcodes. My code is:
private string readPDFBarcode(String fname) {
IBarcodeReader reader = new BarcodeReader()
{
AutoRotate = true,
TryInverted = true,
Options = new DecodingOptions
{
TryHarder = true,
PureBarcode = false,
ReturnCodabarStartEnd = true,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.CODE_128, BarcodeFormat.CODE_39, BarcodeFormat.UPC_E }
}
};
Bitmap oBitmap = new Bitmap(fname);
var result = reader.Decode(oBitmap);
if (result != null)
{
return result.ToString();
}
else {
return "";
}
}
Any suggestions will be of great help. Thank you!
Try this one:
var results = reader.DecodeMultiple(oBitmap);
If it isn't available in your environment, please give some information about the target .net framework (classic or core).
Related
As the title described, I'm having a problem with this proto message:
(Proto file)
message StreamingRecognizeRequest{
oneof streaming_request{
StreamingRecognitionConfig streaming_config = 1;
bytes audio_content = 2;
}
}
Here is how I called it:
(C# main function)
await voice.RequestStream.WriteAsync(new StreamingRecognizeRequest
{
StreamingConfig =
{
SingleUtterance = true,
InterimResults = true,
Config =
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
MaxAlternatives = 1,
SampleRateHertz = 8000,
Enhanced = true,
SpeechContexts =
{
}
}
}
});
But the debugger keep marking my Stream Recognize Reuqest as null reference exception, the same pattern works fine in NodeJS but I only have this error in C#. Note that the first message send to the gRPC server doesn't require "Audio Content". So what is the root cause of this error and how to solve it? Or this is gRPC bug on c#?
Thank you!
It's pretty funny how I fixed it. Just not putting them inside each other but seperately create new object:
RecognitionConfig regcfg = new RecognitionConfig
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
MaxAlternatives = 1,
SampleRateHertz = 8000,
Enhanced = true,
};
StreamingRecognitionConfig stc = new StreamingRecognitionConfig
{
Config = regcfg,
SingleUtterance = true,
InterimResults = true,
};
//Create new gRPC stream listener
var voice = vaisclient.StreamingRecognize(meta);
await voice.RequestStream.WriteAsync(new StreamingRecognizeRequest
{
StreamingConfig = stc
}) ;
I am working on an application in C# using XAudio2 from SharpDX. I know its outdated but instead of CSCore it offers the better Reverb. The aim itself is simple: load a wav file (48khz,24bit,1channel), get it through the XAudio2 Reverb with several Reverb presets and do a playback.
I managed to implement everything for a normal playback and also built in the Reverb. The audio file will be played back with the defaul reverb settings but as soon as I change the preset or change a specific value (RoomFilterFreq) the file isnt audible anymore and i don´t know why this happens. Does anyone has a clue about this? I worked through the few examples which are still online but could not find a reason for this behaviour.
I´m coding under Visual Studio 2019 with Net 4.7.2. The code looks as followed:
xaudio2 = new XAudio2();
xaudio2.StartEngine();
var masteringVoice = new MasteringVoice(xaudio2, 1, 48000);
var stream = new SoundStream(File.OpenRead(soundFilepath));
SharpDX.Multimedia.WaveFormat waveFormat = stream.Format;
var buffer = new AudioBuffer
{
Stream = stream.ToDataStream(),
AudioBytes = (int)stream.Length,
Flags = BufferFlags.EndOfStream
};
stream.Close();
sourceVoice = new SourceVoice(xaudio2, waveFormat, true);
// Set Loop
if (f1.loopcheckBox.Checked == true)
{
buffer.LoopBegin = buffer.PlayBegin;
buffer.LoopLength = buffer.PlayLength;
buffer.LoopCount = AudioBuffer.LoopInfinite;
}
// Set Reverb
if (f1.reverbenableButton.BackColor == Color.LightGreen)
{
var reverb = new Reverb(xaudio2);
var reverbparameters = (ReverbParameters)ReverbI3DL2Parameters.Presets.Room;
var effectDescriptor = new EffectDescriptor(reverb);
if (waveFormat.Channels == 1)
{
effectDescriptor.OutputChannelCount = 1;
}
else if (waveFormat.Channels == 2)
{
effectDescriptor.OutputChannelCount = 2;
}
else
{
MessageBox.Show("Channelrate not supported!");
return sourceVoice = null;
}
sourceVoice.SetEffectChain(effectDescriptor);
sourceVoice.SetEffectParameters(0, reverbparameters);
sourceVoice.EnableEffect(0);
}
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
sourceVoice.Start();
I can successfully decode barcodes ITF using ZXing.Net, but in some cases the Result.Text have some missing numbers, like in this:
original barcode: 836900000008262500403007233338786038100661049195
Result.Text : 83690000000 26250040300 23333878603 10066104919
In this case there are missing a 8,7,8 and 5
In this other case, the numbers are reordered to a random order and have some missing numbers:
original barcode: 23793381285017475716618000050809162310000010000
Result.Text 23791623100000100003381250174757161800005080
Any ideas why is it happening?
Thanks
EDIT: 06/12/2015
If I don't specify the PossibleFormats the decoder decodes all the codes (the images are here:1drv.ms/1B6wD5c) as ITF and the result is the same as the described above. The code I'm using is here:
public BarCodeReaderService(IDialogService _dialogService)
{
dialogService = _dialogService;
barcodeReader = new BarcodeReader
{
Options = new DecodingOptions
{
PureBarcode = true,
TryHarder = true,
PossibleFormats = new BarcodeFormat[] { BarcodeFormat.ITF }
},
};
}
public async Task<string> ScanBitmapAsync(StorageFile file)
{
string result = null;
using (var stream = await file.OpenReadAsync())
{
// initialize with 1,1 to get the current size of the image
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
// and create it again because otherwise the WB isn't fully initialized and decoding
// results in a IndexOutOfRange
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
try
{
result = ScanBitmap(writeableBmp);
}
catch (Exception ex)
{
dialogService.ShowAsync("Ocorreu um erro \n" + ex.Message, "Erro");
}
}
return result != null ? result : null;
}
private string ScanBitmap(WriteableBitmap writeableBmp)
{
var result = barcodeReader.Decode(writeableBmp);
return result != null ? result.Text : null;
}
The missing numbers are indeed validation numbers and they have to be calculated.
I'm using ZXing.net to create a UserControl for scanning barcode into a Windows Phone 8.1 RT app using the camera.
The barcode are decoded well but i'm having freezes on the UI when the method CapturePhotoToStreamAsync is called, even it is awaited.
It takes about 600 ms to execute.
I'm testing the app into the emulator.
The code below is executed in an async method :
// Preview of the camera
await _mediaCapture.InitializeAsync(settings);
VideoCapture.Source = _mediaCapture;
VideoCapture.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;
await _mediaCapture.StartPreviewAsync();
VideoEncodingProperties res = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
ImageEncodingProperties iep = ImageEncodingProperties.CreateBmp();
iep.Height = res.Height;
iep.Width = res.Width;
var barcodeReader = new BarcodeReader
{
TryHarder = true,
AutoRotate = true
};
WriteableBitmap wB = new WriteableBitmap((int)res.Width, (int)res.Height);
while (_result == null)
{
using (var stream = new InMemoryRandomAccessStream())
{
await _mediaCapture.CapturePhotoToStreamAsync(iep, stream);
stream.Seek(0);
await wB.SetSourceAsync(stream);
_result = barcodeReader.Decode(wB);
}
}
await _mediaCapture.StopPreviewAsync();
//callback to handle result
ScanCallback(_result.Text);
What can I do to prevent the UI from freezing ?
Luckily you don't have to capture photo to decode QRCode/Barcode on Windows Phone 8.1 Runtime. It is a quite new solution actually, but it works: https://github.com/mmaitre314/VideoEffect#realtime-video-analysis-and-qr-code-detection
After istalling the nuget package, you can easily decode barcodes realtime, without the need of calling CapturePhotoToStreamAsync. The only drawback is that you can only target ARM. You can find the sample code on the site. Or you can contact me, and I can send you the part of my project where I use this.
i always get better results when i use the camera to take a picture first (lets you focus on the correct place where the barcode is) and then send the picture for barcode recognition.
the stuttering is caused because you try to keep checking the live feed for barcodes which can be hard on the CPU (especially for ARM devices)
var dialog = new CameraCaptureUI();
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
var stream = await file.OpenReadAsync();
// initialize with 1,1 to get the current size of the image
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
// and create it again because otherwise the WB isn't fully initialized and decoding
// results in a IndexOutOfRange
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
var result = ScanBitmap(writeableBmp);
string barcode = "";
if (result != null)
{
barcode = result.Text;
}
and here the ScanBitmap method:
private Result ScanBitmap(WriteableBitmap writeableBmp)
{
var barcodeReader = new BarcodeReader
{
Options = new ZXing.Common.DecodingOptions()
{
TryHarder = true
},
AutoRotate = true
};
var result = barcodeReader.Decode(writeableBmp);
if (result != null)
{
CaptureImage.Source = writeableBmp;
}
return result;
}
i am beginning in develop winphone and nokia imaging sdk. i have two function.
firstly, i call the function below to change image to gray color
private async void PickImageCallback(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
{
return;
}
using (var source = new StreamImageSource(e.ChosenPhoto))
{
using (var filters = new FilterEffect(source))
{
var sampleFilter = new GrayscaleFilter();
filters.Filters = new IFilter[] { sampleFilter };
var target = new WriteableBitmap((int)CartoonImage.ActualWidth, (int)CartoonImage.ActualHeight);
var renderer = new WriteableBitmapRenderer(filters, target);
{
await renderer.RenderAsync();
_thumbnailImageBitmap = target;
CartoonImage.Source = target;
}
}
}
SaveButton.IsEnabled = true;
}
then i call function to change image to binary color
private async void Binary(WriteableBitmap bm_image)
{
var target = new WriteableBitmap((int)CartoonImage.ActualWidth, (int)CartoonImage.ActualHeight);
MemoryStream stream= new MemoryStream();
bm_image.SaveJpeg(stream, bm_image.PixelWidth, bm_image.PixelHeight, 0, 100);
using (var source = new StreamImageSource(stream))
{
using (var filters = new FilterEffect(source))
{
var sampleFilter = new StampFilter(5, 0.7);
filters.Filters = new IFilter[] { sampleFilter };
var renderer1 =new WriteableBitmapRenderer(filters, target);
{
await renderer1.RenderAsync();
CartoonImage.Source = target;
}
}
}
}
but when it run to " await renderer1.RenderAsync();" in the second function, it doesn't work. How can i solve it. And you can explain for me about how "await" and "async" work ?
thank you very much!
I'm mostly guessing here since I do not know what error you get, but I'm pretty sure your problem lies in setting up the source. Have you made sure the memory stream position is set to the beginning (0) before creating an StreamImageSource?
Try adding:
stream.Position = 0;
before creating the StreamImageSource.
Instead of trying to create a memory stream from the writeable bitmap I suggest doing:
using Nokia.InteropServices.WindowsRuntime;
...
using (var source = new BitmapImageSource(bm_image.AsBitmap())
{
...
}