2016-11-16 55 views
0

我正在为Xamarin开发android应用程序。我必须用手机捕捉一些QR码。当用户点击屏幕时,相机必须执行自动对焦。无法使用相机执行自动对焦

有我的代码:

public class MainActivity 
{ 
    TextureView _textureView; 
    Camera _camera; 

    protected override void OnCreate(Bundle bundle) 
    { 
     _camera = Camera.Open(); 

     _textureView = FindViewById<TextureView>(Resource.Id.previewView); 
     _textureView.SurfaceTextureListener = this; 
     _textureView.Click += new EventHandler(clickFocus); 
    } 

    private void clickFocus(object sender, EventArgs e) 
    { 
     try 
     { 
      _camera.AutoFocus(this); 
     } 
     catch (Exception ex) 
     { 
     } 
    } 
} 

在其他类:

private void _mainactivity_onFocus(object sender, EventArgs e) 
{ 
    bool focus = (bool)sender; 

    if (!focus) 
    { 
     Activity.RunOnUiThread(() => 
     { 
      // _ma is the MainActivity 
      Toast.MakeText(_ma, Resource.String.camerafocusfailed, ToastLength.Short).Show(); 
     }); 
    } 
} 

我试图用的Nexus 5,此代码的工作好。但与索尼Xperia Z3紧凑型,总是有一个错误camerafocusfailed

我该怎么办?

回答

0

虽然我不确定你怎么能发送到bool ...你可以尝试找出相机是否支持自动对焦。

var autoFocus = PackageManager.HasSystemFeature("android.hardware.camera.autofocus"); 
+0

在Nexus 5上,我的代码工作正常,我试着专注于非常近的表面,焦点失败。它具有“正常”的距离。随着Z3紧凑,它失败了。预览正在改变(试图聚焦),但总是失败。 –