2016-11-16 57 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緊湊,它失敗了。預覽正在改變(試圖聚焦),但總是失敗。 –