I'm trying to figure out how use to use ZXing Barcode Scanner nuget package in a simple Xamarin Forms application.
I'm trying to have a large area of my form show what my camera is seeing .. and if it scan's something.. then stop scanning and goto the next Page.
All the examples basically show this:
buttonScan.Click += (sender, e) => {
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
Console.WriteLine("Scanned Barcode: " + result.Text);
};
but I don't understand what the actual view/control/widget which gets displayed onto the actual page, is? Like a big square with the camera contents getting displayed etc.
What is the trick to drop a control onto the page .. which I can then wire up to scan whatever the camera is 'seeing' .. and hopefully it will 'read' the barcode or qrcode?
Related
I have a Xamarin.Android app that shows lyrics from Genius and can detect music playing from the notifications.
In this app, the Main Activity contains a FrameLayout that is dynamically filled with a Welcome screen layout or a Song layout. The first is used when the user opens the app and no song has been detected, while the second appears when the user searches a song or opens the app through the notification sent when the app finds a song playing. Both the Welcome layout and the Song layout use ConstraintLayouts to display content.
This is how the layout looks without content:
Sometimes while loading the song, the layout size is not updated to fit the content inside the views. It has a loading wheel that is set to Gone when the lyrics finish loading.
Fully loaded, the screen should look like this:
However, it looks like this:
You can see that the broken layout is the same size and shape as it is on Android Studio.
The only thing I've been able to correlate with this bug is this LogCat message:
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy#2bd3336
This message always appears when this bug happens, but not the other way around. Sometimes it appears but the bug does not happen.
From what I've googled, this is related to the notification opening another instance of the MainActivity. My testing suggests this as well since the OnCreate method is called even when the app is already opened and the notification is touched. I have not been able to get this bug to occur without opening the app through the notification.
Here's the code that created the notification:
private void CreateNotification(string title, string artist)
{
Log.WriteLine(LogPriority.Verbose, "NLService", "CreateNotification: Creating notification");
MainActivity.fromNotification = true;
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(Application.Context);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
stackBuilder.AddNextIntent(new Intent(Application.Context, typeof(MainActivity)));
PendingIntent resultIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true)
.SetContentTitle("SmartLyrics")
.SetContentText(artist + " - " + title)
.SetSmallIcon(Resource.Drawable.ic_stat_name)
.SetContentIntent(resultIntent)
.SetPriority(-1);
ntfManager = NotificationManagerCompat.From(Application.Context);
ntfManager.Notify(NOTIFICATION_ID, builder.Build());
Log.WriteLine(LogPriority.Info, "NLService", "CreateNotification (NLService): Notification made!");
}
I recorded my phone screen showing how it's behaving, first opening from the notification and then closing the app and opening the Song by searching from inside the app. Here's the LogCat for that session.
The whole project is open source, and the code is available here (the layout-testing branch is the one you should be looking at). Please spare me on how much this code is bad, I know about it and I'm slowly fixing stuff. This issue has been plaguing me for a long time and I want to get it fixed before working on other things.
Thanks in advance!
I have an application in which I have a requirement to take multiple images in one shot by phone camera. User don't want to open camera again and again.
I have tried https://github.com/jamesmontemagno/MediaPlugin Plugin also but it do not support continuous Images capturing. It clicks one image at a time.
Is there a way to achieve this behaviour?
Thanks
Can you try this in media plugin
for (int i = 0; i < 3; i++)
{
file = await MediaPicker.TakePhotoAsync(new StoreCameraMediaOptions { SaveToAlbum = true, Name = "", Directory = "" });
if (file != null)
{
//code to save
}
The user can press cancel at anytime to stop taking pictures, and it'll hit the return and stop running the code. But its ran the logic to save the images each time a photo has been taken.
I am currently working on an questionnaire program for android using xamarin and I'm using fragments to display the questions, as there are different question types. I need to make sure there is enough screen space for the questions but unfortunately I can't seem to find anything useful for it or find a version that works with xamarin.
The program is meant to check if there is enough room to draw the question, if there is it draws the question if not then it gets displayed on the next page. The questions are dynamically drawn so the program doesn't know before runtime if there is going to be space or not.
I have managed to get the screen size for the device but when I use similar code for the fragments it returns 0.
This is the code I used for the device and it works fine.
Point size = new Point();
this.WindowManager.DefaultDisplay.GetSize(size);
System.Diagnostics.Debug.WriteLine("Panel " + size.ToString());
I have managed to get the screen size for the device but when I use similar code for the fragments it returns 0.
You can get the fragment's size in your fragment like this:
public class MyFragment:Fragment
{
...
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
//add global layout event listener
view.ViewTreeObserver.GlobalLayout += ViewTreeObserver_GlobalLayout;
}
private void ViewTreeObserver_GlobalLayout(object sender, EventArgs e)
{
//get the size of fragment
var width=this.View.Width;
var height = this.View.Height;
}
}
First off, I'll explain my program.
I have one winform that I use as my control panel. From here I have the ability to take a screen shot of my desktop using the mouse to define the area. Much like how the snipping tool works within windows.
The screen shot is generated using my screen shot class which is ran by another form called Form1. This second form has no other code in it but is simply used to generate the rectangle the screen shot will use. From here, the taken screen shot is stored in the clip board and passed back to my Screenshot class.
Now, from here what I want is a picture box in my control panel to display this taken image. Exactly like how the snipping tool works. However, the code I have wrote to pass this image to the control panel from the screen shot class complains that the event handler is always returning as null.
The code I have wrote for this is as follows:
Image img = (Image)bitmap;
if (OnUpdateStatus == null) return;
ProgressEventArgs args = new ProgressEventArgs(img);
OnUpdateStatus(this, args);
I've tried commenting out the if statement but then the processing OnUpdateStatus throws an exception saying it no longer exists.
Now, in my control panel form I am trying to grab that image and display with the following code:
private ScreenCapture _screenCap;
public ControlPanel()
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
}
private void _screen_CapOnUpdateStatus(object sender, ProgressEventArgs e)
{
imagePreview.Image = e.CapturedImage;
}
I've spent hours on this but i cannot work out why the image won't display in the image box on my ControlPanel. Can anyone with a fresh set of eyes help me out? Why is it the screen shot image I take, isn't being displayed in my picture box held on my ControlPanel?
This is the problem, I think (there's a lot of code to skim through - a short but complete example would have been better):
ScreenCapture capture = new ScreenCapture();
capture.CaptureImage(...);
That's a new instance of ScreenCapture. It doesn't have any event handlers attached to it (which is why you're seeing a value of null for OnUpdateStatus. If you want to use the instance that you've created in the ControlPanel class, you'll want to pass that to your Form1 (e.g. in the constructor).
I am building a win8 app which requires to launch the webcam for taking photos only.
I have seen the sample codes given in MSDN for Camera captures but I only want is onclick of CAPTURE Button the webcam should launched, take a pic and save it.
While in the sample codes they made the user to select option from the list box and on selectionchanged, the required function has been called. My problem is that I don't required any Listbox. Also they have used a class called SuspensionManager which I didn't understand. I am really confused.
Can somebody show me a way out?
Try this:
using Windows.Media.Capture;
var ui = new CameraCaptureUI();
ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
var bitmap = new BitmapImage();
bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
Photo.Source = bitmap;
}
Took from here