Xamarin.iOS ZXing.Net.Mobile barcode scanner - ios

I'm trying to add barcode scanner feature to my xamarin.ios app. I'm developing from visual studio and I've added the Zxing.Net.Mobile component from xamarin components store.
I've implemented it as shown in the samples:
ScanButton.TouchUpInside += async (sender, e) => {
//var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
//options.AutoRotate = false;
//options.PossibleFormats = new List<ZXing.BarcodeFormat>() {
// ZXing.BarcodeFormat.EAN_8, ZXing.BarcodeFormat.EAN_13
//};
var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
//scanner.TopText = "Hold camera up to barcode to scan";
//scanner.BottomText = "Barcode will automatically scan";
//scanner.UseCustomOverlay = false;
scanner.FlashButtonText = "Flash";
scanner.CancelButtonText = "Cancel";
scanner.Torch(true);
scanner.AutoFocus();
var result = await scanner.Scan(true);
HandleScanResult(result);
};
void HandleScanResult(ZXing.Result result)
{
if (result != null && !string.IsNullOrEmpty(result.Text))
TextField.Text = result.Text;
}
The problem is that when I tap the scan button, the capture view is shown correctly but if I try to capture a barcode nothing happens and it seems the scanner doesn't recognize any barcode.
Someone has experienced this issue? How can I made it work?
Thanks in advance for your help!

I answered a similar question here. I couldn't get barcodes to scan because the default camera resolution was set too low. The specific implementation for this case would be:
ScanButton.TouchUpInside += async (sender, e) => {
var options = new ZXing.Mobile.MobileBarcodeScanningOptions {
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};
var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
.
.
.
scanner.AutoFocus();
//call scan with options created above
var result = await scanner.Scan(options, true);
HandleScanResult(result);
};
And then the definition for HandleCameraResolutionSelectorDelegate:
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution () { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions [availableResolutions.Count - 1];
}

Related

Xamarin ios ZXING scanner not analyzing QR

i'm using an Iphone 10 with ios 15.3.1,Zxing version = 2.4.1, compiling the xamarin project in windows. I been trying to make a qr scanner with zxing for ios android, but when i run my app in ios, it won't go to the onResult event. I've tried with a lot of methods of doing this, but nothing seems to work. The camera shows, but the result never comes truh. ¿Does someone have any idea why this is happening? I also tried apps from other devs that are using zxing and it also dosen't work. but when i scan with the camera app it works.
async void OnButtonClickedAsync(object sender, System.EventArgs e)
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};
MobileBarcodeScanner scanner = new MobileBarcodeScanner();
//scanner.TopText = "Hold camera up to barcode to scan";
//scanner.BottomText = "Barcode will automatically scan";
//scanner.UseCustomOverlay = false;
scanner.FlashButtonText = "Flash";
scanner.CancelButtonText = "Cancel";
scanner.Torch(true);
scanner.AutoFocus();
var result = await scanner.Scan(options);
HandleScanResult(result);
}
void HandleScanResult(ZXing.Result result)
{
if (result != null && !string.IsNullOrEmpty(result.Text))
scanResultText.Text = result.Text;
}
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution() { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions[availableResolutions.Count - 1];
}
I compiled using a mac and it worked fine, must be a bug of visual studio on windows.

Xamarin.Forms ZXing.Net.Mobile loosing current page after scan result on iOS 10

I'm using Xamarin.Forms and I have implemented ZXing.Net.Mobile for scanning bar codes.
On Android it's working fine, on iOS 10 after reading a barcode the function "OnScanResult" is fired and executes the command Navigation.PopAsync() which closes the scanning page but after a second it closes also the current page where I have displayed the result !
MyTapScan.Tapped += async (sender, e) =>
{
await MyBtScan.ScaleTo(1.20, 100, Easing.Linear);
await MyBtScan.ScaleTo(1, 100, Easing.Linear);
await Task.Delay(50);
//--------------------------------------------
MyAppLib.MyAppUtilitiesBarCodeReader MyBarCodeReader = new MyAppLib.MyAppUtilitiesBarCodeReader();
var MyScannerPage = MyBarCodeReader.GetBarCodeReaderPage();
//--------------------------------------------
MyScannerPage.OnScanResult += (result) => {
//Stop scanning
MyScannerPage.IsScanning = false;
//Pop the page and show the result
Device.BeginInvokeOnMainThread(() => {
Navigation.PopAsync();
MyMachSerialNumber.Text = result.Text;
});
};
//--------------------------------------------
//Display scanner
await Navigation.PushAsync(MyScannerPage);
};
Please please help..!! :)
Every time MyTapScan.Tapped is called you are subscribing to MyScannerPage.OnScanResult so if you tap button 5 times your OnScanResult will be called 5 times. I hope now you know how to solve that.
One of possible solutions:
Take your OnScanResult delegate and make it separate function, let say ScanFinished. Then instead of
MyScannerPage.OnScanResult += (result)
do
MyScannerPage.OnScanResult -= ScanFinished;
MyScannerPage.OnScanResult += ScanFinished;
Then you can be sure the event unsubscribed before you subscribe to it again
I have introduced a new variable to check if the scanning was already fired and now it's working fine and as expected.
This is the code:
MyTapScan.Tapped += async (sender, e) =>
{
await MyBtScan.ScaleTo(1.20, 100, Easing.Linear);
await MyBtScan.ScaleTo(1, 100, Easing.Linear);
await Task.Delay(50);
bool MyIsScanning = true;
//--------------------------------------------
MyAppLib.MyAppUtilitiesBarCodeReader MyBarCodeReader = new MyAppLib.MyAppUtilitiesBarCodeReader();
var MyScannerPage = MyBarCodeReader.GetBarCodeReaderPage();
//--------------------------------------------
MyScannerPage.OnScanResult += (result) => {
//Stop scanning
MyScannerPage.IsScanning = false;
//Pop the page and show the result
Device.BeginInvokeOnMainThread(() => {
if (MyIsScanning == true)
{
MyIsScanning = false;
MyMachSerialNumber.Text = result.Text;
Navigation.PopAsync();
}
});
};
//--------------------------------------------
//Display scanner
await Navigation.PushAsync(MyScannerPage);
};

