尝试使用Android.Hardware.Camera拍照时出错

时间:2018-10-06 15:20:00

标签: c# android xamarin xamarin.forms camera

我试图在Xamarin.Form App中创建一个自定义渲染器以显示Camera Stream,iOS正常运行,但是android让我哭泣,这时我不知道该怎么做。

摄像头预览效果很好,我什至可以在前置摄像头和后置摄像头之间切换,但是我无法拍照,整个摄像头都按照以下教程在SurfaceView中进行流式处理:{{3 }}

这是我的代码:

Polygon

我在第316行不断收到此错误:

Polygon

1 个答案:

答案 0 :(得分:0)

这是通过按下按钮调用的新方法:

 /// <summary>
    /// cattura un immagine
    /// </summary>
    /// <returns>The picture.</returns>
    public async Task<byte[]> take_picture()
    {
        pictureTaken = null;

        try
        {
            this.PreviewCamera.StopPreview();
            this.PreviewCamera.TakePicture(new ShutterCameraCallback(),new RawCameraCallback(),new JpegCameraCallback());
            this.PreviewCamera.StartPreview();
            return pictureTaken;

        }
        catch(Exception e)
        { Console.WriteLine(e);}

        return null;
    }

这是三个类别:

    using System;
using static Android.Hardware.Camera;

namespace Hangover.Droid.CameraCallbacks
{
    public class ShutterCameraCallback : Java.Lang.Object, IShutterCallback
    {
        public void OnShutter()
        {
            Console.Write("shutter");
        }
    }
}

第二个:

using System;
using Android.Hardware;
using Java.Lang;
using static Android.Hardware.Camera;

namespace Hangover.Droid.CameraCallbacks
{
    public class RawCameraCallback : Java.Lang.Object ,IPictureCallback 
    {

        public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
            Console.Write("raw data");
            Console.Write(data);
        }
    }
}

第三个:

using System;
using static Android.Hardware.Camera;

namespace Hangover.Droid.CameraCallbacks
{
    public class ShutterCameraCallback : Java.Lang.Object, IShutterCallback
    {
        public void OnShutter()
        {
            Console.Write("shutter");
        }
    }
}

仍然没有运气,得到同样的错误:

Java.Lang.RuntimeException: takePicture failed
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <7802aa64ad574c33adca332a3fa9706a>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0001f] in <7802aa64ad574c33adca332a3fa9706a>:0 
  at Android.Hardware.Camera.TakePicture (Android.Hardware.Camera+IShutterCallback shutter, Android.Hardware.Camera+IPictureCallback raw, Android.Hardware.Camera+IPictureCallback jpeg) [0x0007d] in /Users/builder/data/lanes/5945/dffc5912/source/monodroid/external/xamarin-android/src/Mono.Android/obj/Release/android-27/mcw/Android.Hardware.Camera.cs:4763 
  at Hangover.Droid.CustomRenderer.CameraPreview+<take_picture>d__23.MoveNext () [0x00026] in /Users/Giulio_Serra/HangoverCross/Hangover/Hangover.Android/CustomRenderer/CameraPreview.cs:316 
  --- End of managed Java.Lang.RuntimeException stack trace ---
java.lang.RuntimeException: takePicture failed
    at android.hardware.Camera.native_takePicture(Native Method)
    at android.hardware.Camera.takePicture(Camera.java:1488)
    at android.hardware.Camera.takePicture(Camera.java:1433)
    at md58432a647068b097f9637064b8985a5e0.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
    at md58432a647068b097f9637064b8985a5e0.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30)
    at android.view.View.performClick(View.java:6259)
    at android.view.View$PerformClick.run(View.java:24732)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)