移动闪光灯打开使用xamarin但它没有转动,为什么?

时间:2013-06-28 12:23:51

标签: c# .net xamarin

我的应用程序可以使用闪光灯和相机,但它没有打开。该设备是Nexus三星。 我尝试设置FlashModeOn但它不起作用。请帮帮我。

[Activity(Label = "TurnOnLight", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
    public void initCamera()
    {


        var b = FindViewById<Button>(Resource.Id.MyButton);
        try
        {
            if (!hasCamera)
            {

                mcamera = Camera.Open();
                hasCamera = true;

                p=mcamera.GetParameters();
                var s = p.Get(Camera.Parameters.FlashModeOn);

                   p.Set(Camera.Parameters.FlashModeTorch,0);
                   mcamera.SetParameters(p);


                mcamera.StartPreview();
            }
            else
            {
            }
        }
        catch (Exception exception)
        {

            hasCamera = false;
        }

    }

1 个答案:

答案 0 :(得分:1)

这是我在ZXing.Net.Mobile项目中使用的代码,它对我来说很好用。请记住,FlashModeOn与FlashModeTorch略有不同:

https://github.com/Redth/ZXing.Net.Mobile/blob/master/src/ZXing.Net.Mobile/MonoForAndroid/ZXingSurfaceView.cs#L227-L267

public void Torch(bool on)
{
    if (!this.Context.PackageManager.HasSystemFeature(PackageManager.FeatureCameraFlash))
    {
        Android.Util.Log.Info("ZXING", "Flash not supported on this device");
        return;
    }

    if (camera == null)
    {
        Android.Util.Log.Info("ZXING", "NULL Camera");
        return;
    }

    var p = camera.GetParameters();
    var supportedFlashModes = p.SupportedFlashModes;

    if (supportedFlashModes == null)
        supportedFlashModes = new List<string>();

    var flashMode=  string.Empty;

    if (on)
    {
        if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch))
            flashMode = Android.Hardware.Camera.Parameters.FlashModeTorch;
        else if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeOn))
            flashMode = Android.Hardware.Camera.Parameters.FlashModeOn;
    }
    else 
    {
        if ( supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeOff))
            flashMode = Android.Hardware.Camera.Parameters.FlashModeOff;
    }

    if (!string.IsNullOrEmpty(flashMode))
    {
        p.FlashMode = flashMode;
        camera.SetParameters(p);
    }
}