Printing from Win RT Application (Windows 8.1 App)

I am developing a POS application in windows 8.1 (Universal App).
Order receipt will be printed from the application and even I am able to do so.
Printer - EPSON TM-U220D
Input - A grid is being created pragmatically with dynamic content within the ViewModel. So this grid is the input for printer
Output - In the print preview (Attahced pic 1) all looks good but when the receipt is actually printed then content is cut off from the end (Attahced pic 2)
PIC 1
PIC 2
Observations -
If I print a normal text file manually using print command (Right click file and then print), then all content is printed perfectly.
If I print that SAME content, from the application, by creating Dynamic grid, then with Small font size print is good but with bit bigger font size content again cuts off.
Tried -
Optimized the code for Creating Gird, by specifying height
Questions-
If preview is all good then why not output
Did anyone tried using ePOS-Print_SDK_141020E for Windows Store app?
Generate Dynamic Grid Code
private void AddRows(Grid grid, int count)
{
for (int i = 0; i < count; i++)
{
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
grid.RowDefinitions.Add(row);
}
}
private void AddColumns(Grid grid, int count)
{
for (int i = 0; i < count; i++)
{
grid.ColumnDefinitions.Add(new ColumnDefinition());
}
}
private TextBlock CreateTextBlock(string text, Color color, FontWeight fw, double fs = 10, int thick = 5)
{
if (color == null) color = Colors.Black;
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = text;
txtBlock1.FontSize = fs;
txtBlock1.FontWeight = fw;
txtBlock1.Foreground = new SolidColorBrush(color);
txtBlock1.VerticalAlignment = VerticalAlignment.Center;
txtBlock1.Margin = new Thickness(thick);
return txtBlock1;
}
private async Task<Grid> CreateDynamicWPFGrid()
{
Grid ParentGrid = new Grid();
AddRows(ParentGrid, 8);
/* Start First Grid*/
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 230;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.Margin = new Thickness(24, 0, 0, 0);
AddColumns(DynamicGrid, 2);
AddRows(DynamicGrid, 3);
TextBlock txtBlock1 = CreateTextBlock(DateTime.Now.ToString("M/d/yy"), Colors.Black, FontWeights.Normal);
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 1);
.
.
.
.
Return ParentGrid;
}
Printer Events Code
//Register Print Contract
async Task RegisterPrintContract()
{
PrintManager manager = PrintManager.GetForCurrentView();
manager.PrintTaskRequested += OnPrintTaskRequested;
await PrintManager.ShowPrintUIAsync();
}
//Unregister Print Contract
void UnregisterPrintContract()
{
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested -= OnPrintTaskRequested;
}
void OnPrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// If I need to be asynchronous, I can get a deferral. I don't *need*
// to do this here, I'm just faking it.
var deferral = args.Request.GetDeferral();
PrintTask printTask = args.Request.CreatePrintTask("My Print Job", OnPrintTaskSourceRequestedHandler);
printTask.Completed += OnPrintTaskCompleted;
deferral.Complete();
}
void OnPrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// TODO: Tidy up.
this._document = null;
this._pages = null;
}
async void OnPrintTaskSourceRequestedHandler(PrintTaskSourceRequestedArgs args)
{
var deferral = args.GetDeferral();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
this._document = new PrintDocument();
this._document.Paginate += OnPaginate;
this._document.GetPreviewPage += OnGetPreviewPage;
this._document.AddPages += OnAddPages;
// Tell the caller about it.
args.SetSource(this._document.DocumentSource);
});
deferral.Complete();
}
void OnAddPages(object sender, AddPagesEventArgs e)
{
// Loop over all of the preview pages and add each one to add each page to be printied
// We should have all pages ready at this point...
foreach (var page in this._pages)
{
//this._pages[page.Key]
this._document.AddPage(this._pages[page.Key]);
}
PrintDocument printDoc = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
async void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
Grid x = await CreateDynamicWPFGrid();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// NB: assuming it's ok to keep all these pages in
// memory. might not be the right thing to do
// of course.
if (this._pages == null)
{
this._pages = new Dictionary<int, UIElement>();
}
if (!this._pages.ContainsKey(e.PageNumber))
{
this._pages[e.PageNumber] = x;
}
if (this._document == null)
this._document = new PrintDocument();
this._document.SetPreviewPage(e.PageNumber, this._pages[e.PageNumber]);
}
);
}
async void OnPaginate(object sender, PaginateEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// I have one page and that's *FINAL* !
this._document.SetPreviewPageCount(e.CurrentPreviewPageNumber, PreviewPageCountType.Final);
});
}

