Forum Xamarin.Android

Video/animation

mlostmlost USMember

Hello everyone ;)
I'm creating my very first Android app with Xamarin in Visual Studio (I'm rookie), for now I made simple QR reader (app code will be at the bottom). It took me 5 days ( :| ) including reinstaling whole enviroment or starting a couple times from blank project (because of shitty tutorials).
Going to my question:

  • After scaning QR code (which contains path to animation) I want to (after clicking on button) display the animation with sound on new view with camera view as a video/animation background, I mean there is view of back camera for scanning QR and I want to keep that view as a background of animation (that background should be showing actual view of back camera, I mean it should be moving when device will move). And when animation ends app should "move" into starting activity.

I didn't found nothing specific in google so, please, can someone tell me how to do that? Or maybe someone know the right tutorial?

I'm beginner in Xamarin in Visual Studio and for me not everything is that obvious as in just Visual Studio.

Thank you in advance for your help and sorry for my english :blush:

MainActivity.cs :

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Views;
using Android.Gms.Vision.Barcodes;
using Android.Gms.Vision;
using Android.Graphics;
using Android.Runtime;
using System;
using Android.Support.V4.App;
using Android;
using Android.Content.PM;
using static Android.Gms.Vision.Detector;
using Android.Util;
using Android.Content;

namespace limbo
{
    [Activity(Label = "limbo", MainLauncher = true, Icon = "@drawable/icon", Theme ="@style/Theme.AppCompat.Light.NoActionBar")]
    public class MainActivity : AppCompatActivity, ISurfaceHolderCallback, IProcessor
    {
        SurfaceView cameraPreview;
        TextView textResult;
        BarcodeDetector barcodeDetector;
        CameraSource cameraSource;
        const int RequestCameraPermissionID = 1001;
        Button btnShAn;


        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case RequestCameraPermissionID:
                    if (grantResults[0] == Permission.Granted)
                    {
                        if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
                        {
                            //request permission
                            ActivityCompat.RequestPermissions(this, new string[]
                            {
                    Manifest.Permission.Camera
                            }, RequestCameraPermissionID);
                            return;
                        }
                        try
                        {
                            cameraSource.Start(cameraPreview.Holder);
                        }
                        catch (InvalidOperationException) { }
                    }
                    break;
            }
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            cameraPreview = FindViewById<SurfaceView>(Resource.Id.cameraPreview);
            textResult = FindViewById<TextView>(Resource.Id.txtResult);
            btnShAn = FindViewById<Button>(Resource.Id.btnShowAnimation);

            barcodeDetector = new BarcodeDetector.Builder(this)
                .SetBarcodeFormats(BarcodeFormat.QrCode)
                .Build();
            cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .SetRequestedPreviewSize(640, 480)
                .Build();

            cameraPreview.Holder.AddCallback(this);
            barcodeDetector.SetProcessor(this);
        }

        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {
        }

        public void SurfaceCreated(ISurfaceHolder holder)
        {
            if(ActivityCompat.CheckSelfPermission(ApplicationContext,Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
            {
                //request permission
                ActivityCompat.RequestPermissions(this, new string[]
                {
                    Manifest.Permission.Camera
                }, RequestCameraPermissionID);
                return;
            }
            try
            {
                cameraSource.Start(cameraPreview.Holder);
            }
            catch (InvalidOperationException){ }
        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            cameraSource.Stop();
        }

        public void ReceiveDetections(Detections detections)
        {
            SparseArray qrcodes = detections.DetectedItems;
            if (qrcodes.Size() != 0)
            {
                textResult.Post(() =>
                {
                    Vibrator vib = (Vibrator)GetSystemService(Context.VibratorService);
                    vib.Vibrate(200);
                    textResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
                });
            }
        }

        public void Release()
        {
        }
    }
}

Main.axml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <SurfaceView
        android:id="@+id/cameraPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true" />
    <TextView
        android:id="@+id/txtResult"
        android:gravity="center_horizontal"
        android:textSize="20sp"
        android:text="Please focus camera to QR code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="false"
        android:background="@color/bright_foreground_material_light"
        android:textColor="@color/bright_foreground_material_dark" />
    <Button xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/btnShowAnimation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show scanned"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>
Sign In or Register to comment.