Forum Xamarin.Android

Custom camera TakePicture

Hi all,

I am implementing custom camera in my application.

        cam = getCameraInstance();
        cam.SetDisplayOrientation(90);
        FrameLayout preview = FindViewById<FrameLayout>(Resource.Id.surfaceview);
        mPreview = new Preview(this, cam);
        preview.AddView(mPreview);

Preview works fine. I need to take snap on button click.

Android.Hardware.Camera have TakePicture Method:

        public void TakePicture(
            Android.Hardware.Camera.IShutterCallback shutter
            Android.Hardware.Camera.IPictureCallback raw
            Android.Hardware.Camera.IPictureCallback jpeg
        )

My doubt is , What should i assign to these parameters?

        Android.Hardware.Camera.IShutterCallback ishuttercallback;
        Android.Hardware.Camera.IPictureCallback ipicturerawcallback;
        Android.Hardware.Camera.IPictureCallback ipicturejpgcallback;

Someone please Help,
Thanks

Posts

  • KishoreKishore INMember

    I got it working,

        public class MainActivity : Activity, Android.Hardware.Camera.IPictureCallback ,Android.Hardware.Camera.IPreviewCallback, Android.Hardware.Camera.IShutterCallback, ISurfaceHolderCallback
        {
            Camera camera;
            String PICTURE_FILENAME = "picture.jpg";
            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);
    
                SetContentView (Resource.Layout.Main);
    
                SurfaceView surface = (SurfaceView)FindViewById (Resource.Id.surface);
                var holder = surface.Holder;
                holder.AddCallback (this);
                holder.SetType (Android.Views.SurfaceType.PushBuffers);
    
                FindViewById(Resource.Id.myButton).Click += delegate {
                    Android.Hardware.Camera.Parameters p = camera.GetParameters();
                    p.PictureFormat = Android.Graphics.ImageFormatType.Jpeg;
                    camera.SetParameters(p);
                    camera.TakePicture(this,this,this);
    
                };
            }
    
            void Camera.IPictureCallback.OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
            {
                FileOutputStream outStream = null;
                File dataDir = Android.OS.Environment.ExternalStorageDirectory;
                if (data != null) {
                    try{
                        outStream = new FileOutputStream(dataDir + "/" + PICTURE_FILENAME);
                        outStream.Write(data);
                        outStream.Close();
                    }catch(FileNotFoundException e){
                        System.Console.Out.WriteLine (e.Message);
                    }catch(IOException ie){
                        System.Console.Out.WriteLine (ie.Message);
                    }
                }
            }
    
            void Camera.IPreviewCallback.OnPreviewFrame(byte[] b, Android.Hardware.Camera c)
            {
    
            }
    
            void Camera.IShutterCallback.OnShutter()
            {
    
            }
    
    
            public void SurfaceCreated(ISurfaceHolder holder)
            {
                try{
                    camera = Android.Hardware.Camera.Open();
                    Android.Hardware.Camera.Parameters p = camera.GetParameters();
                    p.PictureFormat = Android.Graphics.ImageFormatType.Jpeg;
                    camera.SetParameters(p);
                    camera.SetPreviewCallback(this);
                    camera.Lock();
                    camera.SetPreviewDisplay(holder);
                    camera.StartPreview();
                }
                catch(IOException e){
                }
            }
    
            public void SurfaceDestroyed(ISurfaceHolder holder){
    
                camera.Unlock ();
                camera.StopPreview ();
                camera.Release ();
            }
    
            public void SurfaceChanged(ISurfaceHolder holder,Android.Graphics.Format f,int i, int j)
            {
            }
        }
    
  • ArnoldBoschArnoldBosch ZAMember ✭✭

    Hi Kishore

    Awesome code im using it in my application however if I press back to go to prev activity it crashes the application, dont know if this happens on your side also?

  • Added the following to get the Kishore code to work in a New Xamarin Android Project:


    using System;

    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;

    using Android.Hardware;
    using System.IO;

    using Java.IO;

    using Environment = Android.OS.Environment;
    using Uri = Android.Net.Uri;

    using Android.Graphics;

    Then fixed the errors that showed up with the includes above... Didn't take much to get this to work well.

    Did change the code to generate unique Picture Filenames using Date and Time Stamp like this:


    String dataDir = (String) new Java.IO.File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");

    DateTime DT = DateTime.Now;

    String DateTimeStamp = DT.Year.ToString ("D4") + "-" + DT.Month.ToString ("D2") + "-" + DT.Day.ToString ("D2") + "-" + DT.Hour.ToString ("D2") + DT.Minute.ToString ("D2") + DT.Second.ToString ("D2");

    // Example Picture Filename: "Our-Photo-2015-02-06-123456.jpg"
    String PictureFilename = "Our-Photo-" + DateTimeStamp + ".jpg";

    String FileName = dataDir + "/" + PictureFilename;

    Then changed "FileOutputStream" to use "FileStream" instead.

    Even migrated this into a working program that uses TabHost Tabs and it works GREAT!

    Thanks Kishore for your wonderful code example.

    :smiley:

  • GustavoGalanGustavoGalan USMember ✭✭
    edited March 2015

    Mansour Ansari, we are looking to have a custom camera as well coded in our project. What we are afraid is that it might not work for certain devices, like HTC vs Samsung vs Motorola.... have you experienced issues like this where the custom camera would not work for certain devices.
    Thanks

  • @ArnoldBosch The code was also crashing on clicking back button, but this solved it:

    public void SurfaceDestroyed(ISurfaceHolder holder)
    {           
        camera.Unlock ();
        camera.StopPreview ();
        camera.SetPreviewCallback (null);
        camera.Release ();
        camera = null;
    }
    
  • kirankanuskirankanus USMember

    Kishore how can i get the complete code

  • DanrangiDanrangi NEMember
    Please is ur camera properly released? Am using surfaceview bt d camera isn't released until I restart my phone
  • sey1995sey1995 Member ✭✭

    I m still getting the "Java.Lang.RuntimeException: Fail to connect to camera service" error :(:'( Anyone know why?

  • NickKovalskyNickKovalsky USMember ✭✭✭

    permissions?..

Sign In or Register to comment.