How to write to file (encode) modified camera frames in Windows Phone 8

I have custom an camera app in Windows Phone 8. I need add a watermark image to each frame from camera capture and then record to a video.
I can customise each frame from the preview using the following code:
int[] pixelData = new int[(int)(camera.PreviewResolution.Width * camera.PreviewResolution.Height)];
camera.GetPreviewBufferArgb32(pixelData);
return pixelData;
and write it back to the preview.
my problem is that while I can show the frames on the screen while the user is recording a movie using the camera I cant find a working solution for WP8 to encode the frames and audio to save to a file.
I already tried opencv,libav and others without success,
if anyone can point me to the right direction it would be greatly appreciated.
You can do it like this.
private void GetCameraPicture_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.CameraCaptureTask cameraCaptureTask = new Microsoft.Phone.Tasks.CameraCaptureTask();
cameraCaptureTask.Completed += cct_Completed;
cameraCaptureTask.Show();
}
try
{
if (e.TaskResult == Microsoft.Phone.Tasks.TaskResult.OK)
{
var imageStream = e.ChosenPhoto;
var name = e.OriginalFileName;
using (MemoryStream mem = new MemoryStream())
{
TextBlock tb = new TextBlock() { Text = DateTime.Now.ToString("dd MMM yyyy, HH:mm"), Foreground = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)), FontSize = 40 };
BitmapImage finalImage = new BitmapImage();
finalImage.SetSource(imageStream);
WriteableBitmap wbFinal = new WriteableBitmap(finalImage);
wbFinal.Render(tb, null);
wbFinal.Invalidate();
wbFinal.SaveJpeg(mem, wbFinal.PixelWidth, wbFinal.PixelHeight, 0, 100);
mem.Seek(0, System.IO.SeekOrigin.Begin);
MediaLibrary lib = new MediaLibrary();
lib.SavePictureToCameraRoll("Copy" + name, mem.ToArray());
}
}
}
catch (Exception exp) { MessageBox.Show(exp.Message); }
Hope it may help you.

C# VS WP8 Linking a created tile to a Uri Scheme

First i create a tile with this code:
private void btnIconicTile_Click(object sender, RoutedEventArgs e)
{
IconicTileData oIcontile = new IconicTileData();
oIcontile.Title = "Hello Iconic Tile!!";
oIcontile.Count = 7;
oIcontile.IconImage = new Uri("Assets/Tiles/Iconic/202x202.png", UriKind.Relative);
oIcontile.SmallIconImage = new Uri("Assets/Tiles/Iconic/110x110.png", UriKind.Relative);
oIcontile.WideContent1 = "windows phone 8 Live tile";
oIcontile.WideContent2 = "Icon tile";
oIcontile.WideContent3 = "All about Live tiles By WmDev";
oIcontile.BackgroundColor = System.Windows.Media.Colors.Black;
// find the tile object for the application tile that using "Iconic" contains string in it.
ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Iconic".ToString()));
if (TileToFind != null && TileToFind.NavigationUri.ToString().Contains("Iconic"))
{
TileToFind.Delete();
ShellTile.Create(new Uri("/MainPage.xaml?id=Iconic", UriKind.Relative), oIcontile, true);
}
else
{
ShellTile.Create(new Uri("/MainPage.xaml?id=Iconic", UriKind.Relative), oIcontile, true);//
}
}
Now i want that the created tile in the homescreen links to an app (Uri Scheme?) like this on for ex:
await Windows.System.Launcher.LaunchUriAsync(new System.Uri("whatsapp:"));
How i can modify the "link" of that recently created tile?
Yes i need too.
Windows.System.Launcher.LaunchUriAsync(new System.Uri("whatsapp:"))
homescreen

Resources